Skip to content

Commit eda4227

Browse files
committed
cleanup
1 parent 6c4a848 commit eda4227

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import qualified Development.IDE.GHC.Compat.Core as GHC
6161
import Development.IDE.GHC.Compat.Env hiding (Logger)
6262
import Development.IDE.GHC.Compat.Units (UnitId)
6363
import Development.IDE.GHC.Util
64-
import Development.IDE.Graph (Action)
64+
import Development.IDE.Graph (Action, alwaysRerun)
6565
import qualified Development.IDE.Session.Implicit as GhcIde
6666
import Development.IDE.Session.VersionCheck
6767
import Development.IDE.Types.Diagnostics
@@ -134,6 +134,7 @@ import GHC.Unit.State
134134
import Language.LSP.Protocol.Types (NormalizedUri (NormalizedUri),
135135
toNormalizedFilePath)
136136
#endif
137+
import Development.IDE (RuleResult)
137138
import qualified Development.IDE.Core.Shake as SHake
138139

139140
data Log
@@ -448,6 +449,9 @@ getHieDbLoc dir = do
448449
loadSession :: Recorder (WithPriority Log) -> FilePath -> IO (Rules (), Action IdeGhcSession)
449450
loadSession recorder = loadSessionWithOptions recorder def
450451

452+
type instance RuleResult HieYaml = (HashMap
453+
NormalizedFilePath (IdeResult HscEnvEq, DependencyInfo))
454+
451455
loadSessionWithOptions :: Recorder (WithPriority Log) -> SessionLoadingOptions -> FilePath -> IO (Rules (), Action IdeGhcSession)
452456
loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir = do
453457
let toAbsolutePath = toAbsolute rootDir
@@ -461,13 +465,25 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir = do
461465
-- they are inconsistent. So, everywhere you modify 'fileToFlags',
462466
-- you have to modify 'filesMap' as well.
463467
filesMap <- newVar HM.empty :: IO (Var FilesMap)
468+
469+
let clearCache = do
470+
modifyVar_ hscEnvs $ \_ -> pure Map.empty
471+
modifyVar_ fileToFlags $ \_ -> pure Map.empty
472+
modifyVar_ filesMap $ \_ -> pure HM.empty
473+
464474
-- Version of the mappings above
465475
version <- newVar 0
466476
cradleLock <- newMVar ()
467477
-- putMVar cradleLock ()
468478
biosSessionLoadingVar <- newVar Nothing :: IO (Var (Maybe SessionLoadingPreferenceConfig))
469479
let returnWithVersion fun = IdeGhcSession fun <$> liftIO (readVar version)
470480

481+
let hieYamlRule :: Rules ()
482+
hieYamlRule = defineNoDiagnostics (cmapWithPrio LogShake recorder) $ \HieYaml hieYaml -> do
483+
alwaysRerun
484+
v <- Map.findWithDefault HM.empty (Just $ fromNormalizedFilePath hieYaml) <$> (liftIO $ readVar fileToFlags)
485+
return $ Just v
486+
471487
let cradleLocRule :: Rules ()
472488
cradleLocRule = defineNoDiagnostics (cmapWithPrio LogShake recorder) $ \CradleLoc file -> do
473489
res <- liftIO $ HieBios.findCradle $ fromNormalizedFilePath file
@@ -477,19 +493,21 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir = do
477493
-- todo make it absolute
478494
return $ Just (normalise . toAbsolutePath <$> res)
479495

480-
return $ (cradleLocRule, do
481-
clientConfig <- getClientConfigAction
482-
extras@ShakeExtras{restartShakeSession, ideNc, knownTargetsVar, lspEnv
483-
} <- getShakeExtras
484-
let invalidateShakeCache = do
496+
let invalidateShakeCache = do
485497
void $ modifyVar' version succ
486498
return $ toNoFileKey GhcSessionIO
487499

500+
return $ (cradleLocRule <> hieYamlRule, do
501+
clientConfig <- getClientConfigAction
502+
ShakeExtras{restartShakeSession, ideNc, knownTargetsVar, lspEnv
503+
} <- getShakeExtras
504+
488505
IdeOptions{ optTesting = IdeTesting optTesting
489506
, optCheckProject = getCheckProject
490507
, optExtensions
491508
} <- getIdeOptions
492509

510+
-- relatively stand alone
493511
-- populate the knownTargetsVar with all the
494512
-- files in the project so that `knownFiles` can learn about them and
495513
-- we can generate a complete module graph
@@ -722,28 +740,21 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir = do
722740
let hieYaml = fromMaybe cachedHieYamlLocation (Just hieYamlOld)
723741
-- this cased a recompilation of the whole project
724742
-- this can be turned in to shake
725-
liftIO$Extra.whenM didSessionLoadingPreferenceConfigChange $ do
743+
liftIO $ Extra.whenM didSessionLoadingPreferenceConfigChange $ do
726744
logWith recorder Info LogSessionLoadingChanged
727745
-- If the dependencies are out of date then clear both caches and start
728746
-- again.
729-
liftIO$ modifyVar_ fileToFlags (const (return Map.empty))
730-
liftIO$modifyVar_ filesMap (const (return HM.empty))
731-
-- Don't even keep the name cache, we start from scratch here!
732-
liftIO$modifyVar_ hscEnvs (const (return Map.empty))
733-
747+
clearCache
734748
-- fileToFlags is caching
735749
v <- Map.findWithDefault HM.empty hieYaml <$> (liftIO$readVar fileToFlags)
736750
case HM.lookup file v of
737751
Just (opts, old_di) -> do
738-
deps_ok <- liftIO$checkDependencyInfo old_di
752+
deps_ok <- liftIO $ checkDependencyInfo old_di
739753
if not deps_ok
740754
then do
741755
-- If the dependencies are out of date then clear both caches and start
742756
-- again.
743-
liftIO$modifyVar_ fileToFlags (const (return Map.empty))
744-
liftIO$modifyVar_ filesMap (const (return HM.empty))
745-
-- Keep the same name cache
746-
liftIO$modifyVar_ hscEnvs (return . Map.adjust (const []) hieYaml )
757+
liftIO $ clearCache
747758
consultCradle file
748759
else return (opts, Map.keys old_di)
749760
Nothing -> consultCradle file

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,11 @@ instance Hashable CradleLoc
497497
instance NFData CradleLoc
498498
type instance RuleResult CradleLoc = Maybe FilePath
499499

500+
data HieYaml = HieYaml deriving (Eq, Show, Typeable, Generic)
501+
instance Hashable HieYaml
502+
instance NFData HieYaml
503+
504+
500505
-- A local rule type to get caching. We want to use newCache, but it has
501506
-- thread killed exception issues, so we lift it to a full rule.
502507
-- https://github.com/digital-asset/daml/pull/2808#issuecomment-529639547

0 commit comments

Comments
 (0)