syl20bnr / spacemacs

A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
http://spacemacs.org
GNU General Public License v3.0
23.58k stars 4.9k forks source link

[JS/TS] LSP :: Error from the Language Server: Request initialize failed with message: Could not find a valid TypeScript installation. Please ensure that the "typescript" dependency is installed in the workspace... #16286

Closed stradicat closed 4 months ago

stradicat commented 4 months ago

Description :octocat:

Whenever I load a javascript project, I see this message and lsp fails to autocomplete or syntax-check:

LSP :: Error from the Language Server: Request initialize failed with message: Could not find a valid TypeScript installation. Please ensure that the "typescript" dependency is installed in the workspace or that a valid 'tsserver.path' is specified. Exiting. (Internal Error)

As lsp backend for the javascript layer, I'm using ts-ls as recommended.

Node is installed via NVM, and its path is already both in ~/.zshrc and ~/.spacemacs.env; node is available:

> which node
/Users/my_user/.nvm/versions/node/v18.19.1/bin/node

NVM's bin location was already picked up in spacemacs.env:

NVM_BIN=/Users/my_user/.nvm/versions/node/v18.19.1/bin
NVM_CD_FLAGS=-q
NVM_DIR=/Users/my_user/.nvm
NVM_INC=/Users/my_user/.nvm/versions/node/v18.19.1/include/node

Inside Emacs, the typescript package is installed by lsp-install-server along typescript-language-server when choosing ts-ls as backend (expected, correct behaviour), and both are stored by default under ~/.emacs.d/.cache/lsp/npm/.

For comparison, installing javascript-language-server brings some basic autocompletion and syntax-checking, although it's deprecated already.

I've removed every lsp language server under ~/.emacs.d/.cache/lsp to freshly install ts-ls.

For reference, here are my user session's globally installed node modules, in case any of them could interfere:

├── @angular/cli@17.2.0
├── @angular/language-server@17.2.2
├── @apollo/server@4.10.0
├── @bazel/bazelisk@1.19.0
├── @vue/cli@5.0.8
├── ajv@8.12.0
├── avd-launcher@0.3.0
├── browserify@17.0.0
├── core-js@3.36.0
├── corepack@0.22.0
├── express-generator@4.16.1
├── flow-bin@0.229.0
├── git-standup@2.3.2
├── grunt-cli@1.4.3
├── gulp-cli@2.3.0
├── html-minifier@4.0.0
├── import-js@5.1.0
├── js-beautify@1.15.0
├── jsdoc@4.0.2
├── marked@12.0.0
├── minify@11.0.1
├── node-gyp@10.0.1
├── nodemon@3.0.3
├── npm@10.4.0
├── pm2@5.3.1
├── prettier-eslint-cli@8.0.1
├── prettier-eslint@16.3.0
├── prettier@3.2.5
├── pug-cli@1.0.0-alpha6
├── sqlite3@5.1.7
├── strong-ursa@0.11.0
├── typescript@5.3.3
├── uglify-js@3.17.4
├── uuid@9.0.1
├── volar@0.0.2
└── vue@3.4.19

Reproduction guide :beetle:

Observed behaviour: :eyes: :broken_heart: No completion or syntax-checking

Expected behaviour: :heart: :smile: Auto-completion and syntax-checking

System Info :computer:

alexey0308 commented 4 months ago

It looks like an upstream issue? https://github.com/emacs-lsp/lsp-mode/issues/4254

stradicat commented 4 months ago

As a workaround, I fiddled with my PATH variables in .zshrc, and reimported them into .spacemacs.env, so lsp detects nvm and makes ts-ls work.

Since it's an upstream issue, I'm closing this. Thanks for the heads-up! I'll follow your referenced issue.

Edit: I'll reopen it to add more feedback, in case macOS behaves a bit differently from Linux.

alexey0308 commented 4 months ago

Hi @stradicat,

1) Do you get a message in the lsp-log buffer with something like

Typescript specified through user setting ignored due to invalid path ..completely_wrong_path..

?

If you get this information, I would suggest to close this issue and report your PATH problem to the linked issue, since you also consider it to be not a spacemacs but upstream one.

2) For the nvm installation it looks like you have to update the .spacemacs.env anyway, unless you find a package which would manage the node PATH for you, so basically you did solve your problem. But I suspect that the LSP is probably using the globally installed typescript and not the one from .emacs.d/.cache/....

stradicat commented 4 months ago

Hey @alexey0308,

I am indeed getting the error on 1. :

[lspserver] Typescript specified through user setting ignored due to invalid path "/Users/my_user/Projects/Project_Dir/lib/tsserver.js"

In the end, changing the order of the PATH exports in .zshrc to be picked up by Emacs in .spacemacs.env kind of made lsp pick up typescript (probably the global install).

I've also had problems with vue-semantic-server and rust-analyzer, which were apparently resolved with the PATH reorder.

I guess I'm closing the issue. Thanks again for looking into it.

differentMonster commented 3 months ago

@stradicat

I am facing the same problem could you share what you .zshrc and .spacemacs.env change in here, it will help making it clear about what you change, thanks ya.