tweag / ormolu

A formatter for Haskell source code
https://ormolu-live.tweag.io
Other
958 stars 83 forks source link

--ghc-opt arguments ignored #437

Closed amesgen closed 4 years ago

amesgen commented 4 years ago

GHC options passed via --ghc-opt have been ignored since #423.

I think this code has not been ported.

Changing parseModule like this seems to work (but invalid GHC options are not reported - supposedly it has to be wrapped with catchErrors from parsePragmasIntoDynFlags?):

parseModule Config {..} path input' = liftIO $ do
  let (input, extraComments) = stripLinePragmas path input'
  -- NOTE It's important that 'setDefaultExts' is done before
  -- 'parsePragmasIntoDynFlags', because otherwise we might enable an
  -- extension that was explicitly disabled in the file.
  let baseFlags =
        GHC.setGeneralFlag'
          GHC.Opt_Haddock
          (setDefaultExts baseDynFlags)
  (warnings', dynFlags') <-
    parsePragmasIntoDynFlags baseFlags path input' >>= \case
      Right res -> pure res
      Left err ->
        let loc =
              mkSrcSpan
                (mkSrcLoc (GHC.mkFastString path) 1 1)
                (mkSrcLoc (GHC.mkFastString path) 1 1)
         in throwIO (OrmoluParsingFailed loc err)
  (dynFlags, _, warnings'') <-
    parseDynamicFilePragma dynFlags' (dynOptionToLocatedStr <$> cfgDynOptions)
  let warnings = warnings' <> warnings''
  when (GHC.xopt Cpp dynFlags && not cfgTolerateCpp) $
    throwIO (OrmoluCppEnabled path)
  let r = case runParser GHC.parseModule dynFlags path input of
        GHC.PFailed _ ss m ->
          Left (ss, GHC.showSDoc dynFlags m)
        GHC.POk pstate pmod ->
          let (comments, exts) = mkCommentStream extraComments pstate
           in Right ParseResult
                { prParsedSource = pmod,
                  prAnns = mkAnns pstate,
                  prCommentStream = comments,
                  prExtensions = exts
                }
  return (warnings, r)
mrkkrp commented 4 years ago

Ping @neongreen.

mrkkrp commented 4 years ago

I'm fixing it now.

mrkkrp commented 4 years ago

To be clear: Ormolu did not complain about unrecognized GHC options before either. What happens here, I think, is that those options end up in the "leftover list", second element of 3-tuple returned by parseDynamicFilePragma.

amesgen commented 4 years ago

Ah makes sense, I did not check the previous behavior. Whether a non empty leftover list should be reported might be worth considering (but I don't think it is of great importance).

mrkkrp commented 4 years ago

I'm going to make unrecognized options cause Ormolu to fail. It's no good swallowing them, we only introduce a possibility for subtle bugs and hard-to-debug problems this way. If an option doesn't make sense, it should be reported.