From b7106519a72db548fc8c843351a3679d477ad01f Mon Sep 17 00:00:00 2001 From: Zubin Duggal Date: Tue, 28 Jun 2022 14:01:30 +0530 Subject: [PATCH] Don't use typecheck rule for non FOIs in refine imports plugin Also add an assertion to check that we never use non-FOI rules Fixes #2962 --- ghcide/src/Development/IDE/Core/Rules.hs | 17 ++++++++++++++++- .../src/Ide/Plugin/RefineImports.hs | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/ghcide/src/Development/IDE/Core/Rules.hs b/ghcide/src/Development/IDE/Core/Rules.hs index b02a58dd9f7..9e9c4a4af69 100644 --- a/ghcide/src/Development/IDE/Core/Rules.hs +++ b/ghcide/src/Development/IDE/Core/Rules.hs @@ -165,6 +165,7 @@ data Log | LogLoadingHieFileFail !FilePath !SomeException | LogLoadingHieFileSuccess !FilePath | LogExactPrint ExactPrint.Log + | LogTypecheckedFOI !NormalizedFilePath deriving Show instance Pretty Log where @@ -182,6 +183,14 @@ instance Pretty Log where LogLoadingHieFileSuccess path -> "SUCCEEDED LOADING HIE FILE FOR" <+> pretty path LogExactPrint log -> pretty log + LogTypecheckedFOI path -> vcat + [ "WARNING: Typechecked a file which is not currently open in the editor:" <+> pretty path + , "This can indicate a bug which results in excessive memory usage." + , "This may be a spurious warning if you have recently closed the file." + , "If you haven't opened this file recently, please file a report on the issue tracker mentioning" + <+> "the HLS version being used, the plugins enabled, and if possible the codebase and file which" + <+> "triggered this warning." + ] templateHaskellInstructions :: T.Text templateHaskellInstructions = "https://haskell-language-server.readthedocs.io/en/latest/troubleshooting.html#static-binaries" @@ -650,7 +659,13 @@ typeCheckRule :: Recorder (WithPriority Log) -> Rules () typeCheckRule recorder = define (cmapWithPrio LogShake recorder) $ \TypeCheck file -> do pm <- use_ GetParsedModule file hsc <- hscEnv <$> use_ GhcSessionDeps file - typeCheckRuleDefinition hsc pm + foi <- use_ IsFileOfInterest file + -- We should only call the typecheck rule for files of interest. + -- Keeping typechecked modules in memory for other files is + -- very expensive. + case foi of + NotFOI -> logWith recorder Logger.Info $ LogTypecheckedFOI f + IsFOI {} -> typeCheckRuleDefinition hsc pm knownFilesRule :: Recorder (WithPriority Log) -> Rules () knownFilesRule recorder = defineEarlyCutOffNoFile (cmapWithPrio LogShake recorder) $ \GetKnownTargets -> do diff --git a/plugins/hls-refine-imports-plugin/src/Ide/Plugin/RefineImports.hs b/plugins/hls-refine-imports-plugin/src/Ide/Plugin/RefineImports.hs index 582fba0a72f..d4a80764848 100644 --- a/plugins/hls-refine-imports-plugin/src/Ide/Plugin/RefineImports.hs +++ b/plugins/hls-refine-imports-plugin/src/Ide/Plugin/RefineImports.hs @@ -187,8 +187,8 @@ refineImportsRule recorder = define (cmapWithPrio LogShake recorder) $ \RefineIm -- second layer is from the imports of first layer to their imports ImportMap importIm <- use_ GetImportMap path forM importIm $ \imp_path -> do - imp_tmr <- use_ TypeCheck imp_path - return $ tcg_exports $ tmrTypechecked imp_tmr + imp_hir <- use_ GetModIface imp_path + return $ mi_exports $ hirModIface imp_hir -- Use the GHC api to extract the "minimal" imports -- We shouldn't blindly refine imports