PUBLIC
Hi,
I have the following to .hs files:
I would like to parse/typecheck/load MyLib.hs into some Unit “my-unit”, then add that to the package “my-pkg”, and then typecheck Test.hs, all in-proc using the GHC API, without
putting any other files on disk. How do I do that?
What I tried is loading MyLib.hs after setting the homeUnitId in the DynFlags to “my-unit”, then editing the packageNameMap in the unitState of the DynFlags to may “my-pkg” to
“my-unit”:
setHomeUnit :: (GhcMonad m) => UnitId -> m ()
setHomeUnit unitId = do
dflags <- getSessionDynFlags
modifySession $ \h -> h{ hsc_dflags = dflags{ homeUnitId = unitId } }
registerUnit :: (GhcMonad m) => PackageName -> UnitId -> m ()
registerUnit pkg unitId = modifySession $ \h -> h{ hsc_dflags = addUnit $ hsc_dflags h }
where
addUnit dflags = dflags
{ unitState = let us = unitState dflags in us
{ packageNameMap = M.insert pkg (Indefinite unitId Nothing) $ packageNameMap us
}
}
pipeline = do
setHomeUnit myUnit
loadModule =<< typecheckModule =<< parseModule =<< modSumarryFor “MyLib”
registerUnit myPkg myUnit
setHomeUnit mainUnitId
typecheckModule =<< parseModule =<< modSumarryFor “Test”
Alas, this doesn’t work: the import of `MyLib` from `my-pkg` fails with:
input/linking/Test.hs:5:1: error:
Could not find module ‘MyLib’
It is not a module in the current program, or in any known package.
TBH I’m not very surprised that it didn’t work – that registerUnit function is doing some pretty deep surgery on the unitState that probably breaks several invariants. Still,
I wasn’t able to find a better way – all the functions in GHC.Unit.State seem to be for querying only.
Thanks,
Gergo