Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getCurrentDirectory calls in ghcide #1897

Merged
merged 4 commits into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions ghcide/session-loader/Development/IDE/Session.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ data SessionLoadingOptions = SessionLoadingOptions
-- or 'Nothing' to respect the cradle setting
, getCacheDirs :: String -> [String] -> IO CacheDirs
-- | Return the GHC lib dir to use for the 'unsafeGlobalDynFlags'
, getInitialGhcLibDir :: IO (Maybe LibDir)
, getInitialGhcLibDir :: FilePath -> IO (Maybe LibDir)
, fakeUid :: GHC.InstalledUnitId
-- ^ unit id used to tag the internal component built by ghcide
-- To reuse external interface files the unit ids must match,
Expand Down Expand Up @@ -140,31 +140,29 @@ loadWithImplicitCradle :: Maybe FilePath
-- if no 'hie.yaml' location is given.
-> IO (HieBios.Cradle Void)
loadWithImplicitCradle mHieYaml rootDir = do
crdl <- case mHieYaml of
case mHieYaml of
Just yaml -> HieBios.loadCradle yaml
Nothing -> loadImplicitHieCradle $ addTrailingPathSeparator rootDir
return crdl

getInitialGhcLibDirDefault :: IO (Maybe LibDir)
getInitialGhcLibDirDefault = do
dir <- IO.getCurrentDirectory
hieYaml <- findCradle def dir
cradle <- loadCradle def hieYaml dir
getInitialGhcLibDirDefault :: FilePath -> IO (Maybe LibDir)
getInitialGhcLibDirDefault rootDir = do
hieYaml <- findCradle def rootDir
cradle <- loadCradle def hieYaml rootDir
hPutStrLn stderr $ "setInitialDynFlags cradle: " ++ show cradle
libDirRes <- getRuntimeGhcLibDir cradle
case libDirRes of
CradleSuccess libdir -> pure $ Just $ LibDir libdir
CradleFail err -> do
hPutStrLn stderr $ "Couldn't load cradle for libdir: " ++ show (err,dir,hieYaml,cradle)
hPutStrLn stderr $ "Couldn't load cradle for libdir: " ++ show (err,rootDir,hieYaml,cradle)
pure Nothing
CradleNone -> do
hPutStrLn stderr "Couldn't load cradle (CradleNone)"
pure Nothing

-- | Sets `unsafeGlobalDynFlags` on using the hie-bios cradle and returns the GHC libdir
setInitialDynFlags :: SessionLoadingOptions -> IO (Maybe LibDir)
setInitialDynFlags SessionLoadingOptions{..} = do
libdir <- getInitialGhcLibDir
setInitialDynFlags :: FilePath -> SessionLoadingOptions -> IO (Maybe LibDir)
setInitialDynFlags rootDir SessionLoadingOptions{..} = do
libdir <- getInitialGhcLibDir rootDir
dynFlags <- mapM dynFlagsForPrinting libdir
mapM_ setUnsafeGlobalDynFlags dynFlags
pure libdir
Expand Down Expand Up @@ -423,6 +421,7 @@ loadSessionWithOptions SessionLoadingOptions{..} dir = do
logWarning logger $ implicitCradleWarning lfp

cradle <- loadCradle hieYaml dir
lfp <- flip makeRelative cfp <$> getCurrentDirectory

when optTesting $ mRunLspT lspEnv $
sendNotification (SCustomMethod "ghcide/cradle/loaded") (toJSON cfp)
Expand Down
3 changes: 1 addition & 2 deletions ghcide/src/Development/IDE/LSP/LanguageServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ runLanguageServer options inH outH getHieDbLoc defaultConfig onConfigurationChan
handleInit exitClientMsg clearReqId waitForCancel clientMsgChan env (RequestMessage _ _ m params) = otTracedHandler "Initialize" (show m) $ \sp -> do
traceWithSpan sp params
let root = LSP.resRootPath env

dir <- getCurrentDirectory
dir <- maybe getCurrentDirectory return root
dbLoc <- getHieDbLoc dir

-- The database needs to be open for the duration of the reactor thread, but we need to pass in a reference
Expand Down
15 changes: 7 additions & 8 deletions ghcide/src/Development/IDE/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import Control.Exception.Safe (Exception (displayExcept
import Control.Monad.Extra (concatMapM, unless,
when)
import Data.Default (Default (def))
import Data.Foldable (traverse_)
import qualified Data.HashMap.Strict as HashMap
import Data.Hashable (hashed)
import Data.List.Extra (intercalate, isPrefixOf,
nub, nubOrd, partition)
import Data.Maybe (catMaybes, fromMaybe,
isJust)
import Data.Maybe (catMaybes, isJust)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Development.IDE (Action, Rules,
Expand Down Expand Up @@ -203,21 +203,20 @@ defaultMain Arguments{..} = do
hPutStrLn stderr "Starting LSP server..."
hPutStrLn stderr "If you are seeing this in a terminal, you probably should have run WITHOUT the --lsp option!"
runLanguageServer options inH outH argsGetHieDbLoc argsDefaultHlsConfig argsOnConfigChange (pluginHandlers plugins) $ \env vfs rootPath hiedb hieChan -> do
traverse_ IO.setCurrentDirectory rootPath
t <- t
hPutStrLn stderr $ "Started LSP server in " ++ showDuration t

dir <- IO.getCurrentDirectory
dir <- maybe IO.getCurrentDirectory return rootPath

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


sessionLoader <- loadSessionWithOptions argsSessionLoadingOptions $ fromMaybe dir rootPath
sessionLoader <- loadSessionWithOptions argsSessionLoadingOptions dir
config <- LSP.runLspT env LSP.getConfig
let def_options = argsIdeOptions config sessionLoader

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