Skip to content

Commit 6139522

Browse files
committed
add LogTime to logging for improved time tracking during session loading
1 parent beb1764 commit 6139522

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

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

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,12 @@ data Log
155155
| LogSessionReloadOnError FilePath ![FilePath]
156156
| LogGetOptionsLoop !FilePath
157157
| LogLookupSessionCache !FilePath
158+
| LogTime !String
158159
deriving instance Show Log
159160

160161
instance Pretty Log where
161162
pretty = \case
163+
LogTime s -> "Time:" <+> pretty s
162164
LogLookupSessionCache path -> "Looking up session cache for" <+> pretty path
163165
LogGetOptionsLoop fp -> "Loop: getOptions for" <+> pretty fp
164166
LogSessionReloadOnError path files ->
@@ -582,7 +584,6 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
582584

583585
let flags_map' = HM.fromList (concatMap toFlagsMap all_targets')
584586
all_targets' = concat all_target_details
585-
newLoaded = HM.keys flags_map'
586587
this_dep_info <- getDependencyInfo $ maybeToList hieYaml
587588
let (all_targets, this_flags_map, this_options)
588589
= case HM.lookup _cfp flags_map' of
@@ -599,9 +600,11 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
599600
]
600601

601602
let insertAll m xs = mapM_ (flip (uncurry STM.insert) m) xs
603+
newLoaded = Set.fromList $ fromNormalizedFilePath <$> HM.keys this_flags_map
602604
atomically $ do
603605
STM.insert this_flags_map hieYaml fileToFlags
604606
insertAll filesMap $ map ((hieYaml,) . fst) $ concatMap toFlagsMap all_targets
607+
forM_ newLoaded $ flip S.delete pendingFileSet
605608

606609
-- Typecheck all files in the project on startup
607610
checkProject <- getCheckProject
@@ -621,9 +624,9 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
621624
let !exportsMap' = createExportsMap $ mapMaybe (fmap hirModIface) modIfaces
622625
liftIO $ atomically $ modifyTVar' (exportsMap shakeExtras) (exportsMap' <>)
623626
return [keys1, keys2]
624-
return $ (this_options, Set.fromList $ fromNormalizedFilePath <$> newLoaded)
627+
return $ (this_options, newLoaded)
625628

