From bc957015752bce79389e0092998a7b920d277670 Mon Sep 17 00:00:00 2001 From: w0rp Date: Sat, 24 Dec 2022 13:28:08 +0000 Subject: Fix #4388: Fix pylsp and Pyright cwd Add functions to compute the cwd to be the same as the project root for pylsp and Pyright to work around issues in each language server when they encounter modules that share the same name as first or third party libraries. --- ale_linters/python/pylsp.vim | 14 ++++++++++++++ ale_linters/python/pyright.vim | 14 ++++++++++++++ test/linter/test_pylsp.vader | 8 ++++++++ test/linter/test_pyre.vader | 2 ++ test/linter/test_pyright.vader | 8 ++++++++ 5 files changed, 46 insertions(+) diff --git a/ale_linters/python/pylsp.vim b/ale_linters/python/pylsp.vim index 537d1e74..a699e4f6 100644 --- a/ale_linters/python/pylsp.vim +++ b/ale_linters/python/pylsp.vim @@ -22,6 +22,19 @@ function! ale_linters#python#pylsp#GetExecutable(buffer) abort return ale#python#FindExecutable(a:buffer, 'python_pylsp', ['pylsp']) endfunction +" Force the cwd of the server to be the same as the project root to +" fix issues with treating local files matching first or third party library +" names being imported incorrectly. +function! ale_linters#python#pylsp#GetCwd(buffer) abort + let l:fake_linter = { + \ 'name': 'pylsp', + \ 'project_root': function('ale#python#FindProjectRoot'), + \} + let l:root = ale#lsp_linter#FindProjectRoot(a:buffer, l:fake_linter) + + return !empty(l:root) ? l:root : v:null +endfunction + function! ale_linters#python#pylsp#GetCommand(buffer) abort let l:executable = ale_linters#python#pylsp#GetExecutable(a:buffer) @@ -36,6 +49,7 @@ call ale#linter#Define('python', { \ 'name': 'pylsp', \ 'lsp': 'stdio', \ 'executable': function('ale_linters#python#pylsp#GetExecutable'), +\ 'cwd': function('ale_linters#python#pylsp#GetCwd'), \ 'command': function('ale_linters#python#pylsp#GetCommand'), \ 'project_root': function('ale#python#FindProjectRoot'), \ 'completion_filter': 'ale#completion#python#CompletionItemFilter', diff --git a/ale_linters/python/pyright.vim b/ale_linters/python/pyright.vim index 422ecd61..5ce46600 100644 --- a/ale_linters/python/pyright.vim +++ b/ale_linters/python/pyright.vim @@ -32,10 +32,24 @@ function! ale_linters#python#pyright#GetConfig(buffer) abort return l:config endfunction +" Force the cwd of the server to be the same as the project root to +" fix issues with treating local files matching first or third party library +" names being imported incorrectly. +function! ale_linters#python#pyright#GetCwd(buffer) abort + let l:fake_linter = { + \ 'name': 'pyright', + \ 'project_root': function('ale#python#FindProjectRoot'), + \} + let l:root = ale#lsp_linter#FindProjectRoot(a:buffer, l:fake_linter) + + return !empty(l:root) ? l:root : v:null +endfunction + call ale#linter#Define('python', { \ 'name': 'pyright', \ 'lsp': 'stdio', \ 'executable': {b -> ale#Var(b, 'python_pyright_executable')}, +\ 'cwd': function('ale_linters#python#pyright#GetCwd'), \ 'command': '%e --stdio', \ 'project_root': function('ale#python#FindProjectRoot'), \ 'completion_filter': 'ale#completion#python#CompletionItemFilter', diff --git a/test/linter/test_pylsp.vader b/test/linter/test_pylsp.vader index 290fdbf3..34cc30c6 100644 --- a/test/linter/test_pylsp.vader +++ b/test/linter/test_pylsp.vader @@ -10,6 +10,8 @@ After: call ale#assert#TearDownLinterTest() Execute(The pylsp command callback should return default string): + call ale#test#SetFilename('./foo.py') + AssertLinter 'pylsp', ale#Escape('pylsp') Execute(The pylsp executable should be configurable): @@ -22,6 +24,12 @@ Execute(The pylsp command callback should let you set options): AssertLinter 'pylsp', ale#Escape('pylsp') . ' --some-option' +Execute(The cwd and project root should be detected correctly): + call ale#test#SetFilename('../test-files/python/with_virtualenv/subdir/foo/bar.py') + + AssertLinterCwd ale#test#GetFilename('../test-files/python/with_virtualenv/subdir') + AssertLSPProject ale#test#GetFilename('../test-files/python/with_virtualenv/subdir') + Execute(The pylsp executable should be run from the virtualenv path): call ale#test#SetFilename('../test-files/python/with_virtualenv/subdir/foo/bar.py') diff --git a/test/linter/test_pyre.vader b/test/linter/test_pyre.vader index 331d97ed..053ef12f 100644 --- a/test/linter/test_pyre.vader +++ b/test/linter/test_pyre.vader @@ -8,6 +8,8 @@ After: call ale#assert#TearDownLinterTest() Execute(The pyre command callback should return default string): + call ale#test#SetFilename('./foo.py') + AssertLinter 'pyre', ale#Escape('pyre') . ' persistent' Execute(The pyre executable should be configurable): diff --git a/test/linter/test_pyright.vader b/test/linter/test_pyright.vader index 95dfd853..14e8c142 100644 --- a/test/linter/test_pyright.vader +++ b/test/linter/test_pyright.vader @@ -10,6 +10,8 @@ After: call ale#assert#TearDownLinterTest() Execute(The command callback should return the correct default string): + call ale#test#SetFilename('./foo.py') + AssertLinter \ 'pyright-langserver', \ ale#Escape('pyright-langserver') . ' --stdio' @@ -30,6 +32,12 @@ Execute(The default configuration should be mostly empty): AssertLSPConfig {'python': {}} +Execute(The cwd and project root should be detected correctly): + call ale#test#SetFilename('../test-files/python/with_virtualenv/subdir/foo/bar.py') + + AssertLinterCwd ale#test#GetFilename('../test-files/python/with_virtualenv/subdir') + AssertLSPProject ale#test#GetFilename('../test-files/python/with_virtualenv/subdir') + Execute(virtualenv paths should be set in configuration by default): call ale#test#SetFilename('../test-files/python/with_virtualenv/subdir/foo/bar.py') -- cgit v1.2.3