fix: avoid stage1 bootstrap stdlib shadowing (#3854)

The stage 1 system-python bootstrap imports standard-library modules
before it
re-execs into the configured runtime. If the target output directory
contains
files named like standard-library modules, such as `shutil.py` or
`types.py`,
the bootstrap can import those files instead and fail before user code
starts.

This change removes Python's unsafe script-directory prepend before
those early
imports, while preserving `sys.path[0]` when Python has already
suppressed that
prepend through `-P`/`PYTHONSAFEPATH` or isolated mode. Stage 2 now uses
the same
isolated-mode guard when removing or recreating the main-directory path.

It also pins the Windows venv entry point template to LF line endings so
the
compile-pip CI dirty-worktree check is stable across checkout settings.

Before this change, a generated target output could shadow bootstrap
stdlib
imports, and isolated-mode runs on older Python versions could lose a
required
stdlib path.

After this change, bootstrap stdlib imports resolve from the
interpreter's
standard library, isolated mode preserves the interpreter-provided path,
and the
line-ending-sensitive CI check stays clean.

Tests:

```shell
bazel test --config=fast-tests \
  //tests/bootstrap_impls:stdlib_shadowing_system_python_test

bazel test --config=fast-tests \
  //tests/bootstrap_impls:stdlib_shadowing_system_python_test \
  //tests/bootstrap_impls:interpreter_args_test \
  //tests/bootstrap_impls:sys_path_order_bootstrap_script_test
```

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