Skip to content

Commit 2997348

Browse files
committed
add rule to session loader
1 parent 40d1e3b commit 2997348

File tree

3 files changed

+50
-40
lines changed

3 files changed

+50
-40
lines changed

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

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,12 @@ import GHC.Unit.State
134134
import Language.LSP.Protocol.Types (NormalizedUri (NormalizedUri),
135135
toNormalizedFilePath)
136136
#endif
137+
import qualified Development.IDE.Core.Shake as SHake
137138

138139
data Log
140+
139141
= LogSettingInitialDynFlags
142+
| LogShake SHake.Log
140143
| LogGetInitialGhcLibDirDefaultCradleFail !CradleError !FilePath !(Maybe FilePath) !(Cradle Void)
141144
| LogGetInitialGhcLibDirDefaultCradleNone
142145
| LogHieDbRetry !Int !Int !Int !SomeException
@@ -157,6 +160,7 @@ data Log
157160
| LogSessionLoadingChanged
158161
deriving instance Show Log
159162

163+
160164
instance Pretty Log where
161165
pretty = \case
162166
LogNoneCradleFound path ->
@@ -227,6 +231,7 @@ instance Pretty Log where
227231
LogHieBios msg -> pretty msg
228232
LogSessionLoadingChanged ->
229233
"Session Loading config changed, reloading the full session."
234+
LogShake msg -> pretty msg
230235

231236
-- | Bump this version number when making changes to the format of the data stored in hiedb
232237
hiedbDataVersion :: String
@@ -440,10 +445,10 @@ getHieDbLoc dir = do
440445
-- This is the key function which implements multi-component support. All
441446
-- components mapping to the same hie.yaml file are mapped to the same
442447
-- HscEnv which is updated as new components are discovered.
443-
loadSession :: Recorder (WithPriority Log) -> FilePath -> IO (Action IdeGhcSession)
448+
loadSession :: Recorder (WithPriority Log) -> FilePath -> IO (Rules (), Action IdeGhcSession)
444449
loadSession recorder = loadSessionWithOptions recorder def
445450

446-
loadSessionWithOptions :: Recorder (WithPriority Log) -> SessionLoadingOptions -> FilePath -> IO (Action IdeGhcSession)
451+
loadSessionWithOptions :: Recorder (WithPriority Log) -> SessionLoadingOptions -> FilePath -> IO (Rules (), Action IdeGhcSession)
447452
loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir = do
448453
let toAbsolutePath = toAbsolute rootDir
449454
cradle_files <- newIORef []
@@ -463,7 +468,16 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir = do
463468
biosSessionLoadingVar <- newVar Nothing :: IO (Var (Maybe SessionLoadingPreferenceConfig))
464469
let returnWithVersion fun = IdeGhcSession fun <$> liftIO (readVar version)
465470

