Skip to content

Commit 41a3c7f

Browse files
committed
fix uses of getCurrentDirectory in ghcide
fix uses of getCurrentDirectory in ghcide
1 parent 1e25507 commit 41a3c7f

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
@@ -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,
@@ -151,26 +151,25 @@ loadWithImplicitCradle mHieYaml rootDir = do
151151
setCurrentDirectory rootDir
152152
loadImplicitHieCradle $ addTrailingPathSeparator rootDir
153153

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

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