Skip to content

Customize the unitId used for the fake internal component #1435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions ghcide/session-loader/Development/IDE/Session.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ import HieDb.Types
import HieDb.Utils
import Maybes (MaybeT (runMaybeT))

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

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

instance Default SessionLoadingOptions where
Expand All @@ -111,6 +119,7 @@ instance Default SessionLoadingOptions where
,loadCradle = HieBios.loadCradle
,getCacheDirs = getCacheDirsDefault
,getInitialGhcLibDir = getInitialGhcLibDirDefault
,fakeUid = toInstalledUnitId (stringToUnitId "main")
}

getInitialGhcLibDirDefault :: IO (Maybe LibDir)
Expand Down Expand Up @@ -167,7 +176,7 @@ runWithDb fp k = do

getHieDbLoc :: FilePath -> IO FilePath
getHieDbLoc dir = do
let db = dirHash++"-"++takeBaseName dir++"-"++VERSION_ghc <.> "hiedb"
let db = intercalate "-" [dirHash, takeBaseName dir, VERSION_ghc, hiedbDataVersion] <.> "hiedb"
dirHash = B.unpack $ B16.encode $ H.hash $ B.pack dir
cDir <- IO.getXdgDirectory IO.XdgCache cacheDir
createDirectoryIfMissing True cDir
Expand Down Expand Up @@ -277,7 +286,7 @@ loadSessionWithOptions SessionLoadingOptions{..} dir = do
new_deps' <- forM new_deps $ \RawComponentInfo{..} -> do
-- Remove all inplace dependencies from package flags for
-- components in this HscEnv
let (df2, uids) = removeInplacePackages inplace rawComponentDynFlags
let (df2, uids) = removeInplacePackages fakeUid inplace rawComponentDynFlags
let prefix = show rawComponentUnitId
-- See Note [Avoiding bad interface files]
let hscComponents = sort $ map show uids
Expand Down Expand Up @@ -716,12 +725,15 @@ getDependencyInfo fs = Map.fromList <$> mapM do_one fs
-- There are several places in GHC (for example the call to hptInstances in
-- tcRnImports) which assume that all modules in the HPT have the same unit
-- ID. Therefore we create a fake one and give them all the same unit id.
removeInplacePackages :: [InstalledUnitId] -> DynFlags -> (DynFlags, [InstalledUnitId])
removeInplacePackages us df = (df { packageFlags = ps
removeInplacePackages
:: InstalledUnitId -- ^ fake uid to use for our internal component
-> [InstalledUnitId]
-> DynFlags
-> (DynFlags, [InstalledUnitId])
removeInplacePackages fake_uid us df = (df { packageFlags = ps
, thisInstalledUnitId = fake_uid }, uids)
where
(uids, ps) = partitionEithers (map go (packageFlags df))
fake_uid = toInstalledUnitId (stringToUnitId "fake_uid")
go p@(ExposePackage _ (UnitIdArg u) _) = if toInstalledUnitId u `elem` us
then Left (toInstalledUnitId u)
else Right p
Expand Down
2 changes: 1 addition & 1 deletion ghcide/src/Development/IDE/Core/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ indexHieFile se mod_summary srcPath hash hf = atomically $ do
LSP.sendNotification LSP.SProgress $ LSP.ProgressParams tok $
LSP.Report $ LSP.WorkDoneProgressReportParams
{ _cancellable = Nothing
, _message = Just $ T.pack (show srcPath) <> progress
, _message = Just $ T.pack (fromNormalizedFilePath srcPath) <> progress
, _percentage = Nothing
}

Expand Down
2 changes: 1 addition & 1 deletion ghcide/src/Development/IDE/Core/Rules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ getModIfaceFromDiskAndIndexRule = defineEarlyCutoff $ \GetModIfaceFromDiskAndInd
Left err -> fail $ "failed to read .hie file " ++ show hie_loc ++ ": " ++ displayException err
-- can just re-index the file we read from disk
Right hf -> liftIO $ do
L.logDebug (logger se) $ "Re-indexing hie file for" <> T.pack (show f)
L.logDebug (logger se) $ "Re-indexing hie file for" <> T.pack (fromNormalizedFilePath f)
indexHieFile se ms f hash hf

let fp = hiFileFingerPrint x
Expand Down