626-
let consultCradle :: Maybe FilePath -> FilePath -> IO (IdeResult HscEnvEq, DependencyInfo)
629+
let consultCradle :: Maybe FilePath -> FilePath -> IO ()
627630
consultCradle hieYaml cfp = do
628631
let lfpLog = makeRelative rootDir cfp
629632
logWith recorder Info $ LogCradlePath lfpLog
@@ -658,32 +661,36 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
658661
-- The cradle gave us some options so get to work turning them
659662
-- into and HscEnv.
660663
Right (opts, libDir, version) -> do
664+
let ncfp = toNormalizedFilePath' cfp
661665
let compileTime = fullCompilerVersion
662666
case reverse $ readP_to_S parseVersion version of
663667
[] -> error $ "GHC version could not be parsed: " <> version
664668
((runTime, _):_)
665669
| compileTime == runTime -> do
666-
(results, allNewLoaded) <- session (hieYaml, toNormalizedFilePath' cfp, opts, libDir)
667-
-- delete cfp even if we report No cradle target found for the cfp
670+
(_results, allNewLoaded) <- session (hieYaml, ncfp, opts, libDir)
668671
let newLoaded = pendingFiles `Set.intersection` allNewLoaded
669-
-- delete all new loaded
670-
atomically $ forM_ allNewLoaded $ flip S.delete pendingFileSet
671672
-- log new loaded files
672673
logWith recorder Info $ LogSessionNewLoadedFiles $ Set.toList newLoaded
673674
-- remove all new loaded file from error loading files
674675
atomicModifyIORef' error_loading_files (\old -> (old `Set.difference` allNewLoaded, ()))
675676
atomicModifyIORef' cradle_files (\xs -> (newLoaded <> xs,()))
676-
return results
677677
| otherwise -> do
678678
-- delete cfp from pending files
679-
atomically $ S.delete cfp pendingFileSet
680-
return (([renderPackageSetupException cfp GhcVersionMismatch{..}], Nothing),Map.empty)
679+
atomically $ do
680+
STM.focus (Focus.insertOrMerge HM.union
681+
(HM.singleton ncfp (([renderPackageSetupException cfp GhcVersionMismatch{..}], Nothing), mempty)))
682+
hieYaml fileToFlags
683+
STM.insert hieYaml ncfp filesMap
684+
S.delete cfp pendingFileSet
681685
-- Failure case, either a cradle error or the none cradle
682686
Left err -> do
683687
let attemptToLoadFiles = (Set.delete cfp $ Set.fromList $ concatMap cradleErrorLoadingFiles err)
684688
`Set.difference` old_files
685689
if (not $ null attemptToLoadFiles)
690+
686691
then do
692+
-- we are loading more files and failed, we need to retry
693+
687694
-- mark as less loaded files as failedLoadingFiles as possible
688695
-- limitation is that when we are loading files, and the dependencies of old_files
689696
-- are changed, and old_files are not valid anymore.
@@ -695,16 +702,16 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
695702
logWith recorder Info $ LogSessionReloadOnError cfp (Set.toList attemptToLoadFiles)
696703
consultCradle hieYaml cfp
697704
else do
705+
-- we are only loading this file and it failed
698706
dep_info <- getDependencyInfo ((maybeToList hieYaml) ++ concatMap cradleErrorDependencies err)
699707
let ncfp = toNormalizedFilePath' cfp
700708
let res = (map (\err' -> renderCradleError err' cradle ncfp) err, Nothing)
701709
-- remove cfp from pending files
702-
atomically $ S.delete cfp pendingFileSet
710+
atomicModifyIORef' error_loading_files (\xs -> (Set.insert cfp xs,()))
703711
atomically $ do
704712
STM.focus (Focus.insertOrMerge HM.union (HM.singleton ncfp (res, dep_info))) hieYaml fileToFlags
705-
STM.insert hieYaml ncfp filesMap
706-
atomicModifyIORef' error_loading_files (\xs -> (Set.insert cfp xs,()))
707-
return (res, dep_info)
713+
STM.insert hieYaml ncfp filesMap
714+
S.delete cfp pendingFileSet
708715

709716
let
710717
-- | We allow users to specify a loading strategy.
@@ -727,7 +734,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
727734
-- This caches the mapping from hie.yaml + Mod.hs -> [String]
728735
-- Returns the Ghc session and the cradle dependencies
729736
let sessionOpts :: (Maybe FilePath, FilePath)
730-
-> IO (IdeResult HscEnvEq, DependencyInfo)
737+
-> IO ()
731738
sessionOpts (hieYaml, file) = do
732739
Extra.whenM didSessionLoadingPreferenceConfigChange $ do
733740
logWith recorder Info LogSessionLoadingChanged
@@ -744,10 +751,9 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
744751

745752
v <- atomically $ fromMaybe HM.empty <$> STM.lookup hieYaml fileToFlags
746753
case HM.lookup (toNormalizedFilePath' file) v of
747-
Just (opts, old_di) -> do
754+
Just (_opts, old_di) -> do
748755
deps_ok <- checkDependencyInfo old_di
749-
if not deps_ok
750-
then do
756+
when (not deps_ok) $ do
751757
-- if deps are old, we can try to load the error files again
752758
atomicModifyIORef' error_loading_files (\xs -> (Set.delete file xs,()))
753759
atomicModifyIORef' cradle_files (\xs -> (Set.delete file xs,()))
@@ -759,7 +765,6 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
759765
-- Keep the same name cache
760766
modifyVar_ hscEnvs (return . Map.adjust (const []) hieYaml )
761767
consultCradle hieYaml file
762-
else return (opts, old_di)
763768
Nothing -> consultCradle hieYaml file
764769

765770
let checkInCache ::NormalizedFilePath -> STM (Maybe (IdeResult HscEnvEq, DependencyInfo))
@@ -772,24 +777,27 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
772777
-- at a time. Therefore the IORef contains the currently running cradle, if we try
773778
-- to get some more options then we wait for the currently running action to finish
774779
-- before attempting to do so.
775-
let getOptions :: FilePath -> IO (IdeResult HscEnvEq, DependencyInfo)
780+
let getOptions :: FilePath -> IO ()
776781
getOptions file = do
777782
let ncfp = toNormalizedFilePath' file
778783
cachedHieYamlLocation <- atomically $ STM.lookup ncfp filesMap
779784
hieYaml <- cradleLoc file
780785
let hieLoc = join cachedHieYamlLocation <|> hieYaml
781-
result <- sessionOpts (hieLoc, file) `Safe.catch` \e -> do
786+
sessionOpts (hieLoc, file) `Safe.catch` \e -> do
782787
dep <- getDependencyInfo $ maybe [] pure hieYaml
783-
return (([renderPackageSetupException file e], Nothing), dep)
784-
atomically $ STM.focus (Focus.insertOrMerge HM.union (HM.singleton ncfp result)) hieLoc fileToFlags
785-
return result
788+
let errorResult = (([renderPackageSetupException file e], Nothing), dep)
789+
atomically $ do
790+
STM.focus (Focus.insertOrMerge HM.union (HM.singleton ncfp errorResult)) hieLoc fileToFlags
791+
STM.insert hieYaml ncfp filesMap
792+
-- delete file from pending files
793+
S.delete file pendingFileSet
786794

787795
let getOptionsLoop :: IO ()
788796
getOptionsLoop = do
789797
-- Get the next file to load
790798
absFile <- atomically $ S.readQueue pendingFileSet
791799
logWith recorder Info (LogGetOptionsLoop absFile)
792-
void $ getOptions absFile
800+
getOptions absFile
793801
getOptionsLoop
794802

795803
-- | Given a file, this function will return the HscEnv and the dependencies

0 commit comments

Comments
 (0)