Skip to content

🐛 fix(activation): undo a live activation before activate.bat saves values - #3245

Merged
gaborbernat merged 2 commits into
pypa:mainfrom
darrenhuai:fix/batch-reactivation-restore
Sep 17, 2026
Merged

gaborbernat merged 2 commits into
pypa:mainfrom
darrenhuai:fix/batch-reactivation-restore

Conversation

@darrenhuai

@darrenhuai darrenhuai commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Running activate.bat a second time without deactivating in between (the same environment again, or switching to another one) makes deactivate restore the wrong values.

activate.bat saves TCL_LIBRARY, TK_LIBRARY and PKG_CONFIG_PATH every time it runs. Unlike PATH and PYTHONHOME, it never checks whether an activation is already in effect, so the second run saves the first activation's values as the ones to put back. On current main:

  • after activate, activate, deactivate with PKG_CONFIG_PATH unset, it's left as <venv>\lib\pkgconfig, and while active it holds one copy of that entry per activation
  • activating an environment with tcl, then another one, then deactivating leaves the first environment's TCL_LIBRARY behind
  • with TCL_LIBRARY=C:\mine set beforehand, activating a tcl environment twice and deactivating leaves the environment's tcl path, and C:\mine is gone

The bash and PowerShell scripts avoid this by running deactivate before activating. This does the same in batch, but only when _OLD_VIRTUAL_PATH shows a previous activation is still live, because deactivate.bat clears any variable it has no saved copy of. PowerShell keeps its _OLD_* values as PowerShell variables and bash doesn't export them, so they can't leak into a cmd started from those shells and trigger this by mistake.

test_batch now also runs every existing combination (prompt, tcl, user values set or unset) with the activation done twice. Putting the old activate.bat back fails all 8 of the new cases; dropping the _OLD_VIRTUAL_PATH guard fails the 8 cases where the user already had values set. I also checked by hand that the prompt isn't doubled, nothing extra is echoed, and an environment path with spaces and parentheses works.

  • ran the linter to address style issues (tox -e fix)
  • wrote descriptive pull request text
  • ensured there are test(s) validating the fix
  • added news fragment in docs/changelog folder
  • updated/extended the documentation

…alues

activate.bat saves TCL_LIBRARY, TK_LIBRARY and PKG_CONFIG_PATH before
replacing them, but unlike PATH and PYTHONHOME it never checks whether an
activation is already in effect. Running it a second time without
deactivating (the same environment again, or switching to another one)
saved the first activation's values as the ones to restore. deactivate
then left the environment's pkgconfig directory in PKG_CONFIG_PATH and
its tcl/tk paths in TCL_LIBRARY/TK_LIBRARY, and a TCL_LIBRARY the user had
set beforehand was lost. While active, PKG_CONFIG_PATH also collected one
pkgconfig entry per activation.

The bash and PowerShell scripts avoid this by running deactivate first.
Do the same in batch, but only when _OLD_VIRTUAL_PATH shows a previous
activation is still live, since deactivate.bat clears any variable it has
no saved copy of. The batch activation test now also runs activating
twice before deactivating.
@gaborbernat
gaborbernat merged commit 469dd28 into pypa:main Sep 17, 2026
62 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