tweag / ormolu

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

`ORMOLU_DISABLE` inside multi-line comment trips up parser #952

Closed JaSpa closed 1 year ago

JaSpa commented 1 year ago

Describe the bug The magic {- ORMOLU_DISABLE -} comment inside a multi-line comment trips up the parser. This came up when I wrapped a larger chunk of code inside a comment and formatting stopped working.

To Reproduce Trying to format

{-
{- ORMOLU_DISABLE -}
-}

results in

The GHC parser failed:

<input>:(1,1)-(2,0)`

unterminated `{-' at end of input

Expected behavior Unchanged output; changing the second line to {- ORMOLU_DISAB -} does not lead to a parse failure.

Environment Tested with the current version of Ormolu Live: Version 0.5.1.0, commit 0bcf31c, using ghc-lib-parser 9.4.2.20220822

amesgen commented 1 year ago

This is expected behavior, from the Magic comments section in the README:

Note that for Ormolu to work the fragments where Ormolu is enabled must be parseable on their own.

For your example, Ormolu will format this snippet that preceeds the magic comment individually

{-

which is not parseable, which is exactly where the error message you are quoting is coming from.

In particular, this is not specific to multi-line comments; e.g. this will also fail to be formatted for the same reason:

test = 1 +
{- ORMOLU_DISABLE -}
  2

With additional heuristics, Ormolu's magic comments could try to handle cases like these, but I currently think that the reduced predictability and significantly increased complexity are not worth it.

Does that clear it up?

mrkkrp commented 1 year ago

Closing for now, feel free to reopen if needed.