Skip to content

Commit

Permalink
fix ale_python_auto_virtualenv to correctly set virtualenv env vars (#…
Browse files Browse the repository at this point in the history
…4885)

* fix ale_python_auto_virtualenv to correctly set virtualenv env vars

According to the documentation, `ale_python_auto_virtualenv` should automatically set environment variables for commands, but previously the variables were not set completely or correctly.

Before:
  `PATH` variable was expanded to include `/path/to/venv`
After:
  `PATH` variable is expanded to include `/path/to/venv/bin`
  `VIRTUAL_ENV` variable is set to `path/to/venv`

This mimics exactly what the `activate` scripts do, and allows the configuration knob to work as expected.

For example, after this change, `jedi-language-server` can be installed globally (instead of inside every venv), and it will "just work" (e.g. find references to dependencies in the venv) when editing a file in a project that uses a venv, because the correct variables are set.

* fix test_python_virtualenv.vader test to expect output with both virtualenv vars

* remove unnecessary non-escape in test_python_virtualenv.vader

* fix accidentally removed space in windows test_python_virtualenv.vader
  • Loading branch information
rmuir authored Dec 29, 2024
1 parent 6e4f01f commit 5b2e69a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions autoload/ale/python.vim
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,17 @@ function! ale#python#AutoVirtualenvEnvString(buffer) abort

if !empty(l:venv_dir)
let l:strs = [ ]
" venv/bin directory
let l:pathdir = join([l:venv_dir, s:bin_dir], s:sep)

" expand PATH correctly inside of the appropriate shell.
" set VIRTUAL_ENV to point to venv
if has('win32')
call add(l:strs, 'set PATH=' . ale#Escape(l:venv_dir) . ';%PATH% && ')
call add(l:strs, 'set PATH=' . ale#Escape(l:pathdir) . ';%PATH% && ')
call add(l:strs, 'set VIRTUAL_ENV=' . ale#Escape(l:venv_dir) . ' && ')
else
call add(l:strs, 'PATH=' . ale#Escape(l:venv_dir) . '":$PATH" ')
call add(l:strs, 'PATH=' . ale#Escape(l:pathdir) . '":$PATH" ')
call add(l:strs, 'VIRTUAL_ENV=' . ale#Escape(l:venv_dir) . ' ')
endif

return join(l:strs, '')
Expand Down
4 changes: 2 additions & 2 deletions test/test_python_virtualenv.vader
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Execute(ale#python#FindVirtualenv falls back to $VIRTUAL_ENV when no directories
Execute(ale#python#AutoVirtualenvEnvString should return the correct values):
if has('win32')
AssertEqual
\ 'set PATH=/opt/example/;%PATH% && ',
\ 'set PATH=/opt/example/\Scripts;%PATH% && set VIRTUAL_ENV=/opt/example/ && ',
\ ale#python#AutoVirtualenvEnvString(bufnr(''))
else
AssertEqual
\ 'PATH=''/opt/example/''":$PATH" ',
\ 'PATH=''/opt/example//bin''":$PATH" VIRTUAL_ENV=''/opt/example/'' ',
\ ale#python#AutoVirtualenvEnvString(bufnr(''))
endif

0 comments on commit 5b2e69a

Please sign in to comment.