Skip to content

Commit 16719c9

Browse files
committed
Call indexHieFile in newHscEnvEq
1 parent bad0290 commit 16719c9

File tree

4 files changed

+88
-30
lines changed

4 files changed

+88
-30
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -887,34 +887,32 @@ spliceExpressions Splices{..} =
887887
-- TVar to 0 in order to set it up for a fresh indexing session. Otherwise, we
888888
-- can just increment the 'indexCompleted' TVar and exit.
889889
--
890-
indexHieFile :: ShakeExtras -> ModSummary -> NormalizedFilePath -> Util.Fingerprint -> Compat.HieFile -> IO ()
891-
indexHieFile se mod_summary srcPath !hash hf = do
890+
indexHieFile :: ShakeExtras -> NormalizedFilePath -> HieDb.SourceFile -> Util.Fingerprint -> Compat.HieFile -> IO ()
891+
indexHieFile se hiePath sourceFile !hash hf = do
892892
IdeOptions{optProgressStyle} <- getIdeOptionsIO se
893893
atomically $ do
894894
pending <- readTVar indexPending
895-
case HashMap.lookup srcPath pending of
895+
case HashMap.lookup hiePath pending of
896896
Just pendingHash | pendingHash == hash -> pure () -- An index is already scheduled
897897
_ -> do
898898
-- hiedb doesn't use the Haskell src, so we clear it to avoid unnecessarily keeping it around
899899
let !hf' = hf{hie_hs_src = mempty}
900-
modifyTVar' indexPending $ HashMap.insert srcPath hash
900+
modifyTVar' indexPending $ HashMap.insert hiePath hash
901901
writeTQueue indexQueue $ \withHieDb -> do
902902
-- We are now in the worker thread
903903
-- Check if a newer index of this file has been scheduled, and if so skip this one
904904
newerScheduled <- atomically $ do
905905
pending <- readTVar indexPending
906-
pure $ case HashMap.lookup srcPath pending of
906+
pure $ case HashMap.lookup hiePath pending of
907907
Nothing -> False
908908
-- If the hash in the pending list doesn't match the current hash, then skip
909909
Just pendingHash -> pendingHash /= hash
910910
unless newerScheduled $ do
911911
-- Using bracket, so even if an exception happen during withHieDb call,
912912
-- the `post` (which clean the progress indicator) will still be called.
913913
bracket_ (pre optProgressStyle) post $
914-
withHieDb (\db -> HieDb.addRefsFromLoaded db targetPath (HieDb.RealFile $ fromNormalizedFilePath srcPath) hash hf')
914+
withHieDb (\db -> HieDb.addRefsFromLoaded db (fromNormalizedFilePath hiePath) sourceFile hash hf')
915915
where
916-
mod_location = ms_location mod_summary
917-
targetPath = Compat.ml_hie_file mod_location
918916
HieDbWriter{..} = hiedbWriter se
919917

920918
-- Get a progress token to report progress and update it for the current file
@@ -978,15 +976,15 @@ indexHieFile se mod_summary srcPath !hash hf = do
978976
mdone <- atomically $ do
979977
-- Remove current element from pending
980978
pending <- stateTVar indexPending $
981-
dupe . HashMap.update (\pendingHash -> guard (pendingHash /= hash) $> pendingHash) srcPath
979+
dupe . HashMap.update (\pendingHash -> guard (pendingHash /= hash) $> pendingHash) hiePath
982980
modifyTVar' indexCompleted (+1)
983981
-- If we are done, report and reset completed
984982
whenMaybe (HashMap.null pending) $
985983
swapTVar indexCompleted 0
986984
whenJust (lspEnv se) $ \env -> LSP.runLspT env $
987985
when (coerce $ ideTesting se) $
988986
LSP.sendNotification (LSP.SMethod_CustomMethod (Proxy @"ghcide/reference/ready")) $
989-
toJSON $ fromNormalizedFilePath srcPath
987+
toJSON $ fromNormalizedFilePath hiePath
990988
whenJust mdone $ \done ->
991989
modifyVar_ indexProgressToken $ \tok -> do
992990
whenJust (lspEnv se) $ \env -> LSP.runLspT env $
@@ -1007,7 +1005,7 @@ writeAndIndexHieFile hscEnv se mod_summary srcPath exports ast source =
10071005
GHC.mkHieFile' mod_summary exports ast source
10081006
atomicFileWrite se targetPath $ flip GHC.writeHieFile hf
10091007
hash <- Util.getFileHash targetPath
1010-
indexHieFile se mod_summary srcPath hash hf
1008+
indexHieFile se (toNormalizedFilePath' targetPath) (HieDb.RealFile $ fromNormalizedFilePath srcPath) hash hf
10111009
where
10121010
dflags = hsc_dflags hscEnv
10131011
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 ms f hash hf
879+
indexHieFile se (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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ module Development.IDE.GHC.Compat.Units (
2424
-- * UnitInfo
2525
UnitInfo,
2626
unitExposedModules,
27+
unitLibraryDirs,
28+
UnitInfo.unitId,
2729
unitDepends,
2830
unitHaddockInterfaces,
2931
unitInfoId,
@@ -273,6 +275,9 @@ preloadClosureUs = State.preloadClosure . unitState
273275
preloadClosureUs = const ()
274276
#endif
275277

278+
unitLibraryDirs :: UnitInfo -> [FilePath]
279+
unitLibraryDirs = fmap ST.unpack . UnitInfo.unitLibraryDirs
280+
276281
unitExposedModules :: UnitInfo -> [(ModuleName, Maybe Module)]
277282
unitExposedModules ue =
278283
#if MIN_VERSION_ghc(9,0,0)

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

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,23 @@ import Control.DeepSeq (force)
1818
import Control.Exception (evaluate, mask, throwIO)
1919
import Control.Monad.Extra (eitherM, join, mapMaybeM)
2020
import Data.Either (fromRight)
21+
import qualified Data.Map as Map
2122
import Data.Set (Set)
2223
import qualified Data.Set as Set
24+
import qualified Data.Text as T
2325
import Data.Unique (Unique)
2426
import qualified Data.Unique as Unique
25-
import Development.IDE.Core.Shake (ShakeExtras)
27+
import Development.IDE.Core.Compile (indexHieFile, loadHieFile)
28+
import Development.IDE.Core.Shake (ShakeExtras(ideNc, logger), mkUpdater)
2629
import Development.IDE.GHC.Compat
2730
import qualified Development.IDE.GHC.Compat.Util as Maybes
2831
import Development.IDE.GHC.Error (catchSrcErrors)
2932
import Development.IDE.GHC.Util (lookupPackageConfig)
3033
import Development.IDE.Graph.Classes
3134
import Development.IDE.Types.Exports (ExportsMap, createExportsMap)
35+
import Development.IDE.Types.Location (toNormalizedFilePath')
36+
import qualified Development.IDE.Types.Logger as Logger
37+
import HieDb (SourceFile(FakeFile))
3238
import OpenTelemetry.Eventlog (withSpan)
3339
import System.Directory (makeAbsolute)
3440
import System.FilePath
@@ -71,6 +77,10 @@ newHscEnvEq cradlePath se hscEnv0 deps = do
7177

7278
newHscEnvEqWithImportPaths (Just $ Set.fromList importPathsCanon) se hscEnv deps
7379

80+
newtype UnitInfoOrd = UnitInfoOrd UnitInfo deriving Eq
81+
instance Ord UnitInfoOrd where
82+
compare (UnitInfoOrd u1) (UnitInfoOrd u2) = compare (unitId u1) (unitId u2)
83+
7484
newHscEnvEqWithImportPaths :: Maybe (Set FilePath) -> ShakeExtras -> HscEnv -> [(UnitId, DynFlags)] -> IO HscEnvEq
7585
newHscEnvEqWithImportPaths envImportPaths se hscEnv deps = do
7686

@@ -83,25 +93,70 @@ newHscEnvEqWithImportPaths envImportPaths se hscEnv deps = do
8393
-- compute the package imports
8494
let pkgst = unitState hscEnv
8595
depends = explicitUnits pkgst
86-
modules =
87-
[ m
88-
| d <- depends
89-
, Just pkg <- [lookupPackageConfig d hscEnv]
90-
, (modName, maybeOtherPkgMod) <- unitExposedModules pkg
91-
, let m = case maybeOtherPkgMod of
92-
-- When module is re-exported from another package,
93-
-- the origin module is represented by value in Just
94-
Just otherPkgMod -> otherPkgMod
95-
Nothing -> mkModule (unitInfoId pkg) modName
96-
]
97-
98-
doOne m = do
96+
packages = [ pkg
97+
| d <- depends
98+
, Just pkg <- [lookupPackageConfig d hscEnv]
99+
]
100+
modules = Map.fromSet
101+
(\(UnitInfoOrd pkg) ->
102+
[ m
103+
| (modName, maybeOtherPkgMod) <- unitExposedModules pkg
104+
, let m = case maybeOtherPkgMod of
105+
-- When module is re-exported from another package,
106+
-- the origin module is represented by value in Just
107+
Just otherPkgMod -> otherPkgMod
108+
Nothing -> mkModule (unitInfoId pkg) modName
109+
]
110+
)
111+
(Set.fromList $ map UnitInfoOrd packages)
112+
113+
logPackage :: UnitInfo -> IO ()
114+
logPackage pkg = Logger.logDebug (logger se) $ "\n\n\n!!!!!!!!!!!! hscEnvEq :\n"
115+
<> T.pack (concatMap show $ unitLibraryDirs pkg)
116+
<> "\n!!!!!!!!!!!!!!!!!!!!!!\n\n\n"
117+
doOnePackage :: UnitInfoOrd -> [Module] -> IO [ModIface]
118+
doOnePackage (UnitInfoOrd pkg) ms = do
119+
let pkgLibDir :: FilePath
120+
pkgLibDir = case unitLibraryDirs pkg of
121+
[] -> ""
122+
(libraryDir : _) -> libraryDir
123+
hieDir :: FilePath
124+
hieDir = pkgLibDir </> "extra-compliation-artifacts"
125+
logPackage pkg
126+
mapMaybeM (doOne hieDir) ms
127+
128+
doOne :: FilePath -> Module -> IO (Maybe ModIface)
129+
doOne hieDir m = do
130+
let toFilePath :: ModuleName -> FilePath
131+
toFilePath = separateDirectories . prettyModuleName
132+
where
133+
separateDirectories :: FilePath -> FilePath
134+
separateDirectories moduleNameString =
135+
case breakOnDot moduleNameString of
136+
[] -> ""
137+
ms -> foldr1 (</>) ms
138+
breakOnDot :: FilePath -> [FilePath]
139+
breakOnDot = words . map replaceDotWithSpace
140+
replaceDotWithSpace :: Char -> Char
141+
replaceDotWithSpace '.' = ' '
142+
replaceDotWithSpace c = c
143+
prettyModuleName :: ModuleName -> String
144+
prettyModuleName = filter (/= '"')
145+
. concat
146+
. drop 1
147+
. words
148+
. show
149+
hiePath :: FilePath
150+
hiePath = hieDir </> toFilePath (moduleName m) ++ ".hie"
99151
modIface <- initIfaceLoad hscEnv $
100152
loadInterface "" m (ImportByUser NotBoot)
101-
return $ case modIface of
102-
Maybes.Failed _r -> Nothing
103-
Maybes.Succeeded mi -> Just mi
104-
modIfaces <- mapMaybeM doOne modules
153+
case modIface of
154+
Maybes.Failed _r -> return Nothing
155+
Maybes.Succeeded mi -> do
156+
hie <- loadHieFile (mkUpdater $ ideNc se) hiePath
157+
indexHieFile se (toNormalizedFilePath' hiePath) (FakeFile Nothing) (mi_src_hash mi) hie
158+
return $ Just mi
159+
modIfaces <- concat . Map.elems <$> Map.traverseWithKey doOnePackage modules
105160
return $ createExportsMap modIfaces
106161

107162
-- similar to envPackageExports, evaluated lazily

0 commit comments

Comments
 (0)