Skip to content

Commit

Permalink
refactor loadSessionWithOptions to improve error handling and clarify…
Browse files Browse the repository at this point in the history
… variable names
  • Loading branch information
soulomoon committed Nov 4, 2024
1 parent 9a2aaf1 commit 6e04d28
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions ghcide/session-loader/Development/IDE/Session.hs
Original file line number Diff line number Diff line change
Expand Up @@ -630,13 +630,13 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
-- remove error files from pending files since error loading need to load one by one
let pendingFiles = pendingFiles' `Set.difference` errorFiles
-- if the file is in error loading files, we fall back to single loading mode
let toLoads = if cfp `Set.member` errorFiles then Set.empty else pendingFiles
let extraToLoads = if cfp `Set.member` errorFiles then Set.empty else pendingFiles

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 $ toLoads <> old_files)
res <- cradleToOptsAndLibDir recorder (sessionLoading clientConfig) cradle cfp (Set.toList $ Set.delete cfp $ extraToLoads <> old_files)
addTag "result" (show res)
return res

Expand All @@ -660,16 +660,16 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
logWith recorder Info $ LogSessionNewLoadedFiles $ Set.toList newLoadedT
atomicModifyIORef' cradle_files (\xs -> (newLoadedT <> xs,()))

Check warning on line 661 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 -> (newLoadedT <> xs, ()))" ▫︎ Perhaps: "atomicModifyIORef'_ cradle_files ((<>) newLoadedT)"
-- remove the file from error loading files
atomicModifyIORef' error_loading_files (\old -> (old `Set.difference` newLoadedT, ()))
atomicModifyIORef' error_loading_files (\old -> (old `Set.difference` allNewLoaded, ()))
return results
| otherwise -> return (([renderPackageSetupException cfp GhcVersionMismatch{..}], Nothing),[])
-- Failure case, either a cradle error or the none cradle
Left err -> do
if (length toLoads > 1)
if (not $ null extraToLoads)

Check warning on line 668 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 succLoaded_files <- readIORef cradle_files\n let failedLoadingFiles\n = (Set.insert cfp extraToLoads) `Set.difference` succLoaded_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 succLoaded_files <- readIORef cradle_files\n let failedLoadingFiles\n = (Set.insert cfp extraToLoads) `Set.difference` succLoaded_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 toLoads) `Set.difference` succLoaded_files
let failedLoadingFiles = (Set.insert cfp extraToLoads) `Set.difference` succLoaded_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` succLoaded_files" ▫︎ Perhaps: "Set.insert cfp extraToLoads `Set.difference` succLoaded_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 All @@ -681,6 +681,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
void $ modifyVar' fileToFlags $
Map.insertWith HM.union hieYaml (HM.singleton ncfp (res, dep_info))
void $ modifyVar' filesMap $ HM.insert ncfp hieYaml
atomicModifyIORef' error_loading_files (\xs -> (Set.insert cfp xs,()))

Check warning on line 684 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 -> (Set.insert cfp xs, ()))" ▫︎ Perhaps: "atomicModifyIORef'_ error_loading_files (Set.insert cfp)"
return (res, maybe [] pure hieYaml ++ concatMap cradleErrorDependencies err)

let
Expand Down

0 comments on commit 6e04d28

Please sign in to comment.