Skip to content

Commit a699063

Browse files
authored
fix(py_venv): Activate embedded interpreters (#576)
Previously the `bin/activate` script relied on Bazel (specifically `bazel run`) defined envvars, or being sourced directly from the binary entrypoint. This meant that if the user created a link (eg via the venv link verb or manually) to the venv defined by a given target, sourcing that venv's activate script would fail to locate a runfiles-based interpreter. On systems where there is no appropriately versioned non-hermetic interpreter available, this meant that venvs which should run with the Bazel-managed interpreter instead failed. The fix is to harden venv initialization so that if the `activate` script is directly used AND the runfiles preconditions are not met an effort will be made to satisfy them. --- ### Changes are visible to end-users: yes - Searched for relevant documentation and updated as needed: no - Breaking change (forces users to change their own code or config): no - Suggested release notes appear below: no ### Test plan - [x] Updated script works on BASH ```shellsession $ /bin/bash $ bazel run //examples/py_binary:py_binary.venv INFO: Analyzed target //examples/py_binary:py_binary.venv (0 packages loaded, 0 targets configured). INFO: Found 1 target... Target //examples/py_binary:py_binary.venv up-to-date: bazel-bin/examples/py_binary/py_binary.venv INFO: Elapsed time: 1.224s, Critical Path: 0.00s INFO: 1 process: 1 internal. INFO: Build completed successfully, 1 total action INFO: Running command line: bazel-bin/examples/py_binary/py_binary.venv Linking: /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/.py_binary.venv -> /Users/arrdem/Documents/work/aspect/rules_py/.py_binary.venv To activate the virtualenv run: source /Users/arrdem/Documents/work/aspect/rules_py/.py_binary.venv/bin/activate Link is up to date! $ ( source .py_binary.venv/bin/activate; echo $VIRTUAL_ENV; which python; which python3; echo $PYTHONHOME; deactivate; echo $VIRTUAL_ENV; which python; which python3 ) /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/.py_binary.venv /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/.py_binary.venv/bin/python /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/.py_binary.venv/bin/python3 /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/external/python_toolchain_aarch64-apple-darwin /Users/arrdem/.local/pyenv/shims/python /Users/arrdem/.local/pyenv/shims/python3 ``` - [x] Updated script works on ZSH ```shellsession $ /bin/zsh $ bazel run //examples/py_binary:py_binary.venv INFO: Analyzed target //examples/py_binary:py_binary.venv (0 packages loaded, 0 targets configured). INFO: Found 1 target... Target //examples/py_binary:py_binary.venv up-to-date: bazel-bin/examples/py_binary/py_binary.venv INFO: Elapsed time: 1.224s, Critical Path: 0.00s INFO: 1 process: 1 internal. INFO: Build completed successfully, 1 total action INFO: Running command line: bazel-bin/examples/py_binary/py_binary.venv Linking: /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/.py_binary.venv -> /Users/arrdem/Documents/work/aspect/rules_py/.py_binary.venv To activate the virtualenv run: source /Users/arrdem/Documents/work/aspect/rules_py/.py_binary.venv/bin/activate Link is up to date! $ ( source .py_binary.venv/bin/activate; echo $VIRTUAL_ENV; which python; which python3; echo $PYTHONHOME; deactivate; echo $VIRTUAL_ENV; which python; which python3 ) /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/.py_binary.venv /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/.py_binary.venv/bin/python /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/.py_binary.venv/bin/python3 /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/external/python_toolchain_aarch64-apple-darwin /Users/arrdem/.local/pyenv/shims/python /Users/arrdem/.local/pyenv/shims/python3 ```
1 parent 65da8da commit a699063

File tree

11 files changed

+323
-173
lines changed

11 files changed

+323
-173
lines changed

docs/venv.md

Lines changed: 40 additions & 75 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

py/private/py_venv/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ exports_files([
1010
"link.py",
1111
])
1212

13+
config_setting(
14+
name = "debug_build",
15+
values = {
16+
"compilation_mode": "dbg",
17+
},
18+
)
19+
1320
bzl_library(
1421
name = "py_venv",
1522
srcs = [
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env bash
22

3+
if {{DEBUG}}; then
4+
set -x
5+
fi
6+
37
{{BASH_RLOCATION_FN}}
48

59
runfiles_export_envvars
610

711
set -o errexit -o nounset -o pipefail
812

9-
{{PRELUDE}}
10-
1113
source "$(rlocation "{{VENV}}")"/bin/activate
1214

13-
{{PREEXEC}}
14-
1515
exec "$(rlocation "{{VENV}}")"/bin/python {{INTERPRETER_FLAGS}} "$@"

0 commit comments

Comments
 (0)