Skip to content

Commit a065cd6

Browse files
authored
Configuration for initial ghc lib dir (#1378)
* getInitialGhcLibDir * Fix build and use Data.Default consistently * Fix log line * Fix build * (unrelated) Honor the rules config in the setup tester
1 parent 430ba2d commit a065cd6

File tree

6 files changed

+37
-28
lines changed

6 files changed

+37
-28
lines changed

exe/Wrapper.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
module Main where
55

66
import Control.Monad.Extra
7+
import Data.Default
78
import Data.Foldable
89
import Data.List
910
import Data.Void
10-
import Development.IDE.Session (findCradle, defaultLoadingOptions)
11+
import Development.IDE.Session (findCradle)
1112
import HIE.Bios hiding (findCradle)
1213
import HIE.Bios.Environment
1314
import HIE.Bios.Types
@@ -140,7 +141,7 @@ getRuntimeGhcVersion' cradle = do
140141
-- of the project that may or may not be accurate.
141142
findLocalCradle :: FilePath -> IO (Cradle Void)
142143
findLocalCradle fp = do
143-
cradleConf <- findCradle defaultLoadingOptions fp
144+
cradleConf <- findCradle def fp
144145
crdl <- case cradleConf of
145146
Just yaml -> do
146147
hPutStrLn stderr $ "Found \"" ++ yaml ++ "\" for \"" ++ fp ++ "\""

ghcide/exe/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ main = do
6666
DbCmd opts cmd -> do
6767
dir <- IO.getCurrentDirectory
6868
dbLoc <- getHieDbLoc dir
69-
mlibdir <- setInitialDynFlags
69+
mlibdir <- setInitialDynFlags def
7070
case mlibdir of
7171
Nothing -> exitWith $ ExitFailure 1
7272
Just libdir -> runCommand libdir opts{database = dbLoc} cmd
@@ -79,7 +79,7 @@ main = do
7979
hPutStrLn stderr "If you are seeing this in a terminal, you probably should have run ghcide WITHOUT the --lsp option!"
8080
_ -> return ()
8181

82-
Main.defaultMain Main.defArguments
82+
Main.defaultMain def
8383
{Main.argFiles = case argFilesOrCmd of
8484
Typecheck x | not argLSP -> Just x
8585
_ -> Nothing

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ The logic for setting up a ghcide session by tapping into hie-bios.
88
module Development.IDE.Session
99
(SessionLoadingOptions(..)
1010
,CacheDirs(..)
11-
,defaultLoadingOptions
1211
,loadSession
1312
,loadSessionWithOptions
1413
,setInitialDynFlags
@@ -34,6 +33,7 @@ import qualified Data.Text as T
3433
import Data.Aeson
3534
import Data.Bifunctor
3635
import qualified Data.ByteString.Base16 as B16
36+
import Data.Default
3737
import Data.Either.Extra
3838
import Data.Function
3939
import Data.Hashable
@@ -98,31 +98,38 @@ data SessionLoadingOptions = SessionLoadingOptions
9898
-- return the path for storing generated GHC artifacts,
9999
-- or 'Nothing' to respect the cradle setting
100100
, getCacheDirs :: String -> [String] -> IO CacheDirs
101+
-- | Return the GHC lib dir to use for the 'unsafeGlobalDynFlags'
102+
, getInitialGhcLibDir :: IO (Maybe LibDir)
101103
}
102104

103-
defaultLoadingOptions :: SessionLoadingOptions
104-
defaultLoadingOptions = SessionLoadingOptions
105-
{findCradle = HieBios.findCradle
106-
,loadCradle = HieBios.loadCradle
107-
,getCacheDirs = getCacheDirsDefault
108-
}
105+
instance Default SessionLoadingOptions where
106+
def = SessionLoadingOptions
107+
{findCradle = HieBios.findCradle
108+
,loadCradle = HieBios.loadCradle
109+
,getCacheDirs = getCacheDirsDefault
110+
,getInitialGhcLibDir = getInitialGhcLibDirDefault
111+
}
109112

110-
-- | Sets `unsafeGlobalDynFlags` on using the hie-bios cradle and returns the GHC libdir
111-
setInitialDynFlags :: IO (Maybe LibDir)
112-
setInitialDynFlags = do
113+
getInitialGhcLibDirDefault :: IO (Maybe LibDir)
114+
getInitialGhcLibDirDefault = do
113115
dir <- IO.getCurrentDirectory
114116
hieYaml <- runMaybeT $ yamlConfig dir
115117
cradle <- maybe (loadImplicitHieCradle $ addTrailingPathSeparator dir) HieBios.loadCradle hieYaml
116118
hPutStrLn stderr $ "setInitialDynFlags cradle: " ++ show cradle
117119
libDirRes <- getRuntimeGhcLibDir cradle
118-
libdir <- case libDirRes of
120+
case libDirRes of
119121
CradleSuccess libdir -> pure $ Just $ LibDir libdir
120122
CradleFail err -> do
121123
hPutStrLn stderr $ "Couldn't load cradle for libdir: " ++ show (err,dir,hieYaml,cradle)
122124
pure Nothing
123125
CradleNone -> do
124126
hPutStrLn stderr $ "Couldn't load cradle (CradleNone)"
125127
pure Nothing
128+
129+
-- | Sets `unsafeGlobalDynFlags` on using the hie-bios cradle and returns the GHC libdir
130+
setInitialDynFlags :: SessionLoadingOptions -> IO (Maybe LibDir)
131+
setInitialDynFlags SessionLoadingOptions{..} = do
132+
libdir <- getInitialGhcLibDir
126133
dynFlags <- mapM dynFlagsForPrinting libdir
127134
mapM_ setUnsafeGlobalDynFlags dynFlags
128135
pure libdir
@@ -177,7 +184,7 @@ getHieDbLoc dir = do
177184
-- components mapping to the same hie.yaml file are mapped to the same
178185
-- HscEnv which is updated as new components are discovered.
179186
loadSession :: FilePath -> IO (Action IdeGhcSession)
180-
loadSession = loadSessionWithOptions defaultLoadingOptions
187+
loadSession = loadSessionWithOptions def
181188

182189
loadSessionWithOptions :: SessionLoadingOptions -> FilePath -> IO (Action IdeGhcSession)
183190
loadSessionWithOptions SessionLoadingOptions{..} dir = do
@@ -614,7 +621,7 @@ should be filtered out, such that we dont have to re-compile everything.
614621
-- For the exact reason, see Note [Avoiding bad interface files].
615622
setCacheDirs :: MonadIO m => Logger -> CacheDirs -> DynFlags -> m DynFlags
616623
setCacheDirs logger CacheDirs{..} dflags = do
617-
liftIO $ logInfo logger $ "Using interface files cache dir: " <> T.pack cacheDir
624+
liftIO $ logInfo logger $ "Using interface files cache dir: " <> T.pack (fromMaybe cacheDir hiCacheDir)
618625
pure $ dflags
619626
& maybe id setHiDir hiCacheDir
620627
& maybe id setHieDir hieCacheDir

ghcide/src/Development/IDE/Main.hs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Development.IDE.Main (Arguments(..), defArguments, defaultMain) where
1+
module Development.IDE.Main (Arguments(..), defaultMain) where
22
import Control.Concurrent.Extra (readVar)
33
import Control.Exception.Safe (
44
Exception (displayException),
@@ -47,7 +47,7 @@ import Development.IDE.Plugin (
4747
Plugin (pluginHandlers, pluginRules),
4848
)
4949
import Development.IDE.Plugin.HLS (asGhcIdePlugin)
50-
import Development.IDE.Session (SessionLoadingOptions, defaultLoadingOptions, loadSessionWithOptions, setInitialDynFlags, getHieDbLoc, runWithDb)
50+
import Development.IDE.Session (SessionLoadingOptions, loadSessionWithOptions, setInitialDynFlags, getHieDbLoc, runWithDb)
5151
import Development.IDE.Types.Location (toNormalizedFilePath')
5252
import Development.IDE.Types.Logger (Logger)
5353
import Development.IDE.Types.Options (
@@ -85,16 +85,15 @@ data Arguments = Arguments
8585
, argsGetHieDbLoc :: FilePath -> IO FilePath -- ^ Map project roots to the location of the hiedb for the project
8686
}
8787

88-
defArguments :: Arguments
89-
defArguments =
90-
Arguments
88+
instance Default Arguments where
89+
def = Arguments
9190
{ argsOTMemoryProfiling = False
9291
, argFiles = Nothing
9392
, argsLogger = noLogging
9493
, argsRules = mainRule >> action kick
9594
, argsGhcidePlugin = mempty
9695
, argsHlsPlugins = pluginDescToIdePlugins Ghcide.descriptors
97-
, argsSessionLoadingOptions = defaultLoadingOptions
96+
, argsSessionLoadingOptions = def
9897
, argsIdeOptions = const defaultIdeOptions
9998
, argsLspOptions = def {LSP.completionTriggerCharacters = Just "."}
10099
, argsDefaultHlsConfig = def
@@ -110,6 +109,7 @@ defaultMain Arguments{..} = do
110109
plugins = hlsPlugin <> argsGhcidePlugin
111110
options = argsLspOptions { LSP.executeCommandCommands = Just hlsCommands }
112111
argsOnConfigChange _ide = pure . getConfigFromNotification argsDefaultHlsConfig
112+
rules = argsRules >> pluginRules plugins
113113

114114
case argFiles of
115115
Nothing -> do
@@ -127,15 +127,14 @@ defaultMain Arguments{..} = do
127127
-- We do it here since haskell-lsp changes our working directory to the correct place ('rootPath')
128128
-- before calling this function
129129
_mlibdir <-
130-
setInitialDynFlags
130+
setInitialDynFlags argsSessionLoadingOptions
131131
`catchAny` (\e -> (hPutStrLn stderr $ "setInitialDynFlags: " ++ displayException e) >> pure Nothing)
132132

133133
sessionLoader <- loadSessionWithOptions argsSessionLoadingOptions $ fromMaybe dir rootPath
134134
config <- LSP.runLspT env LSP.getConfig
135135
let options = (argsIdeOptions config sessionLoader)
136136
{ optReportProgress = clientSupportsProgress caps
137137
}
138-
rules = argsRules >> pluginRules plugins
139138
caps = LSP.resClientCapabilities env
140139
debouncer <- newAsyncDebouncer
141140
initialise
@@ -178,7 +177,7 @@ defaultMain Arguments{..} = do
178177
{ optCheckParents = pure NeverCheck
179178
, optCheckProject = pure False
180179
}
181-
ide <- initialise mainRule Nothing argsLogger debouncer options vfs hiedb hieChan
180+
ide <- initialise rules Nothing argsLogger debouncer options vfs hiedb hieChan
182181

183182
putStrLn "\nStep 4/4: Type checking the files"
184183
setFilesOfInterest ide $ HashMap.fromList $ map ((,OnDisk) . toNormalizedFilePath') files

haskell-language-server.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ executable haskell-language-server-wrapper
365365
ghc-options: -Werror
366366

367367
build-depends:
368+
, data-default
368369
, ghc
369370
, ghc-paths
370371
, ghcide

src/Ide/Main.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import HieDb.Run
3131
import qualified Development.IDE.Main as Main
3232
import qualified Development.IDE.Types.Options as Ghcide
3333
import Development.Shake (ShakeOptions(shakeThreads))
34+
import Data.Default
3435

3536
defaultMain :: Arguments -> IdePlugins IdeState -> IO ()
3637
defaultMain args idePlugins = do
@@ -55,7 +56,7 @@ defaultMain args idePlugins = do
5556
dir <- IO.getCurrentDirectory
5657
dbLoc <- getHieDbLoc dir
5758
hPutStrLn stderr $ "Using hiedb at: " ++ dbLoc
58-
mlibdir <- setInitialDynFlags
59+
mlibdir <- setInitialDynFlags def
5960
case mlibdir of
6061
Nothing -> exitWith $ ExitFailure 1
6162
Just libdir ->
@@ -93,7 +94,7 @@ runLspMode lspArgs@LspArguments{..} idePlugins = do
9394
hPutStrLn stderr $ " in directory: " <> dir
9495
hPutStrLn stderr "If you are seeing this in a terminal, you probably should have run ghcide WITHOUT the --lsp option!"
9596

96-
Main.defaultMain Main.defArguments
97+
Main.defaultMain def
9798
{ Main.argFiles = if argLSP then Nothing else Just []
9899
, Main.argsHlsPlugins = idePlugins
99100
, Main.argsLogger = hlsLogger

0 commit comments

Comments
 (0)