fix(py_venv): patch bazel-runfiles root detection#1209
Open
jimmyt857 wants to merge 1 commit into
Open
Conversation
py_binary startup benchmark
sys.path quality
✅ No regression detected (PR is +2.0% vs HEAD main) |
22bc0d1 to
8a9c80b
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When a
py_venvtarget imports the PyPIbazel-runfilespackage from a uv-installed wheel,bazel-runfilesderives its Python runfiles root by walking upward fromrunfiles/runfiles.py. That assumption is correct for the copy vendored underrules_python/python/runfiles, but not for a wheel installed under awhl_installrepository. In the wheel case, the derived root points inside the wheel install tree, not at Bazel's runfiles root.That breaks repository mapping and
Rlocation()calls that rely onCurrentRepository(), unless every caller passes an explicitsource_repo. Under Bazel, the launcher already exportsRUNFILES_DIRorRUNFILES_MANIFEST_FILE, so those environment variables are the authoritative runfiles root.This change extends the
_virtualenv.pystartup shim to patchrunfiles.runfiles._FindPythonRunfilesRootwhen Bazel runfiles env is present. The patch is lazy: ifrunfiles.runfilesis already imported, it patches the module immediately; otherwise it installs an import hook and patches the module when it is imported.Because
_virtualenv.pyis included in several OCI layer snapshot fixtures, this also refreshes those snapshot listings for the expected_virtualenv.pybyte-size change.Regression Test
Adds
e2e/cases/uv-bazel-runfiles-root, a standalone uv project depending onbazel-runfiles==1.1.0. Before the runtime patch, the test fails because_FindPythonRunfilesRoot()returns a path inside the wheel install tree instead of$RUNFILES_DIR. After the patch, the test also verifies thatRlocation()can find a runfile without passing an explicitsource_repo.Testing
(cd e2e && bazel test //cases/uv-bazel-runfiles-root:test_runfiles_root --test_output=errors)failed withAssertionErroron the runfiles-root assertion.bazel run //:gazellepre-commit run buildifier --files py/private/py_venv/_virtualenv.py e2e/MODULE.bazel e2e/cases/uv-bazel-runfiles-root/BUILD.bazel e2e/cases/uv-bazel-runfiles-root/setup.MODULE.bazelpre-commit run prettier --files py/private/py_venv/_virtualenv.py e2e/MODULE.bazel e2e/cases/uv-bazel-runfiles-root/BUILD.bazel e2e/cases/uv-bazel-runfiles-root/pyproject.toml e2e/cases/uv-bazel-runfiles-root/setup.MODULE.bazel e2e/cases/uv-bazel-runfiles-root/test_runfiles_root.py e2e/cases/uv-bazel-runfiles-root/uv.lock e2e/cases/uv-bazel-runfiles-root/runfiles_root_data.txtpre-commit run typos --files py/private/py_venv/_virtualenv.py e2e/MODULE.bazel e2e/cases/uv-bazel-runfiles-root/BUILD.bazel e2e/cases/uv-bazel-runfiles-root/pyproject.toml e2e/cases/uv-bazel-runfiles-root/setup.MODULE.bazel e2e/cases/uv-bazel-runfiles-root/test_runfiles_root.py e2e/cases/uv-bazel-runfiles-root/uv.lock e2e/cases/uv-bazel-runfiles-root/runfiles_root_data.txtbazel test //py/private/py_venv:all //py/tests/py-internal-venv:all //py/tests/external-deps:all //py/tests/py_venv_conflict:all(cd e2e && bazel test //cases/uv-bazel-runfiles-root:test_runfiles_root --test_output=errors)write_source_fileupdater targets.(cd e2e && bazel test //cases/oci/py_image_layer:my_app_layers_test_test //cases/oci/py_image_layer:my_app_layers_fp_test_test //cases/oci/py_image_layer:my_app_layers_multi_test_test //cases/oci/py_venv_image_layer:my_app_amd64_layers_test //cases/oci/py_venv_image_layer:my_app_arm64_layers_test //cases/uv-deps-650/crossbuild:app_amd64_layers_test //cases/uv-deps-650/crossbuild:app_arm64_layers_test //cases/uv-bazel-runfiles-root:test_runfiles_root --test_output=errors)(cd e2e && USE_BAZEL_VERSION=9.x bazel test //cases/oci/py_image_layer:my_app_layers_test_test //cases/oci/py_image_layer:my_app_layers_fp_test_test //cases/oci/py_image_layer:my_app_layers_multi_test_test //cases/oci/py_venv_image_layer:my_app_amd64_layers_test //cases/oci/py_venv_image_layer:my_app_arm64_layers_test //cases/uv-bazel-runfiles-root:test_runfiles_root --test_tag_filters=-skip-on-bazel9 --test_output=errors)