Skip to content

Commit 469dd28

Browse files
authored
🐛 fix(activation): undo a live activation before activate.bat saves values (#3245)
1 parent a045a14 commit 469dd28

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎docs/changelog/3245.bugfix.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Running ``activate.bat`` again before ``deactivate`` no longer makes ``deactivate`` leave the environment's
2+
``PKG_CONFIG_PATH``, ``TCL_LIBRARY`` and ``TK_LIBRARY`` behind, or lose values the user had set before the first
3+
activation - by :user:`darrenhuai`.

‎src/virtualenv/activation/batch/activate.bat‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"%SystemRoot%\System32\chcp.com" 65001 > nul
66
)
77

8+
@REM Undo an activation that is still in effect, otherwise its values get saved as the ones to restore
9+
@if defined _OLD_VIRTUAL_PATH @call "__VIRTUAL_ENV__\__BIN_NAME__\deactivate.bat"
10+
811
@set "VIRTUAL_ENV=__VIRTUAL_ENV__"
912

1013
@set "VIRTUAL_ENV_PROMPT=__VIRTUAL_PROMPT__"

‎tests/unit/activation/test_batch.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def __init__(self, dest) -> None:
9292

9393

9494
@pytest.mark.usefixtures("activation_python")
95-
def test_batch(activation_tester_class, activation_tester, tmp_path) -> None:
95+
@pytest.mark.parametrize("activations", [1, 2], ids=["activate_once", "activate_twice"])
96+
def test_batch(activation_tester_class, activation_tester, tmp_path, activations) -> None:
9697
version_script = tmp_path / "version.bat"
9798
version_script.write_text("ver", encoding="utf-8")
9899

@@ -109,6 +110,10 @@ def __init__(self, session) -> None:
109110
def _get_test_lines(self, activate_script):
110111
return ["@echo off", *super()._get_test_lines(activate_script)]
111112

113+
def activate_call(self, script):
114+
# activating again without deactivating must still restore the values from before the first activation
115+
return " & ".join([super().activate_call(script)] * activations)
116+
112117
def quote(self, s):
113118
if '"' in s or " " in s:
114119
text = s.replace('"', r"\"")

0 commit comments

Comments
 (0)