purcell / envrc

Emacs support for direnv which operates buffer-locally
364 stars 35 forks source link

Janky interaction with lsp-mode #31

Open Ralith opened 2 years ago

Ralith commented 2 years ago

Configured with

(use-package envrc
  :after (lsp-mode flycheck)
  :init (envrc-global-mode))

envrc works for the first file I open in a workspace, but when I open another file in the same workspace, lsp-mode complains that the configured language-server command is not present on the path. Is there a way I can get this working?

purcell commented 2 years ago

Is this for any specific language server?

Ralith commented 2 years ago

I'm using rust-analyzer, though I'd assume the "try to run a binary" code isn't terribly specific to it.

purcell commented 2 years ago

Yeah, I started to have a poke around lsp-mode to investigate. I think this is more of an lsp-mode issue tbh, but I'd at least like to be able to point at the upstream code that should be fixed up.

eqyiel commented 2 years ago

Some other things might have changed since this issue was open, but I found that using (lsp-deferred) to activate LSP instead of (lsp) works well with envrc. LSP can find the language server binary even if's loaded into $PATH using Nix + direnv, i.e. use nix -p gopls

mpasternacki commented 1 year ago

lsp-deferred works fine for starting rust-analyzer provided by direnv, but there's another problem: when I navigate to source of library I use, the files are in ~/.cargo/registry/, which is outside the project directory and its .envrc, so lsp-mode is not finding proper rust-analyzer.

It looks like the problem is caused by envrc working exactly as intended and it will be tricky to solve. Maybe I can convince cargo to download sources inside project? This will cause some duplication, but it will be covered by the .envrc at least.

Edited to add: the following .envrc line seems to be a good workaround to my problem for Rust/Cargo:

export CARGO_HOME="$(dirname "$(realpath "${BASH_SOURCE[0]}")")/target/.cargo"
MagicRB commented 1 year ago

The issue with envrc applying env after lsp-mode is indeed solved by lsp-deferred for me too.