Skip to content

[python] Fix incorrect VIRTUAL_ENV in pyenv-mode-set-local-version#17246

Merged
bcc32 merged 1 commit intosyl20bnr:developfrom
elvinzeng:fix/python-pyenv-remove-incorrect-virtual-env
Mar 15, 2026
Merged

[python] Fix incorrect VIRTUAL_ENV in pyenv-mode-set-local-version#17246
bcc32 merged 1 commit intosyl20bnr:developfrom
elvinzeng:fix/python-pyenv-remove-incorrect-virtual-env

Conversation

@elvinzeng
Copy link
Contributor

Summary

  • Remove erroneous (setenv "VIRTUAL_ENV" version) from
    spacemacs//pyenv-mode-set-local-version
  • This line set VIRTUAL_ENV to a bare version string (e.g. "3.13.2")
    instead of a filesystem path, breaking tools like Poetry that expect
    VIRTUAL_ENV to be a valid virtualenv directory path
  • The call is also unnecessary — pyenv-mode-set already handles
    environment setup internally via pythonic-activate and PYENV_VERSION

Context

Introduced in e699763 to help LSP detect pyenv changes, but the value
was always wrong (version name instead of path). In pyenv + Poetry
workflows, Poetry reads VIRTUAL_ENV and interprets the version string
as a path, causing errors. Whether the bug manifests depends on hook
execution order — if Poetry's pyvenv-activate runs after the pyenv
hook, it overwrites the bad value and masks the issue.

What I tested

  • In a pyenv + Poetry project, confirmed that VIRTUAL_ENV was being
    set to a bare version string (e.g. "3.13.2") by this function
  • After removing this line, poetry env info in the Spacemacs shell
    outputs the correct virtualenv path
  • Verified by reading source code that pyenv-mode-set already
    correctly handles environment setup (PYENV_VERSION via setenv,
    python-shell-virtualenv-root via pythonic-activate)

Not tested

  • LSP (pylsp/pyright) behavior after the change — though pyenv-mode-set
    still sets PYENV_VERSION and python-shell-virtualenv-root, which
    LSP servers should use
  • pyenv-virtualenv scenarios — though the removed line was equally
    incorrect for those users (setting a name string, not a path)

Remove the erroneous `(setenv "VIRTUAL_ENV" version)` call from
`spacemacs//pyenv-mode-set-local-version`.

This line was introduced in commit e699763 to help Python LSP servers
detect pyenv version changes. However, the implementation was incorrect:
it set `VIRTUAL_ENV` to a bare version string (e.g. "3.13.2") instead
of a filesystem path. The `VIRTUAL_ENV` environment variable, by
convention, should contain the absolute path to a virtualenv directory.

This causes issues in pyenv + Poetry workflows: Poetry reads
`VIRTUAL_ENV` and interprets the version string as a virtualenv path,
leading to errors. The severity depends on hook execution order — if
`spacemacs//python-setup-backend` (which calls `poetry-venv-workon` →
`pyvenv-activate`) runs after the pyenv hook, the bogus value gets
overwritten and the bug is masked; otherwise Poetry breaks.

The `setenv` call is also unnecessary. `pyenv-mode-set` already handles
all required environment setup internally:
- calls `pythonic-activate` with the full path from `pyenv-mode-full-path`
  to set `python-shell-virtualenv-root`
- sets `PYENV_VERSION` to the version string

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bcc32
Copy link
Collaborator

bcc32 commented Mar 15, 2026

Thanks for your contribution.

@bcc32 bcc32 merged commit e70ef0a into syl20bnr:develop Mar 15, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants