[GHC] #15513: How to pass "-hide-all-packages" to the GHC API?

GHC ghc-devs at haskell.org
Mon Aug 20 17:48:35 UTC 2018


#15513: How to pass "-hide-all-packages" to the GHC API?
-------------------------------------+-------------------------------------
        Reporter:  lspitzner         |                Owner:  (none)
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:  8.6.1
       Component:  Documentation     |              Version:  8.4.3
      Resolution:                    |             Keywords:  environment
                                     |  file API
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:                    |
-------------------------------------+-------------------------------------
Changes (by lspitzner):

 * related:   => #15541


Old description:

> In brittany, we have make use of the GHC API e.g. in the following
> fashion:
>
> {{{#!hs
> 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`.

New description:

 In brittany, we have make use of the GHC API e.g. in the following
 fashion:

 {{{#!hs
 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).

--

Comment:

 {{{#!hs
   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.

-- 
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/15513#comment:1>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list