Skip to content

Commit

Permalink
refactor loadSessionWithOptions to improve pending file handling and …
Browse files Browse the repository at this point in the history
…error management
  • Loading branch information
soulomoon committed Nov 4, 2024
1 parent 6e04d28 commit 67aebc4
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions ghcide/session-loader/Development/IDE/Session.hs
Original file line number Diff line number Diff line change
Expand Up @@ -625,17 +625,18 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
let progMsg = "Setting up " <> T.pack (takeBaseName (cradleRootDir cradle))
<> " (for " <> T.pack lfpLog <> ")"

pendingFiles' <- Set.fromList <$> (atomically $ flushTQueue pendingFilesTQueue)
pendingFiles <- Set.insert cfp . Set.fromList <$> (atomically $ flushTQueue pendingFilesTQueue)
errorFiles <- readIORef error_loading_files
-- remove error files from pending files since error loading need to load one by one
let pendingFiles = pendingFiles' `Set.difference` errorFiles
old_files <- readIORef cradle_files
-- if the file is in error loading files, we fall back to single loading mode
let extraToLoads = if cfp `Set.member` errorFiles then Set.empty else pendingFiles
let extraToLoads = if cfp `Set.member` errorFiles
then Set.empty
-- remove error files from pending files since error loading need to load one by one
else Set.delete cfp $ pendingFiles `Set.difference` errorFiles

eopts <- mRunLspTCallback lspEnv (\act -> withIndefiniteProgress progMsg Nothing NotCancellable (const act)) $
withTrace "Load cradle" $ \addTag -> do
addTag "file" lfpLog
old_files <- readIORef cradle_files
res <- cradleToOptsAndLibDir recorder (sessionLoading clientConfig) cradle cfp (Set.toList $ Set.delete cfp $ extraToLoads <> old_files)
addTag "result" (show res)
return res
Expand All @@ -654,22 +655,21 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
-- put back to pending que if not listed in the results
-- delete cfp even if we report No cradle target found for the cfp
let remainPendingFiles = Set.delete cfp $ pendingFiles `Set.difference` allNewLoaded
let newLoadedT = pendingFiles `Set.intersection` allNewLoaded
let newLoaded = pendingFiles `Set.intersection` allNewLoaded
atomically $ forM_ remainPendingFiles (writeTQueue pendingFilesTQueue)
-- log new loaded files
logWith recorder Info $ LogSessionNewLoadedFiles $ Set.toList newLoadedT
atomicModifyIORef' cradle_files (\xs -> (newLoadedT <> xs,()))
-- remove the file from error loading files
logWith recorder Info $ LogSessionNewLoadedFiles $ Set.toList newLoaded
-- remove all new loaded file from error loading files
atomicModifyIORef' error_loading_files (\old -> (old `Set.difference` allNewLoaded, ()))
atomicModifyIORef' cradle_files (\xs -> (newLoaded <> xs,()))

Check warning on line 664 in ghcide/session-loader/Development/IDE/Session.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Warning in loadSessionWithOptions in module Development.IDE.Session: Use atomicModifyIORef'_ ▫︎ Found: "atomicModifyIORef' cradle_files (\\ xs -> (newLoaded <> xs, ()))" ▫︎ Perhaps: "atomicModifyIORef'_ cradle_files ((<>) newLoaded)"
return results
| otherwise -> return (([renderPackageSetupException cfp GhcVersionMismatch{..}], Nothing),[])
-- Failure case, either a cradle error or the none cradle
Left err -> do
if (not $ null extraToLoads)

Check warning on line 669 in ghcide/session-loader/Development/IDE/Session.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Suggestion in loadSessionWithOptions in module Development.IDE.Session: Redundant bracket ▫︎ Found: "if (not $ null extraToLoads) then\n do let failedLoadingFiles\n = (Set.insert cfp extraToLoads) `Set.difference` old_files\n atomicModifyIORef'\n error_loading_files (\\ xs -> (failedLoadingFiles <> xs, ()))\n atomically $ forM_ pendingFiles (writeTQueue pendingFilesTQueue)\n consultCradle hieYaml cfp\nelse\n do dep_info <- getDependencyInfo (maybeToList hieYaml)\n let ncfp = toNormalizedFilePath' cfp\n let res\n = (map (\\ err' -> renderCradleError err' cradle ncfp) err, Nothing)\n void\n $ modifyVar' fileToFlags\n $ Map.insertWith\n HM.union hieYaml (HM.singleton ncfp (res, dep_info))\n void $ modifyVar' filesMap $ HM.insert ncfp hieYaml\n atomicModifyIORef'\n error_loading_files (\\ xs -> (Set.insert cfp xs, ()))\n return\n (res, \n maybe [] pure hieYaml ++ concatMap cradleErrorDependencies err)" ▫︎ Perhaps: "if not $ null extraToLoads then\n do let failedLoadingFiles\n = (Set.insert cfp extraToLoads) `Set.difference` old_files\n atomicModifyIORef'\n error_loading_files (\\ xs -> (failedLoadingFiles <> xs, ()))\n atomically $ forM_ pendingFiles (writeTQueue pendingFilesTQueue)\n consultCradle hieYaml cfp\nelse\n do dep_info <- getDependencyInfo (maybeToList hieYaml)\n let ncfp = toNormalizedFilePath' cfp\n let res\n = (map (\\ err' -> renderCradleError err' cradle ncfp) err, Nothing)\n void\n $ modifyVar' fileToFlags\n $ Map.insertWith\n HM.union hieYaml (HM.singleton ncfp (res, dep_info))\n void $ modifyVar' filesMap $ HM.insert ncfp hieYaml\n atomicModifyIORef'\n error_loading_files (\\ xs -> (Set.insert cfp xs, ()))\n return\n (res, \n maybe [] pure hieYaml ++ concatMap cradleErrorDependencies err)"
then do
succLoaded_files <- readIORef cradle_files
-- mark as less loaded files as failedLoadingFiles as possible
let failedLoadingFiles = (Set.insert cfp extraToLoads) `Set.difference` succLoaded_files
let failedLoadingFiles = (Set.insert cfp extraToLoads) `Set.difference` old_files

Check warning on line 672 in ghcide/session-loader/Development/IDE/Session.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Suggestion in loadSessionWithOptions in module Development.IDE.Session: Redundant bracket ▫︎ Found: "(Set.insert cfp extraToLoads) `Set.difference` old_files" ▫︎ Perhaps: "Set.insert cfp extraToLoads `Set.difference` old_files"
atomicModifyIORef' error_loading_files (\xs -> (failedLoadingFiles <> xs,()))

Check warning on line 673 in ghcide/session-loader/Development/IDE/Session.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Warning in loadSessionWithOptions in module Development.IDE.Session: Use atomicModifyIORef'_ ▫︎ Found: "atomicModifyIORef'\n error_loading_files (\\ xs -> (failedLoadingFiles <> xs, ()))" ▫︎ Perhaps: "atomicModifyIORef'_ error_loading_files ((<>) failedLoadingFiles)"
-- retry without other files
atomically $ forM_ pendingFiles (writeTQueue pendingFilesTQueue)
Expand Down

0 comments on commit 67aebc4

Please sign in to comment.