williamboman / mason.nvim

Portable package manager for Neovim that runs everywhere Neovim runs. Easily install and manage LSP servers, DAP servers, linters, and formatters.
Apache License 2.0
7.74k stars 272 forks source link

No autocomplete etc. for external libs #1276

Closed paulfrische closed 1 year ago

paulfrische commented 1 year ago

I've searched open issues for similar requests

I've manually reviewed logs to find potential errors

I've recently downloaded the latest plugin version of mason.nvim

Problem description

When installing pylsp via mason it doesn't recognize external dependencies/libraries you use because mason installs it in a venv.

Expected behavior

Code intelligence for external libs too.

Steps to reproduce

  1. install pylsp
  2. install any external lib like pygame
  3. don't get any support for the external lib

Affected packages

python-lsp-server

Neovim version (>= 0.7)

NVIM v0.10.0-dev-24+g3c724fe1f Build type: RelWithDebInfo LuaJIT 2.1.0-beta3

Operating system/version

Linux archlinux 6.2.12-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 20 Apr 2023 16:11:55 +0000 x86_64 GNU/Linux

Healthcheck

==============================================================================
mason: require("mason.health").check()

mason.nvim ~
- OK mason.nvim version v1.0.1
- OK neovim version >= 0.7.0

mason.nvim [Registries] ~
- OK Registry `github.com/mason-org/mason-registry version: 2023-04-29-famous-doll` is installed.
- OK Registry `github.com/mason-org/mason-registry version: 2023-04-29-famous-doll` is installed.

mason.nvim [Core utils] ~
- OK unzip: `UnZip 6.00 of 20 April 2009, by Info-ZIP.  Maintained by C. Spieler.  Send`
- OK wget: `GNU Wget 1.21.3 built on linux-gnu.`
- OK curl: `curl 8.0.1 (x86_64-pc-linux-gnu) libcurl/8.0.1 OpenSSL/3.0.8 zlib/1.2.13 brotli/1.0.9 zstd/1.5.5 libidn2/2.3.4 libpsl/0.21.2 (+libidn2/2.3.4) libssh2/1.10.0 nghttp2/1.52.0`
- OK gzip: `gzip 1.12`
- OK tar: `tar (GNU tar) 1.34`
- OK bash: `GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)`
- OK sh: `Ok`

mason.nvim [Languages] ~
- WARNING luarocks: not available
  - ADVICE:
    - spawn: luarocks failed with exit code - and signal -. luarocks is not executable
- WARNING Composer: not available
  - ADVICE:
    - spawn: composer failed with exit code - and signal -. composer is not executable
- OK Go: `go version go1.20.3 linux/amd64`
- WARNING npm: not available
  - ADVICE:
    - spawn: npm failed with exit code - and signal -. npm is not executable
- WARNING node: not available
  - ADVICE:
    - spawn: node failed with exit code - and signal -. node is not executable
- OK Ruby: `ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c5) [x86_64-linux]`
- OK cargo: `cargo 1.67.1 (8ecd4f20a 2023-01-10)`
- WARNING julia: not available
  - ADVICE:
    - spawn: julia failed with exit code - and signal -. julia is not executable
- OK python3: `Python 3.10.10`
- OK PHP: `PHP 8.2.5 (cli) (built: Apr 12 2023 15:55:38) (NTS)`
- OK java: `openjdk version "17.0.7" 2023-04-18`
- OK pip3: `pip 23.0.1 from /usr/lib/python3.10/site-packages/pip (python 3.10)`
- OK javac: `javac 17.0.7`
- OK RubyGem: `3.3.25`

mason.nvim [GitHub] ~
- OK GitHub API rate limit. Used: 0. Remaining: 5000. Limit: 5000. Reset: Sat 29 Apr 2023 10:20:58 AM CEST.

Screenshots or recordings

No response

williamboman commented 1 year ago

Hello! Have you tried setting the pyls.plugins.jedi.environment setting? I believe setting it to your project (or global) Python installation should fix things. See for example:

paulfrische commented 1 year ago

Hey thx, this solved my issue. wanna close this thread Have a Great Day!

AMohamedFawzy commented 1 year ago

@paulfrische Could you please elaborate more on how exactly did you fix it, i am having a hard time with this problem

paulfrische commented 1 year ago
  lspconfig.pylsp.setup({ 
     on_attach = M.attach, 
     settings = { 
       pylsp = { 
         plugins = { 
           jedi = { 
             environment = python, 
           } 
         } 
       } 
     } 
   })

EDIT: python is the path to the python Interpreter

AMohamedFawzy commented 1 year ago
local lspconfig = require('lspconfig')
lspconfig.pylsp.setup({
    settings = {
        pylsp = {
            plugins = {
                jedi = {
                    environment = "/usr/bin/python"
                }
            }
        }
    }
})

@paulfrische This is my current config and for some reason it is still using the python interpreter from the Mason venv, do you have any idea why?

paulfrische commented 1 year ago

Maybe you call lspconfig.pylsp.setup twice accidentally

12bchl commented 1 year ago

Can someone tell me where I'm going wrong? Same issue as OP

-- Mason
local servers = {}
servers['pylsp'] = {
  pylsp = {
    plugins = {
      jedi = {
        environment = "/usr/bin/python3",
      }
    }
  }
}

-- nvim-cmp supports additional completion capabilities, so broadcast that to servers
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)

-- Ensure the servers above are installed
local mason_lspconfig = require 'mason-lspconfig'
mason_lspconfig.setup {
  ensure_installed = vim.tbl_keys(servers),
}
...
mason_lspconfig.setup_handlers {
  function(server_name)
    require('lspconfig')[server_name].setup {
      capabilities = capabilities,
      on_attach = attachLSP,
      settings = servers[server_name],
    }
  end,
}
mmoya commented 8 months ago

The problem with setting a fixed path like /usr/bin/python is that completion won't work for projects using custom virtualenvs. The solution is to get the interpreter path dynamically by running env which python.

Sharing my solution here:

local lspconfig = require "lspconfig"
local on_attach = require("plugins.configs.lspconfig").on_attach

local function which_python()
  local f = io.popen('env which python', 'r') or error("Fail to execute 'env which python'")
  local s = f:read('*a') or error("Fail to read from io.popen result")
  f:close()
  return string.gsub(s, '%s+$', '')
end

lspconfig['pylsp'].setup {
  on_attach = on_attach,
  settings = {
    pylsp = {
      plugins = {
        jedi = { environment = which_python() },
      }
    }
  }
}

This still requires to run . $venv/bin/activate before opening nvim. A better integration will be to detect pdm, poetry, etc... and call the appropriate subcommand to get the project virtualenv.