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

135 Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

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.
0
On

Workaround

I moved the stack.yaml,stack.yaml.lock files to the root, and changed packages in the stack.yaml file from

packages:
  - .

to

packages:
  - backend

That way the HLS is happy and can find my Haskell project properly.

Other thoughts

I looked into the code of the vscode-haskell extension, and it looks like it doesn't support anything other than the root as the directory for HLS for workspaces.

I'm looking into contributing to the vscode haskell extension, adding the functionality to change the working dir for the HLS, but currently my setup for debugging the changes of extensions isn't working, so I'll look into that again later.

For now the workaround is the best I found :(