466-
return $ do
471+
let cradleLocRule :: Rules ()
472+
cradleLocRule = defineNoDiagnostics (cmapWithPrio LogShake recorder) $ \CradleLoc file -> do
473+
res <- liftIO $ HieBios.findCradle $ fromNormalizedFilePath file
474+
-- Sometimes we get C:, sometimes we get c:, and sometimes we get a relative path
475+
-- try and normalise that
476+
-- e.g. see https://github.com/haskell/ghcide/issues/126
477+
-- todo make it absolute
478+
return $ Just (normalise . toAbsolutePath <$> res)
479+
480+
return $ (cradleLocRule, do
467481
clientConfig <- getClientConfigAction
468482
extras@ShakeExtras{restartShakeSession, ideNc, knownTargetsVar, lspEnv
469483
} <- getShakeExtras
@@ -612,30 +626,30 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir = do
612626
keys2 <- liftIO $ invalidateShakeCache
613627

614628
-- todo this should be moving out of the session function
615-
restart <- liftIO $ async $ restartShakeSession VFSUnmodified "new component" [] $ do
616-
keys1 <- extendKnownTargets all_targets
617-
return [keys1, keys2]
629+
restart <- liftIO $ async $ do
630+
restartShakeSession VFSUnmodified "new component" [] $ do
631+
keys1 <- extendKnownTargets all_targets
632+
return [keys1, keys2]
633+
-- Typecheck all files in the project on startup
634+
checkProject <- liftIO $ getCheckProject
635+
liftIO $ unless (null new_deps || not checkProject) $ do
636+
cfps' <- liftIO $ filterM (IO.doesFileExist . fromNormalizedFilePath) (concatMap targetLocations all_targets)
637+
void $ shakeEnqueue extras $ mkDelayedAction "InitialLoad" Debug $ void $ do
638+
mmt <- uses GetModificationTime cfps'
639+
let cs_exist = catMaybes (zipWith (<$) cfps' mmt)
640+
modIfaces <- uses GetModIface cs_exist
641+
-- update exports map
642+
shakeExtras <- getShakeExtras
643+
let !exportsMap' = createExportsMap $ mapMaybe (fmap hirModIface) modIfaces
644+
liftIO $ atomically $ modifyTVar' (exportsMap shakeExtras) (exportsMap' <>)
618645
UnliftIO.wait restart
619-
620-
621-
-- Typecheck all files in the project on startup
622-
checkProject <- liftIO $ getCheckProject
623-
liftIO $ unless (null new_deps || not checkProject) $ do
624-
cfps' <- liftIO $ filterM (IO.doesFileExist . fromNormalizedFilePath) (concatMap targetLocations all_targets)
625-
void $ shakeEnqueue extras $ mkDelayedAction "InitialLoad" Debug $ void $ do
626-
mmt <- uses GetModificationTime cfps'
627-
let cs_exist = catMaybes (zipWith (<$) cfps' mmt)
628-
modIfaces <- uses GetModIface cs_exist
629-
-- update exports map
630-
shakeExtras <- getShakeExtras
631-
let !exportsMap' = createExportsMap $ mapMaybe (fmap hirModIface) modIfaces
632-
liftIO $ atomically $ modifyTVar' (exportsMap shakeExtras) (exportsMap' <>)
633-
634646
return $ second Map.keys this_options
635647

636648
let consultCradle :: NormalizedFilePath -> Action (IdeResult HscEnvEq, [FilePath])
637649
consultCradle cfp = do
638-
hieYaml <- use_ CradleLoc cfp
650+
hieYamlOld <- use_ CradleLoc cfp
651+
cachedHieYamlLocation <- join <$> liftIO (HM.lookup cfp <$> readVar filesMap)
652+
let hieYaml = fromMaybe cachedHieYamlLocation (Just hieYamlOld)
639653
let lfpLog = makeRelative rootDir (fromNormalizedFilePath cfp)
640654
logWith recorder Info $ LogCradlePath lfpLog
641655
when (isNothing hieYaml) $
@@ -701,7 +715,9 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir = do
701715
let sessionOpts :: NormalizedFilePath
702716
-> Action (IdeResult HscEnvEq, [FilePath])
703717
sessionOpts file = do
704-
hieYaml <- use_ CradleLoc file
718+
hieYamlOld <- use_ CradleLoc file
719+
cachedHieYamlLocation <- join <$> liftIO (HM.lookup file <$> readVar filesMap)
720+
let hieYaml = fromMaybe cachedHieYamlLocation (Just hieYamlOld)
705721
-- this cased a recompilation of the whole project
706722
-- this can be turned in to shake
707723
liftIO$Extra.whenM didSessionLoadingPreferenceConfigChange $ do
@@ -737,15 +753,15 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir = do
737753

738754
let getOptions :: NormalizedFilePath -> Action (IdeResult HscEnvEq, [FilePath])
739755
getOptions file = do
740-
-- cachedHieYamlLocation <- liftIO $ HM.lookup file <$> readVar filesMap
741756
-- CradleLoc already cached
742757
hieYaml <- use_ CradleLoc file
743758
sessionOpts file `Safe.catch` \e ->
744759
return (([renderPackageSetupException file e], Nothing), maybe [] pure hieYaml)
745760

761+
746762
returnWithVersion $ \file -> do
747763
opts <- UnliftIO.withMVar cradleLock $ const $ getOptions file
748-
pure $ (fmap . fmap) toAbsolutePath opts
764+
pure $ (fmap . fmap) toAbsolutePath opts)
749765

750766

751767
-- | Run the specific cradle on a specific FilePath via hie-bios.
@@ -1074,6 +1090,9 @@ type FilesMap = HM.HashMap NormalizedFilePath (Maybe FilePath)
10741090
-- if some new file4 should be in hie1.yaml,
10751091
-- we need to recompute the hie1.yaml
10761092

1093+
-- hieRule file
1094+
-- get corresponding hie.yaml
1095+
10771096

10781097

10791098
-- This is pristine information about a component

ghcide/src/Development/IDE/Core/Rules.hs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,6 @@ data Log
193193
| LogTypecheckedFOI !NormalizedFilePath
194194
deriving Show
195195

196-
cradleLocRule :: Recorder (WithPriority Log) -> Rules ()
197-
cradleLocRule recorder = defineNoDiagnostics (cmapWithPrio LogShake recorder) $ \CradleLoc file -> do
198-
res <- liftIO $ findCradle $ fromNormalizedFilePath file
199-
-- Sometimes we get C:, sometimes we get c:, and sometimes we get a relative path
200-
-- try and normalise that
201-
-- e.g. see https://github.com/haskell/ghcide/issues/126
202-
-- todo make it absolute
203-
return $ Just $ normalise <$> res
204196

