python-lsp / python-lsp-server

Fork of the python-language-server project, maintained by the Spyder IDE team and the community
MIT License
1.97k stars 196 forks source link

Getting errors if not in project directory #336

Open matthewsia98 opened 1 year ago

matthewsia98 commented 1 year ago

In neovim, I am not getting any diagnostics if pylsp is running in single file mode. I do get diagnostics if opening a file under a git repository.

Seems like auto import doesn't work in non project files too

[ERROR][2023-01-14 20:40:20] .../vim/lsp/rpc.lua:734    "rpc"   "pylsp" "stderr"    '2023-01-14 20:40:20,644 EST - WARNING - pylsp.config.config - Failed to load hook pylsp_document_did_open: 
Traceback (most recent call last):
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp/config/config.py", line 33, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 327, in traced_hookexec
    return outcome.get_result()
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_result.py", line 33, in from_call
    result = func()
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 324, in <lambda>
    lambda: oldcall(hook_name, hook_impls, kwargs, firstresult)
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp/plugins/rope_autoimport.py", line 221, in pylsp_document_did_open
    _reload_cache(config, workspace)
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp/plugins/rope_autoimport.py", line 197, in _reload_cache
    autoimport = workspace._rope_autoimport(rope_config, memory)
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp/workspace.py", line 64, in _rope_autoimport
    self.__rope_autoimport = AutoImport(project, memory=memory)
  File "/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/rope/contrib/autoimport/sqlite.py", line 98, in __init__
    assert project_package is not None
AssertionError
'

[ERROR][2023-01-14 19:52:20] .../vim/lsp/rpc.lua:734    "rpc"   "pylsp" "stderr"    "2023-01-14 19:52:20,317 EST - WARNING - pylsp.config.config - Failed to load hook pylsp_lint: [Errno 2] No such file or directory: ''
Traceback (most recent call last):
  File \"/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp/plugins/flake8_lint.py\", line 108, in run_flake8
    p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=document._workspace.root_path)
  File \"/usr/lib/python3.10/subprocess.py\", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File \"/usr/lib/python3.10/subprocess.py\", line 1847, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File \"/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp/config/config.py\", line 33, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File \"/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_manager.py\", line 327, in traced_hookexec
    return outcome.get_result()
  File \"/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_result.py\", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File \"/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_result.py\", line 33, in from_call
    result = func()
  File \"/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_manager.py\", line 324, in <lambda>
    lambda: oldcall(hook_name, hook_impls, kwargs, firstresult)
  File \"/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_callers.py\", line 60, in _multicall
    return outcome.get_result()
  File \"/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_result.py\", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File \"/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pluggy/_callers.py\", line 39, in _multicall
    res = hook_impl.function(*args)
  File \"/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp/plugins/flake8_lint.py\", line 83, in pylsp_lint
    output = run_flake8(flake8_executable, args, document)
  File \"/home/msia/.local/share/nvim/mason/packages/python-lsp-server/venv/lib/python3.10/site-packages/pylsp/plugins/flake8_lint.py\", line 113, in run_flake8
    p = Popen(  # pylint: disable=consider-using-with
  File \"/usr/lib/python3.10/subprocess.py\", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File \"/usr/lib/python3.10/subprocess.py\", line 1847, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: ''
"
matthewsia98 commented 1 year ago

It seems to be failing when root directory is my home directory

rstcruzo commented 1 year ago

I get this error every time the directory I'm in is not a git repository. Can we reopen this?

matthewsia98 commented 1 year ago

Try this

local lspconfig = require("lspconfig")
local util = require("lspconfig.util")
lspconfig.pylsp.setup({
      root_dir = function(fname)
          local root_files = {
              "pyproject.toml",
              "setup.py",
              "setup.cfg",
              "requirements.txt",
              "Pipfile",
          }
          return util.root_pattern(unpack(root_files))(fname)
              or util.find_git_ancestor(fname)
              or vim.fn.expand("%:p:h")
      end,