Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot parse module with only conditional CPP block #1040

Closed
wenkokke opened this issue Jun 5, 2023 · 1 comment · Fixed by #1042
Closed

Cannot parse module with only conditional CPP block #1040

wenkokke opened this issue Jun 5, 2023 · 1 comment · Fixed by #1042
Assignees
Labels
bug Something isn't working

Comments

@wenkokke
Copy link

wenkokke commented Jun 5, 2023

Describe the bug
The GHC parser (in Haddock mode) and by extension Ormolu cannot parse modules that only contain a conditional CPP block. This is possibly an upstream issue with ghc-lib-parser, but I'm filing it here because I'm unsure as GHC compiles the file correctly.

To Reproduce
Write the following to Bug.hs:

{-# LANGUAGE CPP #-}

module Bug where

#ifdef flag
constant :: Int
constant = 1312
#endif

Then run:

ormolu Bug.hs

Ormolu fails with:

Bug.hs:1:1
  The GHC parser (in Haddock mode) failed:
  {ErrorWithoutFlag
   lexical error in pragma at character 'i'}

Expected behavior
Ormolu outputs the file unchanged.

Environment

  • macOS Ventura
  • ormolu 0.7.0.0
  • ghc-lib-parser 9.6.2.20230523
@amesgen amesgen added the bug Something isn't working label Jun 5, 2023
@amesgen
Copy link
Member

amesgen commented Jun 5, 2023

Thanks for the report, this bug was introduced in #994 because we now parse the imports of a module before formatting:

parseImports dynFlags implicitPrelude path rawInputStringBuffer >>= \case

The problem here is that rawInputStringBuffer is not pre-processed in any way, i.e. it still contains all CPP markers. It seems to be able to cope with some CPP in imports at least, e.g. this is formatted just fine, although only the first import (A) is taken into consideration:

{-# LANGUAGE CPP #-}

module Bug where

import A
#ifdef flag
import B
#endif
import C

One straightforward approach to fix this would be to remove all CPP lines before running parseImports; we could both do it such that we remove or keep import B in this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants