haskell / haskell-language-server

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

Failed to format code with error "OrmoluParsingFailed" #3912

Closed Alecton4 closed 8 months ago

Alecton4 commented 8 months ago

Your environment

#### Which OS do you use? Operating System: openSUSE Tumbleweed 20231206 KDE Plasma Version: 5.27.9 KDE Frameworks Version: 5.112.0 Qt Version: 5.15.11 Kernel Version: 6.6.3-1-default (64-bit) Graphics Platform: X11 #### Which version of GHC do you use and how did you install it? Output of `haskell-language-server-wrapper --probe-tools`: ```text haskell-language-server version: 2.4.0.0 (GHC: 9.0.2) (PATH: /home/az/.ghcup/hls/2.4.0.0/lib/haskell-language-server-2.4.0.0/bin/haskell-language-server-wrapper) Tool versions found on the $PATH cabal: 3.6.2.1 stack: 2.11.1 ghc: 9.4.7 Tool versions in your project 2023-12-18T06:37:13.171524Z | Debug | executing command: ghc --numeric-version ghc: 9.4.7 ``` Installed via `curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh` #### How is your project built (alternative: link to the project)? https://github.com/Alecton4/playground-Haskell/blob/master/Stdm.lhs #### Which LSP client (editor/plugin) do you use? VSCode 1.85.0 + `haskell.haskell` 2.4.2 #### Which version of HLS do you use and how did you install it? See above. #### Have you configured HLS in any way (especially: a `hie.yaml` file)? No.

Steps to reproduce

  1. Open the file and try to format it.

Expected behaviour

The file should be formatted, I guess.

Actual behaviour

An error occur:

image

Debug information

The log in the output panel:

2023-12-18T06:39:31.407264Z | Error | ormolu: Internal Error: ormoluCmd: OrmoluParsingFailed (RealSrcSpan SrcSpanOneLine "/home/az/workspace/playground-Haskell/playground/Stdm.lhs" 6 1 5 Nothing) "[GHC-58481] parse error on input `Last'"
[Error - 2:39:31 PM] Request textDocument/formatting failed.
  Message: ormolu: Internal Error: ormoluCmd: OrmoluParsingFailed (RealSrcSpan SrcSpanOneLine "/home/az/workspace/playground-Haskell/playground/Stdm.lhs" 6 1 5 Nothing) "[GHC-58481] parse error on input `Last'"
  Code: -32603 
fendor commented 8 months ago

Can reproduce with ormolu only: image

Looks like ormolu tries to parse the literate haskell file as a haskell file. Maybe ormolu cannot parse .lhs files?

Would you mind reporting this issue upstream?

Alecton4 commented 8 months ago

@fendor Thanks! I will report this upstream. BTW, do you suggest any other formatter that deals with literal Haskell and could be integrated with the VSCode extension?

fendor commented 8 months ago

HLS comes with a number of formatters such as stylish-haskell, fourmolu, ormolu and floskell. You switch them with Settings > Extensions > Haskell: Formatting Provider. fourmolu is based off ormolu, thus has the same issue. stylish-haskell barely formats, mainly unifies the imports and I haven't used floskell before, I think. (Brittany is also in the list, but has been removed in recent HLS releases). So, give floskell a try, maybe you are lucky :)

Alecton4 commented 8 months ago

Update: Tried floskell and it failed with the following message:

2023-12-18T10:11:20.558419Z | Error | floskell: Internal Error: floskellCmd: /home/az/workspace/playground-Haskell/playground/Stdm.lhs:6:1: TemplateHaskell language extension is not enabled. Please add {-# LANGUAGE TemplateHaskell #-} pragma at the top of your module.
[Error - 6:11:20 PM] Request textDocument/formatting failed.
  Message: floskell: Internal Error: floskellCmd: /home/az/workspace/playground-Haskell/playground/Stdm.lhs:6:1: TemplateHaskell language extension is not enabled. Please add {-# LANGUAGE TemplateHaskell #-} pragma at the top of your module.
  Code: -32603 

After adding {-# LANGUAGE TemplateHaskell #-} to the top of the .lhs file, it failed with the following message:

2023-12-18T10:15:29.624716Z | Error | floskell: Internal Error: floskellCmd: /home/az/workspace/playground-Haskell/playground/Stdm.lhs:7:1: Parse error: Last
[Error - 6:15:29 PM] Request textDocument/formatting failed.
  Message: floskell: Internal Error: floskellCmd: /home/az/workspace/playground-Haskell/playground/Stdm.lhs:7:1: Parse error: Last
  Code: -32603 
fendor commented 8 months ago

Yeah, that looks like it does not understand literate haskell files, I am sorry, it seems you are out of luck :(