Commit a699063
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- docs
- py
- private/py_venv
- tests/py_venv_image_layer
- tools
- py
- src
- venv_bin/src
11 files changed
+323
-173
lines changedSome generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
13 | 20 | | |
14 | 21 | | |
15 | 22 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
3 | 7 | | |
4 | 8 | | |
5 | 9 | | |
6 | 10 | | |
7 | 11 | | |
8 | 12 | | |
9 | | - | |
10 | | - | |
11 | 13 | | |
12 | 14 | | |
13 | | - | |
14 | | - | |
15 | 15 | | |
0 commit comments