williamboman / nvim-lsp-installer

Further development has moved to https://github.com/williamboman/mason.nvim!
https://github.com/williamboman/mason.nvim
Apache License 2.0
2k stars 123 forks source link

For pylsp server command PylspInstall python-lsp-black does not work out of the box #416

Closed binamkayastha closed 2 years ago

binamkayastha commented 2 years ago

(thank you for your work on the installer)

Describe the issue

For the pylsp server after running :PylspInstall python-lsp-black does not use the black formatter. It uses the autopep8 and yapf formatters instead because :LspInstall pylsp installs the python lsp with the [all] descriptor (python-lsp-server[all] ).

Links nvim-lsp-installer PylspInstall code python-lsp-server python-lsp-black

System info (please complete the following information):

Checkhealth output (:checkhealth nvim-lsp-installer): Linux

nvim-lsp-installer: require("nvim-lsp-installer.health").check()
========================================================================
## nvim-lsp-installer report
  - OK: neovim version >= 0.6.0
  - WARNING: **Ruby**: not available
  - WARNING: **RubyGem**: not available
  - WARNING: **Composer**: not available
  - WARNING: **PHP**: not available
  - OK: **gzip**: `gzip 1.11`
  - OK: **curl**: `curl 7.80.0 (x86_64-pc-linux-gnu) libcurl/7.80.0 OpenSSL/1.1.1l zlib/1.2.11 brotli/1.0.9 zstd/1.5.0 libidn2/2.3.2 libpsl/0.21.1 (+libidn2/2.3.0) libssh2/1.10.0 nghttp2/1.46.0`
  - OK: **wget**: `GNU Wget 1.21.2 built on linux-gnu.`
  - OK: **bash**: `GNU bash, version 5.1.12(1)-release (x86_64-pc-linux-gnu)`
  - OK: **tar**: `tar (GNU tar) 1.34`
  - OK: **sh**: `Ok`
  - OK: **Go**: `go version go1.17.5 linux/amd64`
  - OK: **node**: `v17.2.0`
  - OK: **python3**: `Python 3.9.5`
  - OK: **java**: `Ok`
  - OK: **javac**: `javac 17.0.1`
  - OK: **pip3**: `pip 21.1.1 from /home/binam/.pyenv/versions/3.9.5/lib/python3.9/site-packages/pip (python 3.9)`
  - OK: **npm**: `8.1.4`

OSx

nvim-lsp-installer: require("nvim-lsp-installer.health").check()
========================================================================
## nvim-lsp-installer report
  - OK: neovim version >= 0.6.0
  - WARNING: **Composer**: not available
  - OK: **bash**: `GNU bash, version 5.1.8(1)-release (x86_64-apple-darwin19.6.0)`
  - OK: **tar**: `bsdtar 3.3.2 - libarchive 3.3.2 zlib/1.2.11 liblzma/5.0.5 bz2lib/1.0.6`
  - OK: **curl**: `curl 7.64.1 (x86_64-apple-darwin20.0) libcurl/7.64.1 (SecureTransport) LibreSSL/2.8.3 zlib/1.2.11 nghttp2/1.41.0`
  - OK: **wget**: `GNU Wget 1.21.2 built on darwin20.4.0.`
  - OK: **Ruby**: `ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]`
  - OK: **sh**: `Ok`
  - OK: **gzip**: `Ok`
  - OK: **Go**: `go version go1.17.2 darwin/amd64`
  - OK: **node**: `v17.3.0`
  - OK: **python3**: `Python 3.9.1`
  - OK: **PHP**: `WARNING: PHP is not recommended`
  - OK: **RubyGem**: `3.0.3`
  - OK: **pip3**: `pip 20.2.3 from /Users/bkayastha/.pyenv/versions/3.9.1/lib/python3.9/site-packages/pip (python 3.9)`
  - OK: **npm**: `8.3.0`
  - OK: **javac**: `Ok`
  - OK: **java**: `Ok`

Replication Steps

:LspInstall pylsp
:PylspInstall python-lsp-black

Create a temporary python file image

Run the formatter

:lua vim.lsp.buf.formatting()

Expected behavior

Expected (single quotes are replaced with double quotes): image

Actual (single quotes are kept as is): image

Workaround

DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far (In case there is someone out there like who is specifically running neovim, nvim-lspconfig, nvim-lsp-installer, pylsp, python-lsp-black, and black :)

After I understood that PylspInstall wax creating the virtualenv in ~/.local/share/nvim/ I was able to fix my issue by uninstalling yapf and autopep8 specifically:

cd ~/.local/share/nvim/lsp_servers/pylsp/venv/bin
./pip3 uninstall yapf autopep8

It started working after that.

Possible Solutions

These are just off the top of my head, there might be other solutions as well. I also don't know what the "right" solution is.

Modify :LspInstall pylsp to not install with [all] extras.

python-lsp-server[all] would become python-lsp-server :heavy_plus_sign: The black formatter plugin won't conflict with the yapf and autopep8 when it's installed :heavy_minus_sign: We'll need to add extra commands that allow the user to install python-lsp-server[yapf] and python-lsp-server[all] :heavy_minus_sign: It won't include the linters and other packages by default (see pip freeze packages below)

Modify PylspInstall to uninstall other formatters when it installs black

:heavy_plus_sign: "It just works" for the user :heavy_plus_sign: It will include packages from [all] like the formatter :heavy_minus_sign: There's no way to "undo" adding the formatter for black. :heavy_minus_sign: You also still run into the issue when one project uses black, but another may not.

Allow PylspInstall to specify which virtualenv to install the plugins

:heavy_plus_sign: Pylsp can be configured to have per project configurations when these projects have virtualenvs. :heavy_plus_sign: Can use the default virtualenv (the current one in .loacl) when one is not provided :heavy_minus_sign: You still own't be able to run black in the default virtualenv :heavy_minus_sign: This is probably more of a feature request an a bugfix

pip freeze info

pip freeze for pip install python-lsp-server[all]

astroid==2.9.3
autopep8==1.6.0
flake8==4.0.1
isort==5.10.1
jedi==0.18.1
lazy-object-proxy==1.7.1
mccabe==0.6.1
parso==0.8.3
platformdirs==2.4.1
pluggy==1.0.0
pycodestyle==2.8.0
pydocstyle==6.1.1
pyflakes==2.4.0
pylint==2.12.2
python-lsp-jsonrpc==1.0.0
python-lsp-server==1.3.3
rope==0.22.0
snowballstemmer==2.2.0
toml==0.10.2
typing-extensions==4.0.1
ujson==5.1.0
wrapt==1.13.3
yapf==0.32.0

pip freeze for pip install python-lsp-server[yapf]

jedi==0.18.1
parso==0.8.3
pluggy==1.0.0
python-lsp-jsonrpc==1.0.0
python-lsp-server==1.3.3
ujson==5.1.0
yapf==0.32.0

pip freeze for pip install python-lsp-server

jedi==0.18.1
parso==0.8.3
pluggy==1.0.0
python-lsp-jsonrpc==1.0.0
python-lsp-server==1.3.3
ujson==5.1.0
williamboman commented 2 years ago

Hello! Thanks for the detailed report with suggestions! I know pylsp allows toggling certain behavior by providing your own configuration (they've got it documented here).

I see they also provide a JSON schema for this configuration, so I just pushed a commit that incorporates it into the installer UI, so you can explore it from within neovim as well:

Screenshot 2022-01-12 at 14 06 03

I wonder if you'll be able to do what you want to by just changing some configurations, for example setting

lsp_installer.on_server_ready(function (server)
  local opts = {}
  if server.name == "pylsp" then
    opts.settings = {
        pylsp = {
           plugins = {
              pycodestyle = { enabled = false },
              yapf = { enabled = false }
           }
        }
    }
  end
  server:setup(opts)
end)
binamkayastha commented 2 years ago

Oh my gosh yes it works! That's a much better solution 😅 I was trying to do that earlier but I missed the "enabled" 🤦 . I was doing yapf = false instead of yapf = {enabled = false} fyi - I only needed to disable yapf and didn't need to touch pycodestyle.

The UI is awesome~ You're the best, thank you so much!

nyngwang commented 2 years ago

@binamkayastha May I ask you that if I want this behaviour that is described in the link (with image): https://github.com/hrsh7th/nvim-cmp/issues/823 Then which option should I enable?

NOTE: while the link it's about pyright but the same problem happen in pylsp.