How to pass "-hide-all-packages" to the GHC API?
In brittany, we have make use of the GHC API e.g. in the following fashion:
parseModuleFromString
:: [String]
-> System.IO.FilePath
-> (GHC.DynFlags -> IO (Either String a))
-> String
-> IO (Either String (ExactPrint.Anns, GHC.ParsedSource, a))
parseModuleFromString args fp dynCheck str =
mask_ $ ExactPrint.ghcWrapper $ ExceptT.runExceptT $ do
dflags0 <- lift $ ExactPrint.initDynFlagsPure fp str
(dflags1, leftover, warnings) <- lift
$ GHC.parseDynamicFlagsCmdLine dflags0 (GHC.noLoc <$> args)
when (not $ null leftover)
$ ExceptT.throwE
$ "when parsing ghc flags: leftover flags: "
++ show (leftover <&> \(L _ s) -> s)
when (not $ null warnings)
$ ExceptT.throwE
$ "when parsing ghc flags: encountered warnings: "
++ show (warnings <&> warnExtractorCompat)
dynCheckRes <- ExceptT.ExceptT $ liftIO $ dynCheck dflags1
let res = ExactPrint.parseModuleFromStringInternal dflags1 fp str
case res of
Left (span, err) -> ExceptT.throwE $ show span ++ ": " ++ err
Right (a , m ) -> pure (a, m, dynCheckRes)
This code unfortunately picks up "package environment files", which I take it is not at all necessary for this use-case: Brittany only requires parsing functionality, so external packages should be irrelevant. "Unfortunately", because the package environment files can easily become stale, which in turn breaks the API.
The users' guide mentions the "-hide-all-packages" flag, but my attempts to pass this to the API so far have failed. What I have tried is calling parseDynamicFlagsCmdLine
with "-hide-all-packages" as an argument, and using the resulting dynflags afterwards. This appears to be without effect, ~~I think even when it is the first thing executed inside of runGhc
~~ (not entirely correct, see below).