Skip to content

Commit 861c8bf

Browse files
authored
Fix getCurrentDirectory calls in ghcide (#1897)
* loadCradle: change working dir to cradle location * fix uses of getCurrentDirectory in ghcide fix uses of getCurrentDirectory in ghcide * clean up comment and fromMaybe * Set working directory to workspace root when argCommand is LSP
1 parent 0b3bb10 commit 861c8bf

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ data SessionLoadingOptions = SessionLoadingOptions
106106
-- or 'Nothing' to respect the cradle setting
107107
, getCacheDirs :: String -> [String] -> IO CacheDirs
108108
-- | Return the GHC lib dir to use for the 'unsafeGlobalDynFlags'
109-
, getInitialGhcLibDir :: IO (Maybe LibDir)
109+
, getInitialGhcLibDir :: FilePath -> IO (Maybe LibDir)
110110
, fakeUid :: GHC.InstalledUnitId
111111
-- ^ unit id used to tag the internal component built by ghcide
112112
-- To reuse external interface files the unit ids must match,
@@ -140,31 +140,29 @@ loadWithImplicitCradle :: Maybe FilePath
140140
-- if no 'hie.yaml' location is given.
141141
-> IO (HieBios.Cradle Void)
142142
loadWithImplicitCradle mHieYaml rootDir = do
143-
crdl <- case mHieYaml of
143+
case mHieYaml of
144144
Just yaml -> HieBios.loadCradle yaml
145145
Nothing -> loadImplicitHieCradle $ addTrailingPathSeparator rootDir
146-
return crdl
147146

148-
getInitialGhcLibDirDefault :: IO (Maybe LibDir)
149-
getInitialGhcLibDirDefault = do
150-
dir <- IO.getCurrentDirectory
151-
hieYaml <- findCradle def dir
152-
cradle <- loadCradle def hieYaml dir
147+
getInitialGhcLibDirDefault :: FilePath -> IO (Maybe LibDir)
148+
getInitialGhcLibDirDefault rootDir = do
149+
hieYaml <- findCradle def rootDir
150+
cradle <- loadCradle def hieYaml rootDir
153151
hPutStrLn stderr $ "setInitialDynFlags cradle: " ++ show cradle
154152
libDirRes <- getRuntimeGhcLibDir cradle
155153
case libDirRes of
156154
CradleSuccess libdir -> pure $ Just $ LibDir libdir
157155
CradleFail err -> do
158-
hPutStrLn stderr $ "Couldn't load cradle for libdir: " ++ show (err,dir,hieYaml,cradle)
156+
hPutStrLn stderr $ "Couldn't load cradle for libdir: " ++ show (err,rootDir,hieYaml,cradle)
159157
pure Nothing
160158
CradleNone -> do
161159
hPutStrLn stderr "Couldn't load cradle (CradleNone)"
162160
pure Nothing
163161

164162
-- | Sets `unsafeGlobalDynFlags` on using the hie-bios cradle and returns the GHC libdir
165-
setInitialDynFlags :: SessionLoadingOptions -> IO (Maybe LibDir)
166-
setInitialDynFlags SessionLoadingOptions{..} = do
167-
libdir <- getInitialGhcLibDir
163+
setInitialDynFlags :: FilePath -> SessionLoadingOptions -> IO (Maybe LibDir)
164+
setInitialDynFlags rootDir SessionLoadingOptions{..} = do
165+
libdir <- getInitialGhcLibDir rootDir
168166
dynFlags <- mapM dynFlagsForPrinting libdir
169167
mapM_ setUnsafeGlobalDynFlags dynFlags
170168
pure libdir
@@ -423,6 +421,7 @@ loadSessionWithOptions SessionLoadingOptions{..} dir = do
423421
logWarning logger $ implicitCradleWarning lfp
424422

425423
cradle <- loadCradle hieYaml dir
424+
lfp <- flip makeRelative cfp <$> getCurrentDirectory
426425

427426
when optTesting $ mRunLspT lspEnv $
428427
sendNotification (SCustomMethod "ghcide/cradle/loaded") (toJSON cfp)

ghcide/src/Development/IDE/LSP/LanguageServer.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ runLanguageServer options inH outH getHieDbLoc defaultConfig onConfigurationChan
122122
handleInit exitClientMsg clearReqId waitForCancel clientMsgChan env (RequestMessage _ _ m params) = otTracedHandler "Initialize" (show m) $ \sp -> do
123123
traceWithSpan sp params
124124
let root = LSP.resRootPath env
125-
126-
dir <- getCurrentDirectory
125+
dir <- maybe getCurrentDirectory return root
127126
dbLoc <- getHieDbLoc dir
128127

129128
-- The database needs to be open for the duration of the reactor thread, but we need to pass in a reference

ghcide/src/Development/IDE/Main.hs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import Control.Exception.Safe (Exception (displayExcept
1414
import Control.Monad.Extra (concatMapM, unless,
1515
when)
1616
import Data.Default (Default (def))
17+
import Data.Foldable (traverse_)
1718
import qualified Data.HashMap.Strict as HashMap
1819
import Data.Hashable (hashed)
1920
import Data.List.Extra (intercalate, isPrefixOf,
2021
nub, nubOrd, partition)
21-
import Data.Maybe (catMaybes, fromMaybe,
22-
isJust)
22+
import Data.Maybe (catMaybes, isJust)
2323
import qualified Data.Text as T
2424
import qualified Data.Text.IO as T
2525
import Development.IDE (Action, Rules,
@@ -203,21 +203,20 @@ defaultMain Arguments{..} = do
203203
hPutStrLn stderr "Starting LSP server..."
204204
hPutStrLn stderr "If you are seeing this in a terminal, you probably should have run WITHOUT the --lsp option!"
205205
runLanguageServer options inH outH argsGetHieDbLoc argsDefaultHlsConfig argsOnConfigChange (pluginHandlers plugins) $ \env vfs rootPath hiedb hieChan -> do
206+
traverse_ IO.setCurrentDirectory rootPath
206207
t <- t
207208
hPutStrLn stderr $ "Started LSP server in " ++ showDuration t
208209

209-
dir <- IO.getCurrentDirectory
210+
dir <- maybe IO.getCurrentDirectory return rootPath
210211

211212
-- We want to set the global DynFlags right now, so that we can use
212213
-- `unsafeGlobalDynFlags` even before the project is configured
213-
-- We do it here since haskell-lsp changes our working directory to the correct place ('rootPath')
214-
-- before calling this function
215214
_mlibdir <-
216-
setInitialDynFlags argsSessionLoadingOptions
215+
setInitialDynFlags dir argsSessionLoadingOptions
217216
`catchAny` (\e -> (hPutStrLn stderr $ "setInitialDynFlags: " ++ displayException e) >> pure Nothing)
218217

219218

220-
sessionLoader <- loadSessionWithOptions argsSessionLoadingOptions $ fromMaybe dir rootPath
219+
sessionLoader <- loadSessionWithOptions argsSessionLoadingOptions dir
221220
config <- LSP.runLspT env LSP.getConfig
222221
let def_options = argsIdeOptions config sessionLoader
223222

@@ -307,7 +306,7 @@ defaultMain Arguments{..} = do
307306
Db dir opts cmd -> do
308307
dbLoc <- getHieDbLoc dir
309308
hPutStrLn stderr $ "Using hiedb at: " ++ dbLoc
310-
mlibdir <- setInitialDynFlags def
309+
mlibdir <- setInitialDynFlags dir def
311310
case mlibdir of
312311
Nothing -> exitWith $ ExitFailure 1
313312
Just libdir -> HieDb.runCommand libdir opts{HieDb.database = dbLoc} cmd

0 commit comments

Comments
 (0)