Skip to content

Commit 7d416f0

Browse files
authored
Customize the unitId used for the fake internal component (#1435)
* Customize the unitId used for the internal component * add comment * change default fake uid to "main" * move config setting to session loading options (where it really belongs) * hiedb versioning * (unrelated) fix some progress messages
1 parent 2792fcc commit 7d416f0

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ import HieDb.Types
9090
import HieDb.Utils
9191
import Maybes (MaybeT (runMaybeT))
9292

93+
-- | Bump this version number when making changes to the format of the data stored in hiedb
94+
hiedbDataVersion :: String
95+
hiedbDataVersion = "1"
9396

9497
data CacheDirs = CacheDirs
9598
{ hiCacheDir, hieCacheDir, oCacheDir :: Maybe FilePath}
@@ -103,6 +106,11 @@ data SessionLoadingOptions = SessionLoadingOptions
103106
, getCacheDirs :: String -> [String] -> IO CacheDirs
104107
-- | Return the GHC lib dir to use for the 'unsafeGlobalDynFlags'
105108
, getInitialGhcLibDir :: IO (Maybe LibDir)
109+
, fakeUid :: InstalledUnitId
110+
-- ^ unit id used to tag the internal component built by ghcide
111+
-- To reuse external interface files the unit ids must match,
112+
-- thus make sure to build them with `--this-unit-id` set to the
113+
-- same value as the ghcide fake uid
106114
}
107115

108116
instance Default SessionLoadingOptions where
@@ -111,6 +119,7 @@ instance Default SessionLoadingOptions where
111119
,loadCradle = HieBios.loadCradle
112120
,getCacheDirs = getCacheDirsDefault
113121
,getInitialGhcLibDir = getInitialGhcLibDirDefault
122+
,fakeUid = toInstalledUnitId (stringToUnitId "main")
114123
}
115124

116125
getInitialGhcLibDirDefault :: IO (Maybe LibDir)
@@ -167,7 +176,7 @@ runWithDb fp k = do
167176

168177
getHieDbLoc :: FilePath -> IO FilePath
169178
getHieDbLoc dir = do
170-
let db = dirHash++"-"++takeBaseName dir++"-"++VERSION_ghc <.> "hiedb"
179+
let db = intercalate "-" [dirHash, takeBaseName dir, VERSION_ghc, hiedbDataVersion] <.> "hiedb"
171180
dirHash = B.unpack $ B16.encode $ H.hash $ B.pack dir
172181
cDir <- IO.getXdgDirectory IO.XdgCache cacheDir
173182
createDirectoryIfMissing True cDir
@@ -277,7 +286,7 @@ loadSessionWithOptions SessionLoadingOptions{..} dir = do
277286
new_deps' <- forM new_deps $ \RawComponentInfo{..} -> do
278287
-- Remove all inplace dependencies from package flags for
279288
-- components in this HscEnv
280-
let (df2, uids) = removeInplacePackages inplace rawComponentDynFlags
289+
let (df2, uids) = removeInplacePackages fakeUid inplace rawComponentDynFlags
281290
let prefix = show rawComponentUnitId
282291
-- See Note [Avoiding bad interface files]
283292
let hscComponents = sort $ map show uids
@@ -716,12 +725,15 @@ getDependencyInfo fs = Map.fromList <$> mapM do_one fs
716725
-- There are several places in GHC (for example the call to hptInstances in
717726
-- tcRnImports) which assume that all modules in the HPT have the same unit
718727
-- ID. Therefore we create a fake one and give them all the same unit id.
719-
removeInplacePackages :: [InstalledUnitId] -> DynFlags -> (DynFlags, [InstalledUnitId])
720-
removeInplacePackages us df = (df { packageFlags = ps
728+
removeInplacePackages
729+
:: InstalledUnitId -- ^ fake uid to use for our internal component
730+
-> [InstalledUnitId]
731+
-> DynFlags
732+
-> (DynFlags, [InstalledUnitId])
733+
removeInplacePackages fake_uid us df = (df { packageFlags = ps
721734
, thisInstalledUnitId = fake_uid }, uids)
722735
where
723736
(uids, ps) = partitionEithers (map go (packageFlags df))
724-
fake_uid = toInstalledUnitId (stringToUnitId "fake_uid")
725737
go p@(ExposePackage _ (UnitIdArg u) _) = if toInstalledUnitId u `elem` us
726738
then Left (toInstalledUnitId u)
727739
else Right p

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ indexHieFile se mod_summary srcPath hash hf = atomically $ do
571571
LSP.sendNotification LSP.SProgress $ LSP.ProgressParams tok $
572572
LSP.Report $ LSP.WorkDoneProgressReportParams
573573
{ _cancellable = Nothing
574-
, _message = Just $ T.pack (show srcPath) <> progress
574+
, _message = Just $ T.pack (fromNormalizedFilePath srcPath) <> progress
575575
, _percentage = Nothing
576576
}
577577

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ getModIfaceFromDiskAndIndexRule = defineEarlyCutoff $ \GetModIfaceFromDiskAndInd
842842
Left err -> fail $ "failed to read .hie file " ++ show hie_loc ++ ": " ++ displayException err
843843
-- can just re-index the file we read from disk
844844
Right hf -> liftIO $ do
845-
L.logDebug (logger se) $ "Re-indexing hie file for" <> T.pack (show f)
845+
L.logDebug (logger se) $ "Re-indexing hie file for" <> T.pack (fromNormalizedFilePath f)
846846
indexHieFile se ms f hash hf
847847

848848
let fp = hiFileFingerPrint x

0 commit comments

Comments
 (0)