Skip to content

Commit 40c77a3

Browse files
committed
fix uses of getCurrentDirectory in ghcide
1 parent 77f87fb commit 40c77a3

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ data SessionLoadingOptions = SessionLoadingOptions
107107
-- or 'Nothing' to respect the cradle setting
108108
, getCacheDirs :: String -> [String] -> IO CacheDirs
109109
-- | Return the GHC lib dir to use for the 'unsafeGlobalDynFlags'
110-
, getInitialGhcLibDir :: IO (Maybe LibDir)
110+
, getInitialGhcLibDir :: FilePath -> IO (Maybe LibDir)
111111
, fakeUid :: InstalledUnitId
112112
-- ^ unit id used to tag the internal component built by ghcide
113113
-- To reuse external interface files the unit ids must match,
@@ -152,26 +152,25 @@ loadWithImplicitCradle mHieYaml rootDir = do
152152
setCurrentDirectory rootDir
153153
loadImplicitHieCradle $ addTrailingPathSeparator rootDir
154154

155-
getInitialGhcLibDirDefault :: IO (Maybe LibDir)
156-
getInitialGhcLibDirDefault = do
157-
dir <- IO.getCurrentDirectory
158-
hieYaml <- findCradle def dir
159-
cradle <- loadCradle def hieYaml dir
155+
getInitialGhcLibDirDefault :: FilePath -> IO (Maybe LibDir)
156+
getInitialGhcLibDirDefault rootDir = do
157+
hieYaml <- findCradle def rootDir
158+
cradle <- loadCradle def hieYaml rootDir
160159
hPutStrLn stderr $ "setInitialDynFlags cradle: " ++ show cradle
161160
libDirRes <- getRuntimeGhcLibDir cradle
162161
case libDirRes of
163162
CradleSuccess libdir -> pure $ Just $ LibDir libdir
164163
CradleFail err -> do
165-
hPutStrLn stderr $ "Couldn't load cradle for libdir: " ++ show (err,dir,hieYaml,cradle)
164+
hPutStrLn stderr $ "Couldn't load cradle for libdir: " ++ show (err,rootDir,hieYaml,cradle)
166165
pure Nothing
167166
CradleNone -> do
168167
hPutStrLn stderr "Couldn't load cradle (CradleNone)"
169168
pure Nothing
170169

171170
-- | Sets `unsafeGlobalDynFlags` on using the hie-bios cradle and returns the GHC libdir
172-
setInitialDynFlags :: SessionLoadingOptions -> IO (Maybe LibDir)
173-
setInitialDynFlags SessionLoadingOptions{..} = do
174-
libdir <- getInitialGhcLibDir
171+
setInitialDynFlags :: FilePath -> SessionLoadingOptions -> IO (Maybe LibDir)
172+
setInitialDynFlags rootDir SessionLoadingOptions{..} = do
173+
libdir <- getInitialGhcLibDir rootDir
175174
dynFlags <- mapM dynFlagsForPrinting libdir
176175
mapM_ setUnsafeGlobalDynFlags dynFlags
177176
pure libdir

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,14 @@ defaultMain Arguments{..} = do
206206
t <- t
207207
hPutStrLn stderr $ "Started LSP server in " ++ showDuration t
208208

209-
dir <- IO.getCurrentDirectory
209+
dir <- maybe IO.getCurrentDirectory return rootPath
210210

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

219219

@@ -307,7 +307,7 @@ defaultMain Arguments{..} = do
307307
Db dir opts cmd -> do
308308
dbLoc <- getHieDbLoc dir
309309
hPutStrLn stderr $ "Using hiedb at: " ++ dbLoc
310-
mlibdir <- setInitialDynFlags def
310+
mlibdir <- setInitialDynFlags dir def
311311
case mlibdir of
312312
Nothing -> exitWith $ ExitFailure 1
313313
Just libdir -> HieDb.runCommand libdir opts{HieDb.database = dbLoc} cmd

0 commit comments

Comments
 (0)