haskell / haskell-language-server

Official haskell ide support via language server (LSP). Successor of ghcide & haskell-ide-engine.
Apache License 2.0
2.71k stars 368 forks source link

Cannot format only "modified" lines #4074

Closed emlautarom1 closed 8 months ago

emlautarom1 commented 8 months ago

Today I've upgrade to GHC 9.x from 8.10.4, together with HLS and Cabal. Before the upgrade, I was able to format my code using fourmolu alongside VSCode's "editor.formatOnSaveMode": "modificationsIfAvailable" option, which only formats part of the code that has been changed (sometimes I want to decide how to format certain code instead of my formatter).

After upgrading, this option no longer works forcing me to format the entire modified Haskell file.

Your environment

Which OS do you use?

Which version of GHC do you use and how did you install it?

How is your project built (alternative: link to the project)?

Which LSP client (editor/plugin) do you use?

Which version of HLS do you use and how did you install it?

Have you configured HLS in any way (especially: a hie.yaml file)?

Steps to reproduce

This requires VSCode's with the "editor.formatOnSaveMode": "modificationsIfAvailable" option set in the settings.json file.

  1. Create a Haskell project (ex. through cabal) with Git support
  2. Write some valid Haskell code
  3. Commit the changes
  4. Modify the code
  5. Save the file to trigger the formatter

Expected behaviour

Modified lines should be formatted.

Actual behaviour

HLS throws and error and does not format the code as intended.

Debug information

2024-02-13T15:51:12.327971Z | Info | fourmolu: Loaded Fourmolu config from: "/home/emlautarom1/.config/fourmolu.yaml"
2024-02-13T15:51:12.331138Z | Error | fourmolu: Internal Error: OrmoluParsingFailed (RealSrcSpan SrcSpanOneLine "/home/emlautarom1/Development/Haskell/shamir-secret-sharing/app/Main.hs" 245 15 17 Nothing) "[GHC-58481] parse error on input `->'"
[Error - 12:51:12 PM] Request textDocument/rangeFormatting failed.
  Message: fourmolu: Internal Error: OrmoluParsingFailed (RealSrcSpan SrcSpanOneLine "/home/emlautarom1/Development/Haskell/shamir-secret-sharing/app/Main.hs" 245 15 17 Nothing) "[GHC-58481] parse error on input `->'"
  Code: -32603 
2024-02-13T15:51:12.380405Z | Info | Typechecking reverse dependencies for NormalizedFilePath "/home/emlautarom1/Development/Haskell/shamir-secret-sharing/app/Main.hs": [  ]
emlautarom1 commented 8 months ago

For now, my workaround is to format the entire file on save. In VSCode's settings.json, add the following:

"[haskell]": {
    "editor.formatOnSaveMode": "file",
}
emlautarom1 commented 8 months ago

Note that I'm able to format selections by selecting a range of text and using the Format Selection command. My guess is that VSCode is sending different information between the command and it's automatic "format on save" feature, the later causing issues for fourmolu/ormolu.

michaelpj commented 8 months ago

Could you take a look at the message logs and see what different messages VSCode is sending in the two cases? I would indeed expect them to be the same.

emlautarom1 commented 8 months ago

Ok, after further investigation it seems like the problem also exist when using the Format Selection command. A minimal reproducible example is as follows:

  1. Create a Haskell file
  2. Write the following code:
x = case () of
  () ->   ()
  1. Select the complete () -> () line
  2. Run Format Selection.

HLS fails to invoke the formatter with the following error:

2024-02-20T13:29:55.151918Z | Error | fourmolu: Internal Error: OrmoluParsingFailed (RealSrcSpan SrcSpanOneLine "/home/emlautarom1/Development/Haskell/myapp/app/Main.hs" 249 4 6 Nothing) "[GHC-58481] parse error on input `->'"
[Error - 10:29:55 AM] Request textDocument/rangeFormatting failed.
  Message: fourmolu: Internal Error: OrmoluParsingFailed (RealSrcSpan SrcSpanOneLine "/home/emlautarom1/Development/Haskell/myapp/app/Main.hs" 249 4 6 Nothing) "[GHC-58481] parse error on input `->'"
  Code: -32603 

This seems to be an issue with Ormolu/Fourmolu and not HLS related.

michaelpj commented 8 months ago

Yes, that seems right. It looks like we're using native support in ormolu/fourmolu for formatting ranges, so I think it is indeed a bug.

michaelpj commented 8 months ago

Closing as it's tracked upstream.