emacs-lsp / lsp-mode

Emacs client/library for the Language Server Protocol
https://emacs-lsp.github.io/lsp-mode
GNU General Public License v3.0
4.72k stars 860 forks source link

lsp can't find node when installed by nvm and running in GUI Emacs #4478

Open dhdaines opened 1 week ago

dhdaines commented 1 week ago

Thank you for the bug report

Bug description

lsp requires both exec-path and the PATH environment variable to contain a directory with node and npm. When running Emacs from the command-line this isn't an issue, because nvm ensures that PATH gets set via .profie. However when launching Emacs from the window system (e.g. the "Dash" in Gnome) the shell is not involved so PATH isn't set. This leads to confusing errors (in messages / lsp-log) as soon as you try to install a server:

LSP :: Server ts-ls install process failed with the following error message: Unable to find a way to install typescript.

One can add the nvm directory manually to exec-path, but this isn't enough:

/home/dhd/.nvm/versions/node/v20.14.0/bin/npm -g --prefix /home/dhd/.emacs.d/.cache/lsp/npm/typescript install typescript
/usr/bin/env: ‘node’: No such file or directory

Adding it to PATH in .emacs instead doesn't work either, giving the same error as before:

Unable to install typescript via `npm' because it is not present

But if you add it to both exec-path and PATH then everything is fine. This is what would have happened had the PATH variable been set prior to launching Emacs, obviously. So the workaround is to add something like this to .emacs (which only works on Unix, sorry!)

(let ((node-bin
       (string-replace "/node\n" ""
                       (shell-command-to-string ". $HOME/.nvm/nvm.sh && nvm which current"))))
  (add-to-list 'exec-path node-bin)
  (setenv "PATH"
        (concat node-bin ":" (getenv "PATH"))))

One could also use nvm.el but unfortunately it requires you to specify an explicit version and can't just get the default one (which is found in ~/.nvm/alias/default in most cases)

Steps to reproduce

Expected behavior

lsp finds node and npm somehow ;-)

Which Language Server did you use?

ts-ls

OS

Linux

Error callstack

No response

Anything else?

The problem is known for some time but there is a lot of noise in related bug reports that make it hard to understand what is actually going on!