Skip to content

Commit 24738d6

Browse files
committed
session: move handleBatchLoadSuccess inside
restartSession to avoid stale GhcSession results and lost diagnostics
1 parent dd6b562 commit 24738d6

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -817,10 +817,30 @@ session recorder sessionShake sessionState knownTargetsVar(hieYaml, cfp, opts, l
817817
let new_cache = newComponentCache (cmapWithPrio LogSessionGhc recorder) (optExtensions ideOptions) cfp hscEnv
818818
all_target_details <- liftIO $ new_cache old_deps new_deps
819819
(all_targets, this_flags_map) <- liftIO $ addErrorTargetIfUnknown all_target_details hieYaml cfp
820-
821-
liftIO $ handleBatchLoadSuccess recorder sessionState hieYaml this_flags_map all_targets
822-
-- Typecheck all files in the project on startup
823-
liftIO $ loadKnownTargets recorder sessionShake (optCheckProject ideOptions) knownTargetsVar new_deps all_targets
820+
-- The VFS doesn't change on cradle edits, re-use the old one.
821+
-- Invalidate all the existing GhcSession build nodes by restarting the Shake session
822+
liftIO $ do
823+
checkProject <- optCheckProject ideOptions
824+
restartSession sessionShake VFSUnmodified "new component" [] $ do
825+
-- It is necessary to call handleBatchLoadSuccess in restartSession
826+
-- to ensure the GhcSession rule does not return before a new session is started.
827+
-- Otherwise, invalid compilation results may propagate to downstream rules,
828+
-- potentially resulting in lost diagnostics and other issues.
829+
handleBatchLoadSuccess recorder sessionState hieYaml this_flags_map all_targets
830+
keys2 <- invalidateCache sessionShake
831+
keys1 <- extendKnownTargets recorder knownTargetsVar all_targets
832+
-- Typecheck all files in the project on startup
833+
unless (null new_deps || not checkProject) $ do
834+
cfps' <- liftIO $ filterM (IO.doesFileExist . fromNormalizedFilePath) (concatMap targetLocations all_targets)
835+
void $ enqueueActions sessionShake $ mkDelayedAction "InitialLoad" Debug $ void $ do
836+
mmt <- uses GetModificationTime cfps'
837+
let cs_exist = catMaybes (zipWith (<$) cfps' mmt)
838+
modIfaces <- uses GetModIface cs_exist
839+
-- update exports map
840+
shakeExtras <- getShakeExtras
841+
let !exportsMap' = createExportsMap $ mapMaybe (fmap hirModIface) modIfaces
842+
liftIO $ atomically $ modifyTVar' (exportsMap shakeExtras) (exportsMap' <>)
843+
return [keys1, keys2]
824844

825845
-- | Create a new HscEnv from a hieYaml root and a set of options
826846
packageSetup :: Recorder (WithPriority Log) -> SessionState -> SessionM HscEnv -> (Maybe FilePath, NormalizedFilePath, ComponentOptions) -> SessionM ([ComponentInfo], [ComponentInfo])
@@ -897,26 +917,6 @@ extendKnownTargets recorder knownTargetsVar newTargets = do
897917
logWith recorder Debug $ LogKnownFilesUpdated (targetMap x)
898918
return $ toNoFileKey GetKnownTargets
899919

900-
loadKnownTargets :: Recorder (WithPriority Log) -> SessionShake -> IO Bool -> TVar (Hashed KnownTargets) -> [ComponentInfo] -> [TargetDetails] -> IO ()
901-
loadKnownTargets recorder sessionShake getCheckProject knownTargetsVar new_deps targets = do
902-
checkProject <- getCheckProject
903-
904-
-- The VFS doesn't change on cradle edits, re-use the old one.
905-
-- Invalidate all the existing GhcSession build nodes by restarting the Shake session
906-
restartSession sessionShake VFSUnmodified "new component" [] $ do
907-
keys2 <- invalidateCache sessionShake
908-
keys1 <- extendKnownTargets recorder knownTargetsVar targets
909-
unless (null new_deps || not checkProject) $ do
910-
cfps' <- liftIO $ filterM (IO.doesFileExist . fromNormalizedFilePath) (concatMap targetLocations targets)
911-
void $ enqueueActions sessionShake $ mkDelayedAction "InitialLoad" Debug $ void $ do
912-
mmt <- uses GetModificationTime cfps'
913-
let cs_exist = catMaybes (zipWith (<$) cfps' mmt)
914-
modIfaces <- uses GetModIface cs_exist
915-
-- update exports map
916-
shakeExtras <- getShakeExtras
917-
let !exportsMap' = createExportsMap $ mapMaybe (fmap hirModIface) modIfaces
918-
liftIO $ atomically $ modifyTVar' (exportsMap shakeExtras) (exportsMap' <>)
919-
return [keys1, keys2]
920920

921921
loadCradleWithNotifications ::
922922
Recorder (WithPriority Log) ->

0 commit comments

Comments
 (0)