Skip to content

Commit 1425289

Browse files
committed
refactor session loading error handling for improved clarity and separation of concerns
1 parent e339c1d commit 1425289

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ import Control.Concurrent.STM (STM)
122122
import qualified Control.Monad.STM as STM
123123
import qualified Development.IDE.Session.OrderedSet as S
124124
import qualified Focus
125-
import GHC.Data.Bag
126125
import GHC.Driver.Env (hsc_all_home_unit_ids)
127126
import GHC.Driver.Errors.Types
128127
import GHC.Types.Error (errMsgDiagnostic,
@@ -423,20 +422,37 @@ SessionState manages the state for batch loading files in the session loader.
423422
424423
- When a new file needs to be loaded, it is added to the pendingFiles set.
425424
- The loader processes files from pendingFiles, attempting to load them in batches.
426-
- If a file is already in failedFiles, it is loaded individually (single-file mode).
427-
- Otherwise, the loader tries to load as many files as possible together (batch mode).
425+
- (SBL1) If a file is already in failedFiles, it is loaded individually (single-file mode).
426+
- (SBL2) Otherwise, the loader tries to load as many files as possible together (batch mode).
428427
429428
On success:
430-
- All successfully loaded files are removed from pendingFiles and failedFiles,
429+
- (SBL3) All successfully loaded files are removed from pendingFiles and failedFiles,
431430
and added to loadedFiles.
432431
433432
On failure:
434-
- If loading a single file fails, it is added to failedFiles and removed from loadedFiles and pendingFiles.
435-
- If batch loading fails, all files attempted are added to failedFiles.
433+
- (SBL4) If loading a single file fails, it is added to failedFiles and removed from loadedFiles and pendingFiles.
434+
- (SBL5) If batch loading fails, all files attempted are added to failedFiles.
436435
437436
This approach ensures efficient batch loading while isolating problematic files for individual handling.
438437
-}
439438

439+
handleLoadingSucc :: SessionState -> HashSet FilePath -> IO ()
440+
handleLoadingSucc sessionState files = do
441+
atomically $ forM_ (Set.toList files) $ flip S.delete (pendingFiles sessionState)
442+
mapM_ (removeErrorLoadingFile sessionState) (Set.toList files)
443+
addCradleFiles sessionState files
444+
445+
handleLoadingFailureBatch :: SessionState -> [FilePath] -> IO ()
446+
handleLoadingFailureBatch sessionState files = do
447+
addErrorLoadingFiles sessionState files
448+
449+
handleLoadingFailureSingle :: SessionState -> FilePath -> IO ()
450+
handleLoadingFailureSingle sessionState file = do
451+
addErrorLoadingFile sessionState file
452+
removeErrorLoadingFile sessionState file
453+
atomically $ S.delete file (pendingFiles sessionState)
454+
removeCradleFile sessionState file
455+
440456
data SessionState = SessionState
441457
{ loadedFiles :: !(IORef (HashSet FilePath)),
442458
failedFiles :: !(IORef (HashSet FilePath)),
@@ -530,15 +546,13 @@ handleSessionError state hieYaml file e = do
530546
-- | Common pattern: Insert file flags, insert file mapping, and remove from pending
531547
handleFileProcessingError :: SessionState -> Maybe FilePath -> FilePath -> [FileDiagnostic] -> [FilePath] -> IO ()
532548
handleFileProcessingError state hieYaml file diags extraDepFiles = do
533-
addErrorLoadingFile state file
534-
removeCradleFile state file
535549
dep <- getDependencyInfo $ maybeToList hieYaml <> extraDepFiles
536550
let ncfp = toNormalizedFilePath' file
537551
let flags = ((diags, Nothing), dep)
552+
handleLoadingFailureSingle state file
538553
atomically $ do
539554
insertFileFlags state hieYaml ncfp flags
540555
insertFileMapping state hieYaml ncfp
541-
removeFromPending state file
542556

543557
-- | Get the set of extra files to load based on the current file path
544558
-- If the current file is in error loading files, we fallback to single loading mode (empty set)
@@ -738,12 +752,9 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
738752
atomically $ do
739753
STM.insert this_flags_map hieYaml (fileToFlags sessionState)
740754
insertAllFileMappings sessionState $ map ((hieYaml,) . fst) $ concatMap toFlagsMap all_targets
741-
forM_ newLoaded $ flip S.delete (pendingFiles sessionState)
742755

743756
logWith recorder Info $ LogSessionNewLoadedFiles $ Set.toList newLoaded
744-
-- remove all new loaded file from error loading files
745-
mapM_ (removeErrorLoadingFile sessionState) (Set.toList newLoaded)
746-
addCradleFiles sessionState newLoaded
757+
handleLoadingSucc sessionState newLoaded
747758
-- Typecheck all files in the project on startup
748759
checkProject <- getCheckProject
749760

@@ -814,7 +825,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
814825
-- are changed, and old_files are not valid anymore.
815826
-- but they will still be in the old_files, and will not move to failedFiles.
816827
-- And make other files failed to load in batch mode.
817-
addErrorLoadingFiles sessionState errorToLoadNewFiles
828+
handleLoadingFailureBatch sessionState errorToLoadNewFiles
818829
-- retry without other files
819830
logWith recorder Info $ LogSessionReloadOnError cfp (Set.toList attemptToLoadFiles)
820831
consultCradle hieYaml cfp

0 commit comments

Comments
 (0)