Skip to content

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

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

Closed
liammcdermott opened this issue Jun 25, 2024 · 11 comments
Closed

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

liammcdermott opened this issue Jun 25, 2024 · 11 comments
Labels
level: easy The issue is suited for beginners type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc..

Comments

@liammcdermott
Copy link

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 liammcdermott added status: needs triage type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc.. labels Jun 25, 2024
@fendor
Copy link
Collaborator

fendor commented Jun 26, 2024

Thank you for the bug report!

We should log the stderr if the command fails!

@fendor fendor added level: easy The issue is suited for beginners and removed status: needs triage labels Jun 26, 2024
@liammcdermott
Copy link
Author

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.

@michaelpj
Copy link
Collaborator

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.

@sgillespie
Copy link
Contributor

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))

@fendor
Copy link
Collaborator

fendor commented Aug 20, 2024

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.

@sgillespie
Copy link
Contributor

sgillespie commented Aug 20, 2024

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 -->

@fendor
Copy link
Collaborator

fendor commented Aug 20, 2024

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!

@sgillespie
Copy link
Contributor

sgillespie commented Aug 20, 2024

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,
      |                                       ^

@fendor
Copy link
Collaborator

fendor commented Aug 20, 2024

Yes, exactly!

@fendor
Copy link
Collaborator

fendor commented Aug 21, 2024

Fixed by #4387

@fendor fendor closed this as completed Aug 21, 2024
@liammcdermott
Copy link
Author

Thank you very much for fixing this! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
level: easy The issue is suited for beginners type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc..
Projects
None yet
Development

No branches or pull requests

4 participants