Skip to content

Commit 096c52b

Browse files
committed
Check if dependency HIE files already indexed
1 parent 0390106 commit 096c52b

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

ghcide/src/Development/IDE/Core/Compile.hs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Development.IDE.Core.Compile
2121
, generateByteCode
2222
, generateHieAsts
2323
, writeAndIndexHieFile
24+
, HieDbModuleQuery(..)
2425
, indexHieFile
2526
, writeHiFile
2627
, getModSummaryFromImports
@@ -859,6 +860,10 @@ spliceExpressions Splices{..} =
859860
, DL.fromList $ map fst awSplices
860861
]
861862

863+
data HieDbModuleQuery
864+
= HieDbModuleQuery ModuleName Unit
865+
| DontCheckForModule
866+
862867
-- | In addition to indexing the `.hie` file, this function is responsible for
863868
-- maintaining the 'IndexQueue' state and notifying the user about indexing
864869
-- progress.
@@ -887,16 +892,14 @@ spliceExpressions Splices{..} =
887892
-- TVar to 0 in order to set it up for a fresh indexing session. Otherwise, we
888893
-- can just increment the 'indexCompleted' TVar and exit.
889894
--
890-
indexHieFile :: ShakeExtras -> NormalizedFilePath -> HieDb.SourceFile -> Util.Fingerprint -> Compat.HieFile -> IO ()
891-
indexHieFile se hiePath sourceFile !hash hf = do
895+
indexHieFile :: ShakeExtras -> HieDbModuleQuery -> NormalizedFilePath -> HieDb.SourceFile -> Util.Fingerprint -> Compat.HieFile -> IO ()
896+
indexHieFile se query hiePath sourceFile !hash hf = do
892897
IdeOptions{optProgressStyle} <- getIdeOptionsIO se
893898
atomically $ do
894899
pending <- readTVar indexPending
895900
case HashMap.lookup hiePath pending of
896901
Just pendingHash | pendingHash == hash -> pure () -- An index is already scheduled
897902
_ -> do
898-
-- hiedb doesn't use the Haskell src, so we clear it to avoid unnecessarily keeping it around
899-
let !hf' = hf{hie_hs_src = mempty}
900903
modifyTVar' indexPending $ HashMap.insert hiePath hash
901904
writeTQueue indexQueue $ \withHieDb -> do
902905
-- We are now in the worker thread
@@ -911,8 +914,24 @@ indexHieFile se hiePath sourceFile !hash hf = do
911914
-- Using bracket, so even if an exception happen during withHieDb call,
912915
-- the `post` (which clean the progress indicator) will still be called.
913916
bracket_ (pre optProgressStyle) post $
914-
withHieDb (\db -> HieDb.addRefsFromLoaded db (fromNormalizedFilePath hiePath) sourceFile hash hf')
917+
withHieDb indexIfNotAlready
915918
where
919+
-- hiedb doesn't use the Haskell src, so we clear it to avoid unnecessarily keeping it around
920+
hf' :: Compat.HieFile
921+
!hf' = hf{hie_hs_src = mempty}
922+
indexIfNotAlready :: HieDb -> IO ()
923+
indexIfNotAlready db = case query of
924+
DontCheckForModule -> doIndexing
925+
HieDbModuleQuery moduleName unit -> do
926+
mRow <- HieDb.lookupHieFile db moduleName unit
927+
case mRow of
928+
Nothing -> doIndexing
929+
Just _row -> return ()
930+
where
931+
doIndexing :: IO ()
932+
doIndexing =
933+
HieDb.addRefsFromLoaded db (fromNormalizedFilePath hiePath) sourceFile hash hf'
934+
916935
HieDbWriter{..} = hiedbWriter se
917936

918937
-- Get a progress token to report progress and update it for the current file
@@ -1005,7 +1024,7 @@ writeAndIndexHieFile hscEnv se mod_summary srcPath exports ast source =
10051024
GHC.mkHieFile' mod_summary exports ast source
10061025
atomicFileWrite se targetPath $ flip GHC.writeHieFile hf
10071026
hash <- Util.getFileHash targetPath
1008-
indexHieFile se (toNormalizedFilePath' targetPath) (HieDb.RealFile $ fromNormalizedFilePath srcPath) hash hf
1027+
indexHieFile se DontCheckForModule (toNormalizedFilePath' targetPath) (HieDb.RealFile $ fromNormalizedFilePath srcPath) hash hf
10091028
where
10101029
dflags = hsc_dflags hscEnv
10111030
mod_location = ms_location mod_summary

ghcide/src/Development/IDE/Core/Rules.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ getModIfaceFromDiskAndIndexRule recorder =
876876
-- can just re-index the file we read from disk
877877
Right hf -> liftIO $ do
878878
logWith recorder Logger.Debug $ LogReindexingHieFile f
879-
indexHieFile se (toNormalizedFilePath' hie_loc) (HieDb.RealFile $ fromNormalizedFilePath f) hash hf
879+
indexHieFile se DontCheckForModule (toNormalizedFilePath' hie_loc) (HieDb.RealFile $ fromNormalizedFilePath f) hash hf
880880

881881
return (Just x)
882882

ghcide/src/Development/IDE/GHC/Compat/Units.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Development.IDE.GHC.Compat.Units (
2727
unitHiddenModules,
2828
unitLibraryDirs,
2929
UnitInfo.unitId,
30+
UnitInfo.mkUnit,
3031
unitDepends,
3132
unitHaddockInterfaces,
3233
unitInfoId,
@@ -125,7 +126,6 @@ type PreloadUnitClosure = ()
125126
type Unit = UnitId
126127
#endif
127128

128-
129129
#if !MIN_VERSION_ghc(9,0,0)
130130
unitString :: Unit -> String
131131
unitString = Module.unitIdString

ghcide/src/Development/IDE/Types/HscEnvEq.hs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import qualified Data.Set as Set
2626
import qualified Data.Text as T
2727
import Data.Unique (Unique)
2828
import qualified Data.Unique as Unique
29-
import Development.IDE.Core.Compile (indexHieFile, loadHieFile)
29+
import Development.IDE.Core.Compile (HieDbModuleQuery(HieDbModuleQuery), indexHieFile, loadHieFile)
3030
import Development.IDE.Core.Shake (ShakeExtras(ideNc, logger), mkUpdater)
3131
import Development.IDE.GHC.Compat
3232
import qualified Development.IDE.GHC.Compat.Util as Maybes
@@ -127,19 +127,25 @@ newHscEnvEqWithImportPaths envImportPaths se hscEnv deps = do
127127
(libraryDir : _) -> libraryDir
128128
hieDir :: FilePath
129129
hieDir = pkgLibDir </> "extra-compilation-artifacts"
130+
packageUnit :: Unit
131+
packageUnit = mkUnit package
130132
modIfaces <- mapMaybeM loadModIface modules
131-
traverse_ (indexModuleHieFile hieDir) modIfaces
132-
indexModuleHieFile :: FilePath -> ModIface -> IO ()
133-
indexModuleHieFile hieDir modIface = do
134-
let hiePath :: FilePath
135-
hiePath = hieDir </> toFilePath (moduleName $ mi_module modIface) ++ ".hie"
133+
traverse_ (indexModuleHieFile hieDir packageUnit) modIfaces
134+
indexModuleHieFile :: FilePath -> Unit -> ModIface -> IO ()
135+
indexModuleHieFile hieDir packageUnit modIface = do
136+
let modName :: ModuleName
137+
modName = moduleName $ mi_module modIface
138+
hiePath :: FilePath
139+
hiePath = hieDir </> toFilePath modName ++ ".hie"
140+
query :: HieDbModuleQuery
141+
query = HieDbModuleQuery modName packageUnit
136142
hieResults <- tryAny $ loadHieFile (mkUpdater $ ideNc se) hiePath
137143
case hieResults of
138144
Left e -> Logger.logDebug (logger se) $
139145
"Failed to index dependency HIE file:\n"
140146
<> T.pack (show e)
141147
Right hie ->
142-
indexHieFile se (toNormalizedFilePath' hiePath) (FakeFile Nothing) (mi_src_hash modIface) hie
148+
indexHieFile se query (toNormalizedFilePath' hiePath) (FakeFile Nothing) (mi_src_hash modIface) hie
143149
toFilePath :: ModuleName -> FilePath
144150
toFilePath = separateDirectories . prettyModuleName
145151
where

0 commit comments

Comments
 (0)