haskellghcuphaskell-language-server

How to get a working Haskell VS Code setup with ghcup?


I installed Haskell via ghcup

I have installed the HLS via ghcup

I have ghcup set 9.6.1

I have the VS Code extension: https://marketplace.visualstudio.com/items?itemName=haskell.haskell

When I open the project in VS Code I get the following error:

Failed to find the GHC version of this Stack project. Error when calling stack setup --silent

So far I have been using Cabal in the terminal outside of VS Code e.g. cabal build cabal repl etc

I notice that VS Code is trying to use stack... if I try and use Stack in the terminal I get errors like:

$ stack build
Error: [S-9443]
No setup information found for ghc-8.6.5 on your platform.
This probably means a GHC bindist has not yet been added for OS key 'macosx-aarch64'.
Supported versions: ghc-8.10.5, ghc-8.10.6, ghc-8.10.7, ghc-9.0.2, ghc-9.2.1, ghc-9.2.2, ghc-9.2.3, ghc-9.2.4, ghc-9.2.5, ghc-9.2.6, ghc-9.2.7, ghc-9.4.1, ghc-9.4.2, ghc-9.4.3, ghc-9.4.4, ghc-9.6.1

I don't know why Stack wants to use GHC 8.6.5, or if this is related to the problem that VS Code extension is having

No idea really what to do, the instructions are ...minimal https://www.haskell.org/ghcup/install/#vscode-integration


Solution

  • Issue discussed in more detail here: https://github.com/haskell/vscode-haskell/issues/841

    Essentially I had a collection of problems, some because the project I was trying to open was old, and some because I am still finding my way around the ghcup toolchain:

    • project was built for a version of GHC (8.6.5) too old to install via ghcup and thus needed some config tweaks
    • project defined both a .cabal and a stack.yaml file
    • the stack.yaml defined resolver: lts-14.4... this represents a "stackage snapshot" version. I'm not entirely sure what that is, I think it is a concept that doesn't exist in package managers I have used in other languages. I think it is a frozen set of package versions, in this case so old they are only compatible with the old GHC 8.6.5 version, thus was unusable with the GHC 9.x I was trying to use currently, leading to errors - VS Code extension was trying to use this to install dependencies
    • I had installed latest GHC (9.6.1) via ghcup but apparently it's not possible to install a Haskell Language Server for that version yet

    So a bunch of fixes were needed:

    • in my case I just deleted the stack.yaml ... it had an obsolete resolver and also defined:
        packages:
        - '.'
      
      which seemed to prevent VS Code from installing any dependencies. The stack.yaml didn't seem to be doing anything useful.
    • tweaked some dependency versions (remove upper bound from build-depends: base >=4.0 && <4.13 so that a version compatible with GHC 9.x could be resolved)
    • used ghcup set ghc to end up with older GHC 9.2.5, this seems to have a compatible HLS version installed.
      This is based on the advice I had previously ignored in the terminal:
      $ ghcup set 9.6.1
      [ Warn  ] This is an old-style command for setting GHC. Use 'ghcup set ghc' instead.
      
    • after all these, restart VS Code and open a source file... wait a few minutes while it works silently in the background. Eventually the intellisense was working now

    I do still have "haskell.manageHLS": "GHCup" in my VS Code settings.json, not sure if this is needed or not.