Nvim-lspconfig: Add pyright support

Created on 6 Jul 2020  ·  8Comments  ·  Source: neovim/nvim-lspconfig

Microsoft is moving on to a proprietary language server backend for python in vscode, pylance, which is implemented on top of the pyright library. It seems like development will be ceasing on the C# Microsoft Python Language Server in favor of this implementation (which is JS based, but apparently somehow much faster). Pyright also includes a language server implementation (sans some additional ML powered intellisense features in pyright). Coc has added support via coc-pyright and there has been some discussion on using the pyright in emacs lsp-mode [here] (https://github.com/emacs-lsp/lsp-mode/issues/1863).

I managed to get nvim to launch and connect to the pyright language server as follows:

local configs = require 'nvim_lsp/configs'
local util = require 'nvim_lsp/util'

local server_name = "pyright"

configs[server_name] = {
  default_config = {
    cmd = {"node", "/home/michael/Repositories/pyright/client/server/server.bundle.js", "--stdio"};
    filetypes = {"python"};
    root_dir = function(fname)
      return util.find_git_ancestor(fname) or vim.loop.os_homedir()
    end;
  };
  docs = {
    description = [[
]];
    default_config = {
      root_dir = [[root_pattern(".git", vim.fn.getcwd())]];
    };
  };
}

-- vim:et ts=2 sw=2

But currently it fails with:

[ DEBUG ] 2020-07-01T22:19:39-0700 ] ...-unwrapped-master/share/nvim/runtime/lua/vim/lsp/rpc.lua:347 ]  "decoded"       {  jsonrpc = "2.0",  method = "window/logMessage",  params = {    message = "Notification handler 'initialized' failed with message: Client doesn't support sending workspace folder change events.",    type = 1  }}
[ DEBUG ] 2020-07-01T22:19:39-0700 ] ...ovim-unwrapped-master/share/nvim/runtime/lua/vim/lsp.lua:343 ]  "notification"  "window/logMessage"     {  message = "Notification handler 'initialized' failed with message: Client doesn't support sending workspace folder change events.",  type = 1}
[ DEBUG ] 2020-07-01T22:19:39-0700 ] ...pped-master/share/nvim/runtime/lua/vim/lsp/callbacks.lua:257 ]  "default_callback"      "window/logMessage"     {  client_id = 1,  params = {    message = "Notification handler 'initialized' failed with message: Client doesn't support sending workspace folder change events.",    type = 1  }}
[ ERROR ] 2020-07-01T22:19:39-0700 ] ...pped-master/share/nvim/runtime/lua/vim/lsp/callbacks.lua:226 ]  "Notification handler 'initialized' failed with message: Client doesn't support sending workspace folder change events."
[ DEBUG ] 2020-07-01T22:21:32-0700 ] ...ovim-unwrapped-master/share/nvim/runtime/lua/vim/lsp.lua:587 ]  "on_lines"      1       4       7       7       8       0       0       0       { "" }
bug

Most helpful comment

Damn you, Microsoft

All 8 comments

Damn you, Microsoft

As of https://github.com/microsoft/pyright/pull/810 pyright language server can be installed via npm

npm install -g pyright
pyright-langserver

As of microsoft/pyright#810 pyright language server can be installed via npm

npm install -g 
pyright-langserver

Doesn't seem to be in the npm registry?

npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/pyright-langserver - Not found
npm ERR! 404
npm ERR! 404  'pyright-langserver@latest' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)

@nymann Was missing the package, pyright-langserver --stdio is the command invoked, the package is pyright

FYI while we wait for the PR
I think you can get around the neovim PR by adding the following to the pyright config

before_init = function(initialize_params, config)
            initialize_params['workspaceFolders'] = {{
                name = 'workspace',
                uri = initialize_params['rootUri']
            }}

this will not add, remove, or list the workspace folders, but those functions I think can be added to your init.vim

Pylance support If you need.
https://github.com/aca/pylance.nvim

Damn you, Microsoft

Unfortunately, as of v2020.10.3 running Pylance outside of Visual Studio Code no longer works 😞

$ node ~/.vscode/extensions/ms-python.vscode-pylance-2020.10.3/dist/server.bundle.js --stdio
You may install and use any number of copies of the software only with
Microsoft Visual Studio, Visual Studio for Mac, Visual Studio Code, Azure
DevOps, Team Foundation Server, and successor Microsoft products andservices
(collectively, the “Visual Studio Products and Services”) to develop and test
your applications. The software is licensed, not sold. This agreement only
gives you some rights to use the software. Microsoft reserves all other rights.
You may not: work around any technical limitations in the software that only
allow you to use it in certain ways; reverse engineer, decompile or disassemble
the software, or otherwise attempt to derive the source code for the software,
except and to the extent required by third party licensing terms governing use
of certain open source components that may be included in the software; remove,
minimize, block, or modify any notices of Microsoft or its suppliers in the
software; use the software in any way that is against the law or to create or
propagate malware; or share, publish, distribute, or lease the software (except
for any distributable code, subject to the terms above), provide the software
as a stand-alone offering for others to use, or transfer the software or this
agreement to any third party.

Using Pyright directly is also the recommended and supported way and that still works.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

justinmk picture justinmk  ·  7Comments

glacambre picture glacambre  ·  5Comments

nomasprime picture nomasprime  ·  5Comments

Kyle-Thompson picture Kyle-Thompson  ·  7Comments

K1DV5 picture K1DV5  ·  10Comments