Opened 13 months ago

Last modified 13 months ago

#15513 new feature request

How to pass "-hide-all-packages" to the GHC API?

Reported by: lspitzner Owned by:
Priority: normal Milestone: 8.6.1
Component: Documentation Version: 8.4.3
Keywords: environment file API Cc: dcoutts
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #15541 Differential Rev(s):
Wiki Page:

Description (last modified by lspitzner)

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).

Change History (2)

comment:1 Changed 13 months ago by lspitzner

Description: modified (diff)
  dflags0           <- GHC.getSessionDynFlags
  (dflags1, [], []) <- GHC.parseDynamicFlagsCmdLine dflags0 [GHC.noLoc "-hide-all-packages"]
  GHC.setSessionDynFlags dflags1

must be called before any other invocation of setSessionDynFlags.

I'll leave this open as it is unconfirmed and as a reminder to add this snippet or a better version to the appropriate (API) docs.

comment:2 Changed 13 months ago by bgamari

Cc: dcoutts added
Note: See TracTickets for help on using tickets.