Repository navigation
fix: recover cached venvs from dangling package symlinks #4318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rickeylev
merged 7 commits into
bazel-contrib:main
from
sean0407:fix/rules-python-4269-dangling-venv
Oct 25, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7b6e573
fix: recover cached venvs from dangling package symlinks
sean0407 0721065
Merge branch 'main' into fix/rules-python-4269-dangling-venv
aignas f632a50
test: set explicit main for dangling venv test
sean0407 3d94855
test: format dangling venv test target
e6da0b7
test: follow buildifier attribute order
b88ba42
test: narrow runfile template path type
sean0407 ad0a8b9
fix: explain dangling cached venv symlink
sean0407 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
tests/bootstrap_impls/cached_venv_dangling_symlink_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """Regression test for dangling runtime package links in persistent venvs.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import pathlib | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| from python.runfiles import runfiles | ||
|
|
||
|
|
||
| def _load_bootstrap() -> dict: | ||
| r = runfiles.Create() | ||
| if r: | ||
| template_path = r.Rlocation( | ||
| "rules_python/python/private/python_bootstrap_template.txt" | ||
| ) | ||
| assert template_path is not None | ||
| template = pathlib.Path(template_path) | ||
| else: | ||
| template = ( | ||
| pathlib.Path(__file__).resolve().parents[2] | ||
| / "python/private/python_bootstrap_template.txt" | ||
| ) | ||
| source = template.read_text(encoding="utf-8") | ||
| for placeholder, value in [ | ||
| ("%shebang%", "#!/usr/bin/env python3"), | ||
| ("%stage2_bootstrap%", "unused.py"), | ||
| ("%runtime_venv_symlinks%", ""), | ||
| ]: | ||
| source = source.replace(placeholder, value) | ||
|
|
||
| bootstrap = {"__name__": "bootstrap_test"} | ||
| saved_path = sys.path[:] | ||
| try: | ||
| exec(compile(source, str(template), "exec"), bootstrap) | ||
| finally: | ||
| sys.path[:] = saved_path | ||
| return bootstrap | ||
|
|
||
|
|
||
| class CachedVenvDanglingSymlinkTest(unittest.TestCase): | ||
| def test_replaces_dangling_package_link_before_creating_child_link(self) -> None: | ||
| bootstrap = _load_bootstrap() | ||
| with tempfile.TemporaryDirectory() as temp: | ||
| root = pathlib.Path(temp) | ||
| runfiles = root / "runfiles" | ||
| old_package = runfiles / "old/annotated_types" | ||
| old_package.mkdir(parents=True) | ||
| (old_package / "__init__.py").touch() | ||
| bootstrap.update( | ||
| EXTRACT_ROOT=str(root / "cache"), | ||
| PYTHON_BINARY="_main/_process.venv/Scripts/python.exe", | ||
| PYTHON_BINARY_ACTUAL=sys.executable, | ||
| RESOLVE_PYTHON_BINARY_AT_RUNTIME=False, | ||
| RUNTIME_VENV_SYMLINKS={ | ||
| "Lib/site-packages/annotated_types": "old/annotated_types", | ||
| }, | ||
| ) | ||
|
|
||
| bootstrap["_create_venv"](str(runfiles), []) | ||
| old_package.rename(root / "retired-package") | ||
|
|
||
| new_package = runfiles / "new/annotated_types" | ||
| new_package.mkdir(parents=True) | ||
| (new_package / "__init__.py").touch() | ||
| bootstrap["RUNTIME_VENV_SYMLINKS"] = { | ||
| "Lib/site-packages/annotated_types/__init__.py": | ||
| "new/annotated_types/__init__.py", | ||
| } | ||
|
|
||
| bootstrap["_create_venv"](str(runfiles), []) | ||
|
|
||
| package = root / "cache/_main/_process.venv/Lib/site-packages/annotated_types" | ||
| package_init = package / "__init__.py" | ||
| self.assertTrue(package.is_dir()) | ||
| self.assertFalse(package.is_symlink()) | ||
| self.assertTrue(package_init.is_symlink()) | ||
| self.assertTrue(os.path.samefile(package_init, new_package / "__init__.py")) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.