Skip to content

Replace activation script regexes with literal path swap - #40

Open
danielpops wants to merge 2 commits into
mainfrom
fix_activation_scripts_literal_path
Open

danielpops wants to merge 2 commits into
mainfrom
fix_activation_scripts_literal_path

Conversation

@danielpops

@danielpops danielpops commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Summary

virtualenv 21.14.9 changed the bash activate template again (the first if now spans two lines), so the regexes from #36 stop matching and leave the old path behind. A relocated venv then warns on every bash source and fails to activate from zsh:

Relocating this activation script requires sourcing it from bash.

Instead of matching each template line, swap every whole-path occurrence of the original path, which get_orig_path already finds:

 update_scripts(bin_dir, orig_path, new_path, activation=True)
-  update_activation_script(path, new_path)
-    for each line: try _activation_path_re / _bash_if_check_re /
-                   _bash_echo_path_re / _bash_virtual_env_re
+  update_activation_script(path, orig_path, new_path)
+    replace orig_path wherever it appears as a whole path
+    ("/venv" leaves "/venv2", "/venv.bak", "/x/venv" alone)

Also:

  • adds activate.nu to ACTIVATION_SCRIPTS (it was never rewritten)
  • adds a weekly scheduled CI run. The existing tests already fail on current virtualenv, but CI hadn't run since July, so nobody saw it.
  • fixes CI for ubuntu-latest now being 26.04, which has no setup-python 3.8/3.9 builds: each matrix entry picks its runner (3.8/3.9 on 24.04), fail-fast: false, checkout/setup-python bumped to v4/v5 (same approach as Fix test matrix for ubuntu-26.04 runners docker-custodian#73)

Evidence

Relocating a venv built with virtualenv 21.14.12:

  • Before (3.1.2): activate, activate.csh, activate.xsh, activate.nu still reference the old path. bash warns Virtual environment directory <old> does not exist!, zsh exits 1.
    After: 0 files reference the old path. bash, zsh and sh all activate cleanly. A second run reports Already up-to-date.

Test suite:

virtualenv main this branch
20.35.4 not run 26 passed, 1 skipped
20.36.1 not run 26 passed, 1 skipped
21.14.8 not run 27 passed
21.15.5 3 failed (test_verbose, test_update_activation_script[activate], [activate.csh]) 27 passed

New regression test, which fails on main against 21.15.5:

test_move_leaves_no_reference_to_old_path
  relocate venv, delete old dir
  assert old path not in any activate script
  assert sourcing bin/activate writes nothing to stderr

Merge Danger

Door: two-way

Merging is cheap to revert. Publishing to PyPI is not (versions are immutable), so this PR doesn't bump the version.

Blast Radius: deploys

Every relocated venv's activate scripts go through this path. update_activation_script now takes orig_path. Only update_scripts calls it internally, but direct importers would break. Not manually tested: activating relocated venvs in fish, csh, nu or xonsh (I only verified their scripts no longer contain the old path).

✨ Created with help from Clanker Claude

virtualenv 21.14.9 restructured the bash activate template again, so the
per-line regexes added for 20.36.0 left the old path in three places.
Relocated venvs then warn on every bash activation and fail to activate
from zsh. activate.csh, activate.xsh and activate.nu were also missing
updates.

Since get_orig_path already finds the original path, replace every
whole-path occurrence of it instead of matching template lines. Also
cover activate.nu, add a regression test that no activate script keeps
the old path, and run CI weekly so new virtualenv releases are caught.

Co-Authored-By: Clanker Claude <dpopes@yelp.com>
@danielpops
danielpops requested a review from nurdann November 6, 2026 00:09
@danielpops

Copy link
Copy Markdown
Member Author

@nurdann this might be a fix in case issues are caused by the import of virtualenv

ubuntu-latest now resolves to ubuntu-26.04, which has no setup-python
builds for 3.8/3.9. Pin each matrix entry to a runner that has its
version, and disable fail-fast so one broken entry doesn't cancel the
rest. Bump checkout and setup-python to v4/v5.

Drop the exists() branch from the no-old-path test by globbing
activate*, and mark the old-virtualenv skip as no cover.

Co-Authored-By: Clanker Claude <dpopes@yelp.com>
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