Search code examples
visual-studio-codehaskellhaskell-stackhaskell-language-server

Haskell-language-server starting in wrong folder in VS Code


I have a project that looks like this:

- my-project
  - backend
    - stack.yaml
    - src
      - ... a bunch of haskell files
    - ... exe & test folders here too
  - frontend
    - ... an Elm frontend app
  - flake.nix
  - ... other files

When I open this in vscode (cd my-project && codium .) and go to one of the haskell files I get some errors it cannot find packages.

However, when I first cd into the backend folder and then open code (cd my-project/backend && codium .) then everything works perfectly.

So I think the HLS is just being started at the root and then not able to pick up the haskell project properly.

I tried adding "haskell.serverExtraArgs": "--cwd /home/theoddler/Dev/my-project/backend" to my .vscode/.settings.json file, but I get the same errors.

How do I fix this? Can I change the directory where the vscode plugin starts the HLS?


Solution

  • I ran into a similar problem, but I am using Cabal instead of Stack. I wasn't quite happy with the solution of moving the files to the root directory just to make a VS Code Plugin work, as this would break my code structure.

    I found the following workaround, where my .cabal (and hopefully your stack.yaml) file can stay in the ./backend/ directory:

    1. Add my-project.code-workspace to your projects root directory:
    {
        "folders": [
            {
                "name": "my-workspace",
                "path": "."
            },
            {
                "name": "backend",
                "path": "backend"
            }
        ],
    }
    
    1. Use File -> Open Workspace from File... and select my-project.code-workspace
    2. Optionally, add a ./backend/settings.json to hide the redundant ./backend/ directory from the Explorer sidebar.
      Note: whenever you edit a file in ./backend/, the Explorer will highlight this file inside backend folder instead of the my-workspace/backend folder - but at least it will disappear again, once you switch to another file.