haskell / haskell-language-server

Official haskell ide support via language server (LSP). Successor of ghcide & haskell-ide-engine.
Apache License 2.0
2.64k stars 354 forks source link

How can I pass custom build options JUST to HLS? #4361

Closed ChrisPenner closed 3 weeks ago

ChrisPenner commented 1 month ago

Your environment

Which OS do you use?

MacOs

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

GHC 9.5 from GHCup

How is your project built (alternative: link to the project)?

I'm building using stack build --fast during development, but the default flags specify -O2.

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

N/A

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

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

What's wrong?

I realized recently that I'm running into a lot of problems because my project's (https://github.com/unisonweb/unison) default build settings specify an optimized build (which is necessary for our CI, and for folks building from source etc.) but during development I always stack build --fast to speed things up.

So what happens is:

Ideally I'd just have HLS always build using stack build --fast or the cabal equivalent, but I don't see any mechanism in the configuration for specifying how HLS builds things, it seems to always want to use your default build (or one inferred from the cradle).

Even if I could edit hie.yaml, our hie.yaml is checked-in, so it would necessarily change the behaviour for the rest of the team.

Is there any way I can configure HLS (maybe through an env var, or my editor's LSP settings) to specify some GHC flags or similar?

Thanks! Keep up the good work!

fendor commented 1 month ago

The easiest way is to write a stack-hls.yaml file that contains the options that you want and tell HLS to use that.

I can think of two solutions / workarounds:

  1. Provide the STACK_YAML variable to HLS only https://github.com/haskell/vscode-haskell?tab=readme-ov-file#set-additional-environment-variables-for-the-server
  2. Provide the stackYaml key in hie.yaml (https://hackage.haskell.org/package/hie-bios-0.14.0/#stack) and tell git to ignore changes, for example via git update-index --assume-unchanged hie.yaml.

I open a file in the editor and HLS starts building, but the build flags have changed so it starts over

I believe, this happens because HLS tells stack to use our custom compiler, which invalidates the caches. You can provide a different work-dir, this should avoid cache invalidation: https://docs.haskellstack.org/en/stable/environment_variables/#stack_work

ChrisPenner commented 1 month ago

@fendor ah great, this gives me something to work with! A bit annoying that I need to copy the whole stack.yaml and keep that up to date though 😢 ; but I'll give this a try 🙌🏼

fendor commented 3 weeks ago

Ill consider this issue resolved, feel free to open a new issue, if you want to propose a new feature :)