neovim / nvim-lspconfig

Quickstart configs for Nvim LSP
Apache License 2.0
10.46k stars 2.06k forks source link

LspInstall for pyright and intelephense fails when node packages are installed with the same name as the plugin #467

Closed rverton closed 3 years ago

rverton commented 3 years ago

How to reproduce the problem from neovim startup

Execute :LspInstall pyright or :LspInstall intelephense.

Actual behaviour

The following error is printed (reformatted because its broken):

18 verbose stack Error: Refusing to install package with name "pyright" under a package
18 verbose stack also called "pyright". Did you name your project the same
18 verbose stack as the dependency you're installing?

The same happens when installing intelephense. Looking in the /Users/robin/.cache/nvim/lspconfig/pyright folder (which is created by the LspInstall command), there is a package.json with the following content:

{
  "name": "pyright",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Running a npm install pyright here fails because the package name can't have the same name as the dependency I think. Executing :LspInstall tsserver works here, because the actual package which is installed is called typescript-language-server and therefore has a different name than the folder.

mjlbach commented 3 years ago

I'm not sure I understand this bug. If pyright is already installed, and you call :LspInstall pyright it fails?

rverton commented 3 years ago

Pyright/intelephense are not installed. If they would be installed, LspInstall would tell it and refuse to go on.

The LspInstall command creates a folder called “pyright”, puts a package.json in there with the name “pyright” and tries to install the module “pyright” in this I guess. Npm then refuses to do this.

mjlbach commented 3 years ago

Does npm i -g pyright not work for you?

rverton commented 3 years ago

When I install pyright manually via npm, then LSP does not work here. This is what LspInstallInfo gives me for pyright:

  pyright = {
    bin_dir = "/Users/robin/.cache/nvim/lspconfig/pyright/node_modules/.bin",
    binaries = {
      ["pyright-langserver"] = "/Users/robin/.cache/nvim/lspconfig/pyright/node_modules/.bin/pyright-langserver"
    },
    install_dir = "/Users/robin/.cache/nvim/lspconfig/pyright",
    is_installed = false
  },

The folder /Users/robin/.cache/nvim/lspconfig/pyright does not exist (when pyright is installed manually), is it the intended way that I create it and symlink pyright in there?

mjlbach commented 3 years ago

It doesn't matter where pyright is installed so long as it is in your path. LspInstallInfo just uses the built-in installer directory to check if pyright was installed via LspInstall (not that it's actually present) so this would be a false negative, but you can install pyright as a global npm package and it will work (no symlink required)

rverton commented 3 years ago

All right. So even when it is installed, I’m missing the LSP features. Is there some log or health info to see what’s wrong?

mjlbach commented 3 years ago

Are you using node15? There's a tracking issue and PR to workaround node 15 issues if you look in the tracker. To get logs you can add vim.lsp.set_log_level("debug") to your lua heredoc, and your log will be saved as $HOME/.local/share/nvim/lsp.log

rverton commented 3 years ago

@mjlbach thanks for the hint, I'm using node v15.0.1. I guess you are referring to https://github.com/neovim/nvim-lspconfig/pull/451.

I had a look at the lsp.log and found this:

[ ERROR ] 2020-12-20T13:18:50+0100 ] .../share/nvim-osx64/share/nvim/runtime/lua/vim/lsp/rpc.lua:455 ]  "rpc"   "pyright-langserver"    "stderr"    "Error: MethodNotFound\n    at handleResponse (/usr/local/Cellar/node/15.0.1/lib/node_modules/pyright/dist/pyright-internal/node_modules/vscode-jsonrpc/lib/common/connection.js:477:48)\n    at processMessageQueue (/usr/local/Cellar/node/15.0.1/lib/node_modules/pyright/dist/pyright-internal/node_modules/vscode-jsonrpc/lib/common/connection.js:292:17)\n    at Immediate.<anonymous> (/usr/local/Cellar/node/15.0.1/lib/node_modules/pyright/dist/pyright-internal/node_modules/vscode-jsonrpc/lib/common/connection.js:276:13)\n    at processImmediate (node:internal/timers:462:21)\n"

There is also an issue for this bug (which is all linked to the node15 bug I think). I replace my local file .vim/plugged/nvim-lspconfig/lua/lspconfig/pyright.lua with the one from your PR and it worked, awesome! I guess the easiest way is to leave this replaced file now, wait until the PR gets merged and then do a fresh pull from the master branch then?

mjlbach commented 3 years ago

Yep! You can follow that issue and pr for updates.

monooso commented 3 years ago

@mjlbach I have the same issue as the OP. The ensuing discussion didn't address the initial problem, so I think it's worth re-opening this issue.

To reiterate, Node will not install a dependency which has the same name as the package into which it's being installed. An example may help to clarify this statement.

Assume you have the following package.json:

{ "name": "intelephense" }

In this case, npm install intelephense will fail, because the dependency name conflicts with the package name.

Unfortunately, this is exactly what :LspInstall attempts to do; it creates the following ~/.cache/nvim/lspconfig/intelephense/package.json, and then attempts to install the intelephense package.

{
  "name": "intelephense",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

This seems like a bug in the behaviour of LspInstall.

mjlbach commented 3 years ago

Yes, that's true (I realize I just helped OP workaround the built-in installer). Have you seen the pinned issue? https://github.com/neovim/nvim-lspconfig/issues/334

These installers are likely being removed soon. There are a ton of issues with the built-in installers, as evidenced by a large number of bug reports ultimately being related to failing installers, and windows support is spotty at best.

mjlbach commented 3 years ago

Do you want to try https://github.com/neovim/nvim-lspconfig/pull/476?

mjlbach commented 3 years ago

Whoops ignore, wrong fix

monooso commented 3 years ago

@mjlbach Thanks for pointing me to #334. Am I correct in saying that the current advice is to avoid :LspInstall, and manually install and run the appropriate LSP instead?

mjlbach commented 3 years ago

I don't think there's official advice/general policy, but my gut would be that PRs will be merged that fix LspInstall but that generally it will be more in-line with future recommendations to allow your system package manager to handle install of servers. FWIW, I use nix for this, and only ever debug LspInstall on behalf of others.

monooso commented 3 years ago

Does that mean you manually spin up the relevant Language Servers outside of Neovim, so that the Neovim LSP client can connect to them?

Sorry if that's a dumb question; I'm a little unclear as to what I need to do manually, and what this plugin takes care of automatically.

mjlbach commented 3 years ago

No, neovim will launch the language server for you. What is planning to be removed is the installer (so LspInstall and LspInstallInfo). So long as the language server is on your path, everything will work as it does currently.

monooso commented 3 years ago

Got it. Thanks for the clarification.

mjlbach commented 3 years ago

Closed by #498