fix(py_test): fix Windows crash in `py_test` main validation (#4079)

`py_test` main validation (`--validate_test_main=enabled`) crashes on
Windows, because it invokes the interpreter via
`exec_tools.exec_interpreter`, which resolves through
`current_interpreter_executable()`'s relocated copy of the interpreter.

The crash may be reproduced on existing tests in the repo, for instance:
```
bazel build \
  --@rules_python//python/config_settings:validate_test_main=enabled  \
  //tests/validate_test_main:validate_test_main_test
...
ERROR: .../tests/validate_test_main/BUILD.bazel:3:8: Validating py_test
 main //tests/validate_test_main:validate_test_main_test failed:
 (Exit -1073741515): python.exe failed: error executing
 PyValidateTestMain command
```
```
cd tests/integration/validate_test_main
bazel build \
  --@rules_python//python/config_settings:validate_test_main=enabled \
  //:good_test
...
ERROR: .../tests/integration/validate_test_main/BUILD.bazel:11:8:
 Validating py_test main //:good_test failed: (Exit -1073741515):
 python.exe failed: error executing PyValidateTestMain command
```

`-1073741515` is `STATUS_DLL_NOT_FOUND`: the relocated copy can't find
its DLLs beside itself.

This is also the Windows-local manifestation of #2703
(`exec_interpreter` broken on RBE): the same relocation severs the
interpreter from files resolved relative to itself, just triggered
differently: RBE's copy materialization there, a lack of symlink
privilege here.

Colocating the DLLs alone (a first attempt) traded this for a second,
still fatal error, `ModuleNotFoundError: No module named 'encodings'`,
because the copy still can't find its stdlib.
Patching each missing file individually doesn't scale: DLLs today,
stdlib tomorrow, whatever else a future toolchain needs beside itself
after that.

`_maybe_add_test_main_validation` now uses `actions_run()` with
`exec_runtime` instead of `exec_tools.exec_interpreter`'s relocated
`DefaultInfo.files_to_run`, matching
`PyExecToolsInfo.exec_interpreter`'s own documented recommendation and
the pattern `common.bzl`'s `actions_run()` and `py_zipapp_rule.bzl`
already use.
`exec_runtime.interpreter` is the real file, used directly, with its
real files as plain action inputs, so nothing is relocated and nothing
loses its siblings.

With the proposed fix[^1], above examples now build cleanly, with no
relocated runfiles tree for the interpreter at all, and
`tests/integration/validate_test_main`'s `inert_test` still fails with
its intended "will not run any tests" message rather than a crash.

`tests/integration/validate_test_main_test` is the corresponding
integration test, but it was not exercised on Windows, where it was
failing on `OSError: [WinError 193] %1 is not a valid Win32 application`
in `tests/integration/runner.py`'s Bazel-in-Bazel invocation, itself
unable to run `bazel_from_env`'s `#!` shebang line the way POSIX's
`exec` does.
The present change therefore fixes this, by resolving the shebang's
interpreter itself, and enables the test on Windows.

[^1]: This does not fix `exec_interpreter`/#2703 itself:
`precompile.bzl` still resolves the interpreter via the relocated path
and would need the same migration.

---------

Co-authored-by: Richard Levasseur <richardlev@gmail.com>
4 files changed
tree: a7346443bbbd235acbe7fc2bb63a2e1010fa3d6d
  1. .agents/
  2. .bazelci/
  3. .bcr/
  4. .ci/
  5. .github/
  6. command_line_option/
  7. dev/
  8. docs/
  9. examples/
  10. gazelle/
  11. news/
  12. private/
  13. python/
  14. sphinxdocs/
  15. tests/
  16. tools/
  17. .bazelignore
  18. .bazelrc
  19. .bazelrc.deleted_packages
  20. .bazelversion
  21. .editorconfig
  22. .git-blame-ignore-revs
  23. .gitattributes
  24. .gitignore
  25. .pre-commit-config.yaml
  26. .python-version
  27. .readthedocs.yml
  28. addlicense.sh
  29. AGENTS.md
  30. AUTHORS
  31. BUILD.bazel
  32. BZLMOD_SUPPORT.md
  33. CHANGELOG.md
  34. CONTRIBUTING.md
  35. CONTRIBUTORS
  36. downloader_config.cfg
  37. GEMINI.md
  38. internal_dev_deps.bzl
  39. internal_dev_setup.bzl
  40. LICENSE
  41. MODULE.bazel
  42. README.md
  43. RELEASING.md
  44. replicate_ci
  45. ruff.toml
  46. specialized_configs.bazelrc
  47. version.bzl
  48. WORKSPACE
  49. WORKSPACE.bzlmod
  50. workspace_bazel9.bzl
README.md

Python Rules for Bazel

Build status

Overview

This repository is the home of the core Python rules -- py_library, py_binary, py_test, and related symbols that provide the basis for Python support in Bazel. It also contains package installation rules for integrating with PyPI and other indices.

Documentation for rules_python is at https://rules-python.readthedocs.io and in the Bazel Build Encyclopedia.

Examples live in the examples directory.

The core rules are stable. Their implementation is subject to Bazel's backward compatibility policy. This repository aims to follow semantic versioning.

The Bazel community maintains this repository. Neither Google nor the Bazel team provides support for the code. However, this repository is part of the test suite used to vet new Bazel releases. See How to contribute page for information on our development workflow.

Design

  • Supported OSes - as per our supported platform policy, we strive for support on all of the platforms that we have CI for. Some platforms do not have the same backwards compatibility guarantees, but we hope the community can step in where needed to make the support more robust.
  • requirements.txt is how users have been defining dependencies for a long time. We support this to support legacy usecases or package managers that we don't support directly. Any additional information that we need will be retrieved from the SimpleAPI during the bzlmod extension evaluation phase. Then it will be written to the MODULE.bazel.lock file for future reuse. We have plans to support uv.lock file directly. uv is recommended for generating a fully locked requirements.txt file and we do provide a rule for it.
  • The py_binary, py_test rules should scale to large monorepos and we work hard to minimize the work done during analysis and build phase. What is more, the space requirements for should be minimal, so we strive to use symlinks rather than extracting wheels at build time. This means that for different configurations of the same build, we are not extracting the wheel multiple times thus scaling better over the time. From 2.0 onwards we are creating a virtual env for each target by creating an actual minimal virtual environment using symlinks. We plan on creating the traditional site-packages layout in the future by default.
  • Support for standards - we strive to first implement any standards needed within rules_python and this has resulted in a few PEPs supported within pure starlark - PEP440, PEP509.

Common misconceptions:

  • rules_python has to keep backwards compatibility with google3. Whilst this might have been true in the past, rules_python is an open source project and any compatibility needs should come from the community - we have no requirement to keep this compatibility and are allowed to make our decisions. However, we do want to keep backwards compatibility as long as possible to not upset users with never ending migrations.
  • rules_python is not caching pip downloads. With 2.0, we use Bazel's downloader by default and rely on bazel to provide the repository caching mechanisms. This means that for simpler setups this should result in transparent and scalable caching with the most recent bazel versions unless there are issues in the bazel itself.

Documentation

For detailed documentation, see https://rules-python.readthedocs.io

Bzlmod support

See Bzlmod support for more details.