haskell-language-server icon indicating copy to clipboard operation
haskell-language-server copied to clipboard

Improve the error message when there is a typo in the Cabal file

Open liammcdermott opened this issue 1 year ago • 3 comments

Your environment

Which OS do you use? Ubuntu 24.04 + Nix flakes (Nix 2.21.2)

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

GHC 9.6.4 installed via devenv and Flakes, using languages.haskell.enable = true; documentation

How is your project built?

I haven't reached a point where that is a concern

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

VS Code + vscode-haskell

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

haskell-language-server version: 2.7.0.0 (GHC: 9.6.4), installed with GHC.

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

No. However, I configured vscode-haskell to look for HLS on the PATH and to not try downloading it

Steps to reproduce

Have a Cabal file with a small typo in it, for example:

cabal-version:       >=1.10

name:                foobar
version:             0.1.0.0
-- synopsis:
-- description:
-- bug-reports:
-- license:
license-file:        LICENSE
-- author:
-- maintainer:
-- copyright:
-- category:
build-type:          Simple
extra-source-files:  CHANGELOG.md

executable foobar
  main-is:             Main.hs
  -- other-modules:
  -- other-extensions:
  build-depends:
    base,
    modern-uri,
    text,
  -- hs-source-dirs:
  default-language:    Haskell2010
  extensions:
    OverloadedStrings
    , FlexibleInstances
    , ExtendedDefaultRules

See this error in VSCode:

Failed to find the GHC version of this Cabal project. Error when calling cabal exec -v0 -- ghc --print-libdir

Run haskell-language-server-wrapper --probe-tools and get something like:

haskell-language-server version: 2.7.0.0 (GHC: 9.6.4) (PATH: /nix/store/z5cmjy6h5bssrj41rykhfzwc8yx48ixn-haskell-language-server-2.7.0.0/bin/haskell-language-server-wrapper)
Tool versions found on the $PATH
cabal:          3.10.2.1
stack:          2.15.3
ghc:            9.6.4

Tool versions in your project
2024-06-25T17:46:25.475821Z | Debug | cabal exec -v0 -- ghc --print-libdir
ghc:            Not found

Expected behaviour

Haskell Language Server should return an error message pointing to the actual problem, for example:

Error parsing foobar.cabal: [ ... error details ... ]

This points the user in the right direction, and it's obvious what they should do to fix the error.

Actual behaviour

The current error message leads the user on a wild goose chase, trying to figure out why ghc is in their PATH but not their project. For example, I was hunting down ways to specify a GHC version/path in foobar.cabal.

The second part of the message points to the source of the error, but the first part is just confusing for the user. I understand the first part isn't wrong, it's what Haskell Language Server was doing when it hit an error condition, but Haskell Language Server could be better about checking for errors in the .cabal file up-front, and reporting them early.

liammcdermott avatar Jun 25 '24 18:06 liammcdermott

Thank you for the bug report!

We should log the stderr if the command fails!

fendor avatar Jun 26 '24 12:06 fendor

Thank you, that would help!

I wonder, are there any circumstances where, Failed to find the GHC version of this Cabal project. is helpful? I found it very confusing in this case, which is where my suggestion to check the Cabal file for errors ahead of time comes from.

liammcdermott avatar Jun 27 '24 18:06 liammcdermott

I think the problem is that this comes before HLS has even started up, and we're trying to interrogate cabal to find out the version of GHC so we can actually launch the right HLS.

michaelpj avatar Jun 28 '24 21:06 michaelpj

We should log the stderr if the command fails!

If I'm reading this correctly, this is already the behavior? The problem would therefore be that it's not sent to the client?

Here's what I see in emacs/eglot:

[server-request] (id:0) Mon Aug 19 16:28:17 2024:
(:id 0 :jsonrpc "2.0" :method "window/showMessageRequest" :params
     (:actions
      [(:title "Try to restart")]
      :message "Failed to find the GHC version of this Cabal project.\nError when calling cabal exec -v0 -- ghc --print-libdir" :type 1))

sgillespie avatar Aug 19 '24 20:08 sgillespie

I am not sure, but it looks like we are not logging the stderr of cabal exec -v0 -- ghc --print-libdir due to the -v0 flag hiding the stderr. We could run cabal exec -- ghc --print-libdir if the prior command fails to actually capture the stderr.

fendor avatar Aug 20 '24 10:08 fendor

In my case, I can also see the stderr:

[stderr] 2024-08-19T20:32:03.323578Z | Debug | cabal exec -v0 -- ghc --print-libdir
[stderr] Failed to find the GHC version of this Cabal project.
[stderr] Error when calling cabal exec -v0 -- ghc --print-libdir
[stderr] 
[stderr] Errors encountered when parsing cabal file ./hls4336.cabal:
[stderr] 
[stderr] hls4336.cabal:24:39: error:
[stderr] unexpected major bounded version syntax (caret, ^>=) used. To use this syntax the package need to specify at least 'cabal-version: 2.0'. Alternatively, if broader compatibility is important then use: >=4.18.2.1 && <4.19
[stderr] expecting "." or "-"
[stderr] 
[stderr]    21 |     main-is:          Main.hs
[stderr]    22 |     -- other-modules:
[stderr]    23 |     -- other-extensions:
[stderr]    24 |     build-depends:    base ^>=4.18.2.1,
[stderr]       |                                       ^
[stderr] 
[stderr] hls4336.cabal:20:5: warning:
[stderr] Unknown field: import. You should set cabal-version: 2.2 or larger to use common stanzas
[stderr] 
[stderr]    19 | executable hls4336
[stderr]    20 |     import:           warnings
[stderr]       |     ^
[stderr] 
<-- ... More warnings -->

sgillespie avatar Aug 20 '24 12:08 sgillespie

Hm, thanks for checking, you are right, I have this as well!

Then this issue seems to be purely about the behaviour of haskell-language-server --probe-tools. Should be an easy fix!

fendor avatar Aug 20 '24 13:08 fendor

Then this issue seems to be purely about the behaviour of haskell-language-server --probe-tools. Should be an easy fix!

Are you thinking something like this?

> haskell-language-server-wrapper --probe-tools
haskell-language-server version: 2.9.0.1 (GHC: 9.6.5) (PATH: /home/sgillespie/dev/haskell/haskell-language-server/dist-newstyle/build/x86_64-linux/ghc-9.6.5/haskell-language-server-2.9.0.1/x/haskell-language-server-wrapper/build/haskell-language-server-wrapper/haskell-language-server-wrapper) (GIT hash: 6f6f75bc410c51352e56a87a38a5345bdd44d0bb)
Tool versions found on the $PATH
cabal:          3.10.3.0
stack:          2.15.7
ghc:            9.6.5

Tool versions in your project
2024-08-20T18:32:24.325299Z | Debug | cabal exec -v0 -- ghc --print-libdir
Failed to find the GHC version of this Cabal project.
Error when calling cabal exec -v0 -- ghc --print-libdir

Errors encountered when parsing cabal file ./hls4336.cabal:

hls4336.cabal:20:39: error:
unexpected end of input
expecting white space

   17 |     main-is:          Main.hs
   18 |     -- other-modules:
   19 |     -- other-extensions:
   20 |     build-depends:    base >=4.18.2.1,
      |                                       ^

sgillespie avatar Aug 20 '24 18:08 sgillespie

Yes, exactly!

fendor avatar Aug 20 '24 20:08 fendor

Fixed by #4387

fendor avatar Aug 21 '24 07:08 fendor

Thank you very much for fixing this! :heart:

liammcdermott avatar Aug 22 '24 19:08 liammcdermott