205197
instance Pretty Log where
206198
pretty = \case
@@ -1229,7 +1221,6 @@ mainRule recorder RulesConfig{..} = do
12291221
addIdeGlobal $ CompiledLinkables linkables
12301222
rebuildCountVar <- liftIO $ newTVarIO 0
12311223
addIdeGlobal $ RebuildCounter rebuildCountVar
1232-
cradleLocRule recorder
12331224
getParsedModuleRule recorder
12341225
getParsedModuleWithCommentsRule recorder
12351226
getLocatedImportsRule recorder

ghcide/src/Development/IDE/Main.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re
337337
-- TODO: should probably catch/log/rethrow at top level instead
338338
`catchAny` (\e -> logWith recorder Error (LogSetInitialDynFlagsException e) >> pure Nothing)
339339

340-
sessionLoader <- loadSessionWithOptions (cmapWithPrio LogSession recorder) argsSessionLoadingOptions rootPath
340+
(sessionLoaderRule, sessionLoader) <- loadSessionWithOptions (cmapWithPrio LogSession recorder) argsSessionLoadingOptions rootPath
341341
config <- LSP.runLspT env LSP.getConfig
342342
let def_options = argsIdeOptions config sessionLoader
343343

@@ -356,7 +356,7 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re
356356
(cmapWithPrio LogService recorder)
357357
argsDefaultHlsConfig
358358
argsHlsPlugins
359-
rules
359+
(rules <> sessionLoaderRule)
360360
(Just env)
361361
debouncer
362362
ideOptions
@@ -408,14 +408,14 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re
408408
putStrLn $ "Found " ++ show n ++ " cradle" ++ ['s' | n /= 1]
409409
when (n > 0) $ putStrLn $ " (" ++ intercalate ", " (catMaybes ucradles) ++ ")"
410410
putStrLn "\nStep 3/4: Initializing the IDE"
411-
sessionLoader <- loadSessionWithOptions (cmapWithPrio LogSession recorder) argsSessionLoadingOptions dir
411+
(sessionLoaderRule, sessionLoader) <- loadSessionWithOptions (cmapWithPrio LogSession recorder) argsSessionLoadingOptions dir
412412
let def_options = argsIdeOptions argsDefaultHlsConfig sessionLoader
413413
ideOptions = def_options
414414
{ optCheckParents = pure NeverCheck
415415
, optCheckProject = pure False
416416
, optModifyDynFlags = optModifyDynFlags def_options <> pluginModifyDynflags plugins
417417
}
418-
ide <- initialise (cmapWithPrio LogService recorder) argsDefaultHlsConfig argsHlsPlugins rules Nothing debouncer ideOptions hiedb hieChan mempty dir
418+
ide <- initialise (cmapWithPrio LogService recorder) argsDefaultHlsConfig argsHlsPlugins (rules <> sessionLoaderRule) Nothing debouncer ideOptions hiedb hieChan mempty dir
419419
shakeSessionInit (cmapWithPrio LogShake recorder) ide
420420
registerIdeConfiguration (shakeExtras ide) $ IdeConfiguration mempty (hashed Nothing)
421421

@@ -446,14 +446,14 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re
446446
let root = argsProjectRoot
447447
dbLoc <- getHieDbLoc root
448448
runWithDb (cmapWithPrio LogSession recorder) dbLoc $ \hiedb hieChan -> do
449-
sessionLoader <- loadSessionWithOptions (cmapWithPrio LogSession recorder) argsSessionLoadingOptions "."
449+
(sessionLoaderRule, sessionLoader) <- loadSessionWithOptions (cmapWithPrio LogSession recorder) argsSessionLoadingOptions "."
450450
let def_options = argsIdeOptions argsDefaultHlsConfig sessionLoader
451451
ideOptions = def_options
452452
{ optCheckParents = pure NeverCheck
453453
, optCheckProject = pure False
454454
, optModifyDynFlags = optModifyDynFlags def_options <> pluginModifyDynflags plugins
455455
}
456-
ide <- initialise (cmapWithPrio LogService recorder) argsDefaultHlsConfig argsHlsPlugins rules Nothing debouncer ideOptions hiedb hieChan mempty root
456+
ide <- initialise (cmapWithPrio LogService recorder) argsDefaultHlsConfig argsHlsPlugins (rules <> sessionLoaderRule) Nothing debouncer ideOptions hiedb hieChan mempty root
457457
shakeSessionInit (cmapWithPrio LogShake recorder) ide
458458
registerIdeConfiguration (shakeExtras ide) $ IdeConfiguration mempty (hashed Nothing)
459459
c ide

0 commit comments

Comments
 (0)