11{-# LANGUAGE CPP #-}
22{-# LANGUAGE TypeFamilies #-}
3- #include "ghc-api-version.h"
43
54{-|
65The logic for setting up a ghcide session by tapping into hie-bios.
@@ -48,6 +47,7 @@ import Development.IDE.GHC.Compat hiding (Target,
4847 TargetFile , TargetModule )
4948import qualified Development.IDE.GHC.Compat as GHC
5049import Development.IDE.GHC.Util
50+ import Development.IDE.Graph (Action )
5151import Development.IDE.Session.VersionCheck
5252import Development.IDE.Types.Diagnostics
5353import Development.IDE.Types.Exports
@@ -56,7 +56,6 @@ import Development.IDE.Types.HscEnvEq (HscEnvEq, newHscEnvEq,
5656import Development.IDE.Types.Location
5757import Development.IDE.Types.Logger
5858import Development.IDE.Types.Options
59- import Development.IDE.Graph (Action )
6059import GHC.Check
6160import qualified HIE.Bios as HieBios
6261import HIE.Bios.Environment hiding (getCacheDir )
@@ -85,12 +84,10 @@ import Control.Concurrent.STM (atomically)
8584import Control.Concurrent.STM.TQueue
8685import qualified Data.HashSet as Set
8786import Database.SQLite.Simple
88- import HIE.Bios.Cradle ( yamlConfig )
87+ import GHC.LanguageExtensions ( Extension ( EmptyCase ) )
8988import HieDb.Create
9089import HieDb.Types
9190import HieDb.Utils
92- import Maybes (MaybeT (runMaybeT ))
93- import GHC.LanguageExtensions (Extension (EmptyCase ))
9491
9592-- | Bump this version number when making changes to the format of the data stored in hiedb
9693hiedbDataVersion :: String
@@ -100,15 +97,18 @@ data CacheDirs = CacheDirs
10097 { hiCacheDir , hieCacheDir , oCacheDir :: Maybe FilePath }
10198
10299data SessionLoadingOptions = SessionLoadingOptions
103- { findCradle :: FilePath -> IO (Maybe FilePath )
104- , loadCradle :: FilePath -> IO (HieBios. Cradle Void )
100+ { findCradle :: FilePath -> IO (Maybe FilePath )
101+ -- | Load the cradle with an optional 'hie.yaml' location.
102+ -- If a 'hie.yaml' is given, use it to load the cradle.
103+ -- Otherwise, use the provided project root directory to determine the cradle type.
104+ , loadCradle :: Maybe FilePath -> FilePath -> IO (HieBios. Cradle Void )
105105 -- | Given the project name and a set of command line flags,
106106 -- return the path for storing generated GHC artifacts,
107107 -- or 'Nothing' to respect the cradle setting
108- , getCacheDirs :: String -> [String ] -> IO CacheDirs
108+ , getCacheDirs :: String -> [String ] -> IO CacheDirs
109109 -- | Return the GHC lib dir to use for the 'unsafeGlobalDynFlags'
110- , getInitialGhcLibDir :: IO (Maybe LibDir )
111- , fakeUid :: InstalledUnitId
110+ , getInitialGhcLibDir :: IO (Maybe LibDir )
111+ , 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,
114114 -- thus make sure to build them with `--this-unit-id` set to the
@@ -118,17 +118,39 @@ data SessionLoadingOptions = SessionLoadingOptions
118118instance Default SessionLoadingOptions where
119119 def = SessionLoadingOptions
120120 {findCradle = HieBios. findCradle
121- ,loadCradle = HieBios. loadCradle
121+ ,loadCradle = loadWithImplicitCradle
122122 ,getCacheDirs = getCacheDirsDefault
123123 ,getInitialGhcLibDir = getInitialGhcLibDirDefault
124124 ,fakeUid = toInstalledUnitId (stringToUnitId " main" )
125125 }
126126
127+ -- | Find the cradle for a given 'hie.yaml' configuration.
128+ --
129+ -- If a 'hie.yaml' is given, the cradle is read from the config.
130+ -- If this config does not comply to the "hie.yaml"
131+ -- specification, an error is raised.
132+ --
133+ -- If no location for "hie.yaml" is provided, the implicit config is used
134+ -- using the provided root directory for discovering the project.
135+ -- The implicit config uses different heuristics to determine the type
136+ -- of the project that may or may not be accurate.
137+ loadWithImplicitCradle :: Maybe FilePath
138+ -- ^ Optional 'hie.yaml' location. Will be used if given.
139+ -> FilePath
140+ -- ^ Root directory of the project. Required as a fallback
141+ -- if no 'hie.yaml' location is given.
142+ -> IO (HieBios. Cradle Void )
143+ loadWithImplicitCradle mHieYaml rootDir = do
144+ crdl <- case mHieYaml of
145+ Just yaml -> HieBios. loadCradle yaml
146+ Nothing -> loadImplicitHieCradle $ addTrailingPathSeparator rootDir
147+ return crdl
148+
127149getInitialGhcLibDirDefault :: IO (Maybe LibDir )
128150getInitialGhcLibDirDefault = do
129151 dir <- IO. getCurrentDirectory
130- hieYaml <- runMaybeT $ yamlConfig dir
131- cradle <- maybe (loadImplicitHieCradle $ addTrailingPathSeparator dir) HieBios. loadCradle hieYaml
152+ hieYaml <- findCradle def dir
153+ cradle <- loadCradle def hieYaml dir
132154 hPutStrLn stderr $ " setInitialDynFlags cradle: " ++ show cradle
133155 libDirRes <- getRuntimeGhcLibDir cradle
134156 case libDirRes of
@@ -400,7 +422,7 @@ loadSessionWithOptions SessionLoadingOptions{..} dir = do
400422 when (isNothing hieYaml) $
401423 logWarning logger $ implicitCradleWarning lfp
402424
403- cradle <- maybe (loadImplicitHieCradle $ addTrailingPathSeparator dir) loadCradle hieYaml
425+ cradle <- loadCradle hieYaml dir
404426
405427 when optTesting $ mRunLspT lspEnv $
406428 sendNotification (SCustomMethod " ghcide/cradle/loaded" ) (toJSON cfp)
0 commit comments