Skip to content

Conversation

@arrdem
Copy link
Collaborator

@arrdem arrdem commented Jun 2, 2025

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

  • Updated script works on BASH
$ /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
  • Updated script works on ZSH
$ /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

@aspect-workflows
Copy link

aspect-workflows bot commented Jun 2, 2025

Test

19 test targets passed

Targets
//examples/multi_version:py_version_default_test [k8-fastbuild]                             1s
//examples/multi_version:py_version_test [k8-fastbuild-ST-494921797612]                     1s
//examples/pytest:pytest_test [k8-fastbuild]                                                2s
//examples/pytest:sharded/test [k8-fastbuild]                                               3s
//examples/virtual_deps:pytest_test [k8-fastbuild]                                          1s
//py/tests/cc-deps:test_smoke [k8-fastbuild]                                                428ms
//py/tests/external-deps:test_can_import_runfiles_helper [k8-fastbuild]                     552ms
//py/tests/internal-deps:assert [k8-fastbuild]                                              999ms
//py/tests/py-binary:runfiles_from_pip_test [k8-fastbuild]                                  1s
//py/tests/py-external-venv:test [k8-fastbuild]                                             190ms
//py/tests/py-internal-venv:test [k8-fastbuild]                                             202ms
//py/tests/py-test:test_env_vars [k8-fastbuild]                                             418ms
//py/tests/py_image_layer:py_image_test [k8-fastbuild]                                      1s
//py/tests/py_venv_image_layer:my_app_amd64_layers_test_test [k8-fastbuild]                 46ms
//py/tests/py_venv_image_layer:my_app_arm64_layers_test_test [k8-fastbuild]                 42ms
//py/tests/py_venv_image_layer:py_amd64_image_command_test [k8-fastbuild]                   998ms
//py/tests/py_venv_image_layer:py_amd64_image_content_test [k8-fastbuild]                   204ms
//py/tests/py_venv_image_layer:py_arm64_image_content_test [k8-fastbuild]                   701ms
//py/tests/repo_relative_imports/test:test [k8-fastbuild]                                   421ms

Total test execution time was 16s. 19 tests (50.0%) were fully cached saving 46s.

@github-actions
Copy link

github-actions bot commented Jun 2, 2025

e2e/use_release folder: LCOV of commit a4c3dbd during CI #1816

Summary coverage rate:
  lines......: 100.0% (2 of 2 lines)
  functions..: 100.0% (1 of 1 function)
  branches...: no data found

Files changed coverage rate: n/a

@arrdem arrdem requested a review from thesayyn June 3, 2025 21:36
Copy link
Member

@alexeagle alexeagle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌮

@arrdem arrdem merged commit a699063 into main Jun 3, 2025
16 checks passed
@arrdem arrdem deleted the arrdem/static-venv-runfiles-interpreter branch June 3, 2025 22:04
@arrdem arrdem added this to the 1.6.0 milestone Jun 9, 2025
arrdem pushed a commit that referenced this pull request Oct 15, 2025
### Changes are visible to end-users: yes

When creating a virtual environment, the `activate` script no longer
modifies bash flags. These flags were added in
#576 (v1.6.0) (note, they
don't appear in the scripts that Python's `venv` module generates).

<!-- If no, please delete this section. -->

- Searched for relevant documentation and updated as needed: yes
(behaviour is not documented)
- Breaking change (forces users to change their own code or config):no
- Suggested release notes appear below: yes/no

### Test plan

- Manual testing; please provide instructions so we can reproduce:

Potentially, users could have some tests running the following commands.
The new flags introduced in
#576 broke this kind of
tests:

```sh
#!/usr/bin/env bash
set -euo pipefail

# Create a virtualenvironment
bazel run //:venv

# Activate it (previous it was setting +e flag)
source .venv/bin/activate

# Some failing test -- users expects the script to end and fail here
false

# Some other command that doesn't fail
true

# !!!! Script returns 0 !!!!
```
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.

4 participants