Skip to content

Commit 8635890

Browse files
committed
Merge remote-tracking branch 'upstream/main' into HEAD
2 parents 9121abd + a19e1e4 commit 8635890

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1920
-1542
lines changed

.github/workflows/mypy.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defaults:
1515

1616
jobs:
1717
ci:
18-
runs-on: ubuntu-20.04
18+
runs-on: ubuntu-latest
1919
steps:
2020
# Checkout the code
2121
- uses: actions/checkout@v4

CHANGELOG.md

+55-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ A brief description of the categories of changes:
2121
`(docs)`.
2222

2323
<!--
24-
Unreleased changes template.
24+
BEGIN_UNRELEASED_TEMPLATE
2525
2626
{#v0-0-0}
2727
## Unreleased
@@ -43,8 +43,9 @@ Unreleased changes template.
4343
{#v0-0-0-removed}
4444
### Removed
4545
* Nothing removed.
46-
-->
4746
47+
END_UNRELEASED_TEMPLATE
48+
-->
4849

4950
{#v0-0-0}
5051
## Unreleased
@@ -53,6 +54,29 @@ Unreleased changes template.
5354

5455
{#v0-0-0-changed}
5556
### Changed
57+
* Nothing changed.
58+
59+
{#v0-0-0-fixed}
60+
### Fixed
61+
* (rules) PyInfo provider is now advertised by py_test, py_binary, and py_library;
62+
this allows aspects using required_providers to function correctly.
63+
([#2506](https://github.com/bazel-contrib/rules_python/issues/2506)).
64+
65+
{#v0-0-0-added}
66+
### Added
67+
* Nothing added.
68+
69+
{#v0-0-0-removed}
70+
### Removed
71+
* Nothing removed.
72+
73+
{#1-4-0}
74+
## [1.4.0] - 2025-04-19
75+
76+
[1.4.0]: https://github.com/bazel-contrib/rules_python/releases/tag/1.4.0
77+
78+
{#1-4-0-changed}
79+
### Changed
5680
* (toolchain) The `exec` configuration toolchain now has the forwarded
5781
`exec_interpreter` now also forwards the `ToolchainInfo` provider. This is
5882
for increased compatibility with the `RBE` setups where access to the `exec`
@@ -72,15 +96,29 @@ Unreleased changes template.
7296
* (toolchains) Previously [#2636](https://github.com/bazel-contrib/rules_python/pull/2636)
7397
changed the semantics of `ignore_root_user_error` from "ignore" to "warning". This is now
7498
flipped back to ignoring the issue, and will only emit a warning when the attribute is set
75-
`False`.
99+
`False`.
76100
* (pypi) The PyPI extension will no longer write the lock file entries as the
77101
extension has been marked reproducible.
78102
Fixes [#2434](https://github.com/bazel-contrib/rules_python/issues/2434).
103+
* (gazelle) Lazily load and parse manifest files when running Gazelle. This ensures no
104+
manifest files are loaded when Gazelle is run over a set of non-python directories
105+
[PR #2746](https://github.com/bazel-contrib/rules_python/pull/2746).
106+
* (rules) {attr}`py_binary.srcs` and {attr}`py_test.srcs` is no longer mandatory when
107+
`main_module` is specified (for `--bootstrap_impl=script`)
108+
* (pypi) From now on the `Requires-Dist` from the wheel metadata is analysed in
109+
the loading phase instead of repository rule phase giving better caching
110+
performance when the target platforms are changed (e.g. target python
111+
versions). This is preparatory work for stabilizing the cross-platform wheel
112+
support. From now on the usage of `experimental_target_platforms` should be
113+
avoided and the `requirements_by_platform` values should be instead used to
114+
specify the target platforms for the given dependencies.
79115

80116
[20250317]: https://github.com/astral-sh/python-build-standalone/releases/tag/20250317
81117

82-
{#v0-0-0-fixed}
118+
{#1-4-0-fixed}
83119
### Fixed
120+
* (pypi) Platform specific extras are now correctly handled when using
121+
universal lock files with environment markers. Fixes [#2690](https://github.com/bazel-contrib/rules_python/pull/2690).
84122
* (runfiles) ({obj}`--bootstrap_impl=script`) Follow symlinks when searching for runfiles.
85123
* (toolchains) Do not try to run `chmod` when downloading non-windows hermetic toolchain
86124
repositories on Windows. Fixes
@@ -91,8 +129,12 @@ Unreleased changes template.
91129
Fixes [#2685](https://github.com/bazel-contrib/rules_python/issues/2685).
92130
* (toolchains) Run the check on the Python interpreter in isolated mode, to ensure it's not affected by userland environment variables, such as `PYTHONPATH`.
93131
* (toolchains) Ensure temporary `.pyc` and `.pyo` files are also excluded from the interpreters repository files.
132+
* (pypi) Run interpreter version call in isolated mode, to ensure it's not affected by userland environment variables, such as `PYTHONPATH`.
133+
* (packaging) An empty `requires_file` is treated as if it were omitted, resulting in a valid `METADATA` file.
134+
* (rules) py_wheel and sphinxdocs rules now propagate `target_compatible_with` to all targets they create.
135+
[PR #2788](https://github.com/bazel-contrib/rules_python/pull/2788).
94136

95-
{#v0-0-0-added}
137+
{#1-4-0-added}
96138
### Added
97139
* (pypi) From now on `sha256` values in the `requirements.txt` is no longer
98140
mandatory when enabling {attr}`pip.parse.experimental_index_url` feature.
@@ -116,14 +158,20 @@ Unreleased changes template.
116158
allow specifying links to create within the venv site packages (only
117159
applicable with {obj}`--bootstrap_impl=script`)
118160
([#2156](https://github.com/bazelbuild/rules_python/issues/2156)).
161+
* (toolchains) Local Python installs can be used to create a toolchain
162+
equivalent to the standard toolchains. See [Local toolchains] docs for how to
163+
configure them.
164+
* (toolchains) Expose `$(PYTHON2_ROOTPATH)` and `$(PYTHON3_ROOTPATH)` which are runfiles
165+
locations equivalents of `$(PYTHON2)` and `$(PYTHON3) respectively.
119166

120-
{#v0-0-0-removed}
167+
168+
{#1-4-0-removed}
121169
### Removed
122170
* Nothing removed.
123171

124172

125173
{#v1-3-0}
126-
## Unreleased
174+
## [1.3.0] - 2025-03-27
127175

128176
[1.3.0]: https://github.com/bazel-contrib/rules_python/releases/tag/1.3.0
129177

RELEASING.md

+21
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ These are the steps for a regularly scheduled release from HEAD.
1414

1515
1. [Determine the next semantic version number](#determining-semantic-version).
1616
1. Update CHANGELOG.md: replace the `v0-0-0` and `0.0.0` with `X.Y.0`.
17+
```
18+
awk -v version=X.Y.0 'BEGIN { hv=version; gsub(/\./, "-", hv) } /END_UNRELEASED_TEMPLATE/ { found_marker = 1 } found_marker { gsub(/v0-0-0/, hv, $0); gsub(/Unreleased/, "[" version "] - " strftime("%Y-%m-%d"), $0); gsub(/0.0.0/, version, $0); } { print } ' CHANGELOG.md > /tmp/changelog && cp /tmp/changelog CHANGELOG.md
19+
```
1720
1. Replace `VERSION_NEXT_*` strings with `X.Y.0`.
21+
```
22+
grep -l --exclude=CONTRIBUTING.md --exclude=RELEASING.md --exclude-dir=.* VERSION_NEXT_ -r \
23+
| xargs sed -i -e 's/VERSION_NEXT_FEATURE/X.Y.0/' -e 's/VERSION_NEXT_PATCH/X.Y.0/'
24+
```
1825
1. Send these changes for review and get them merged.
1926
1. Create a branch for the new release, named `release/X.Y`
2027
```
@@ -90,6 +97,20 @@ It will be promoted to stable next week, pending feedback.
9097
It's traditional to include notable changes from the changelog, but not
9198
required.
9299

100+
### Re-releasing a version
101+
102+
Re-releasing a version (i.e. changing the commit a tag points to) is
103+
*sometimes* possible, but it depends on how far into the release process it got.
104+
105+
The two points of no return are:
106+
* If the PyPI package has been published: PyPI disallows using the same
107+
filename/version twice. Once published, it cannot be replaced.
108+
* If the BCR package has been published: Once it's been committed to the BCR
109+
registry, it cannot be replaced.
110+
111+
If release steps fail _prior_ to those steps, then its OK to change the tag. You
112+
may need to manually delete the GitHub release.
113+
93114
## Secrets
94115

95116
### PyPI user rules-python

config.bzl.tmpl.bzlmod

Whitespace-only changes.

docs/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ sphinx_stardocs(
108108
"//python/cc:py_cc_toolchain_bzl",
109109
"//python/cc:py_cc_toolchain_info_bzl",
110110
"//python/entry_points:py_console_script_binary_bzl",
111+
"//python/local_toolchains:repos_bzl",
111112
"//python/private:attr_builders_bzl",
112113
"//python/private:builders_util_bzl",
113114
"//python/private:py_binary_rule_bzl",

docs/requirements.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,13 @@ sphinxcontrib-serializinghtml==2.0.0 \
351351
--hash=sha256:6e2cb0eef194e10c27ec0023bfeb25badbbb5868244cf5bc5bdc04e4464bf331 \
352352
--hash=sha256:e9d912827f872c029017a53f0ef2180b327c3f7fd23c87229f7a8e8b70031d4d
353353
# via sphinx
354-
typing-extensions==4.12.2 \
355-
--hash=sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d \
356-
--hash=sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8
354+
typing-extensions==4.13.2 \
355+
--hash=sha256:a439e7c04b49fec3e5d3e2beaa21755cadbbdc391694e28ccdd36ca4a1408f8c \
356+
--hash=sha256:e6c81219bd689f51865d9e372991c540bda33a0379d5573cddb9a3a23f7caaef
357357
# via
358358
# rules-python-docs (docs/pyproject.toml)
359359
# sphinx-autodoc2
360-
urllib3==2.3.0 \
361-
--hash=sha256:1cee9ad369867bfdbbb48b7dd50374c0967a0bb7710050facf0dd6911440e3df \
362-
--hash=sha256:f8c5449b3cf0861679ce7e0503c7b44b5ec981bec0d1d3795a07f1ba96f0204d
360+
urllib3==2.4.0 \
361+
--hash=sha256:414bc6535b787febd7567804cc015fee39daab8ad86268f1310a9250697de466 \
362+
--hash=sha256:4e16665048960a0900c702d4a66415956a584919c03361cac9f1df5c5dd7e813
363363
# via requests

docs/support.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,35 @@ minor/patch versions.
3131
See [Bazel's release support matrix](https://bazel.build/release#support-matrix)
3232
for what versions are the rolling, active, and prior releases.
3333

34+
## Supported Python versions
35+
36+
As a general rule we test all released non-EOL Python versions. Different
37+
interpreter versions may work but are not guaranteed. We are interested in
38+
staying compatible with upcoming unreleased versions, so if you see that things
39+
stop working, please create tickets or, more preferably, pull requests.
40+
3441
## Supported Platforms
3542

3643
We only support the platforms that our continuous integration jobs run, which
37-
is Linux, Mac, and Windows. Code to support other platforms is allowed, but
38-
can only be on a best-effort basis.
44+
is Linux, Mac, and Windows.
45+
46+
In order to better describe different support levels, the below acts as a rough
47+
guideline for different platform tiers:
48+
* Tier 0 - The platforms that our CI runs: `linux_x86_64`, `osx_x86_64`, `RBE linux_x86_64`.
49+
* Tier 1 - The platforms that are similar enough to what the CI runs: `linux_aarch64`, `osx_arm64`.
50+
What is more, `windows_x86_64` is in this list as we run tests in CI but
51+
developing for Windows is more challenging and features may come later to
52+
this platform.
53+
* Tier 2 - The rest of the platforms that may have varying level of support, e.g.
54+
`linux_s390x`, `linux_ppc64le`, `windows_arm64`.
55+
56+
:::{note}
57+
Code to support Tier 2 platforms is allowed, but regressions will be fixed on a
58+
best-effort basis, so feel free to contribute by creating PRs.
59+
60+
If you would like to provide/sponsor CI setup for a platform that is not Tier 0,
61+
please create a ticket or contact the maintainers on Slack.
62+
:::
3963

4064
## Compatibility Policy
4165

docs/toolchains.md

+98-5
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ Remember to call `use_repo()` to make repos visible to your module:
199199

200200

201201
:::{deprecated} 1.1.0
202-
The toolchain specific `py_binary` and `py_test` symbols are aliases to the regular rules.
202+
The toolchain specific `py_binary` and `py_test` symbols are aliases to the regular rules.
203203
i.e. Deprecated `load("@python_versions//3.11:defs.bzl", "py_binary")` & `load("@python_versions//3.11:defs.bzl", "py_test")`
204204

205-
Usages of them should be changed to load the regular rules directly;
205+
Usages of them should be changed to load the regular rules directly;
206206
i.e. Use `load("@rules_python//python:py_binary.bzl", "py_binary")` & `load("@rules_python//python:py_test.bzl", "py_test")` and then specify the `python_version` when using the rules corresponding to the python version you defined in your toolchain. {ref}`Library modules with version constraints`
207207
:::
208208

@@ -215,7 +215,11 @@ attribute. You can obtain the path to the Python interpreter using the
215215
`$(PYTHON2)` and `$(PYTHON3)` ["Make"
216216
Variables](https://bazel.build/reference/be/make-variables). See the
217217
{gh-path}`test_current_py_toolchain <tests/load_from_macro/BUILD.bazel>` target
218-
for an example.
218+
for an example. We also make available `$(PYTHON2_ROOTPATH)` and `$(PYTHON3_ROOTPATH)`
219+
which are Make Variable equivalents of `$(PYTHON2)` and `$(PYTHON3)` but for runfiles
220+
locations. These will be helpful if you need to set env vars of binary/test rules
221+
while using [`--nolegacy_external_runfiles`](https://bazel.build/reference/command-line-reference#flag--legacy_external_runfiles).
222+
The original make variables still work in exec contexts such as genrules.
219223

220224
### Overriding toolchain defaults and adding more versions
221225

@@ -327,7 +331,97 @@ After registration, your Python targets will use the toolchain's interpreter dur
327331
is still used to 'bootstrap' Python targets (see https://github.com/bazel-contrib/rules_python/issues/691).
328332
You may also find some quirks while using this toolchain. Please refer to [python-build-standalone documentation's _Quirks_ section](https://gregoryszorc.com/docs/python-build-standalone/main/quirks.html).
329333

330-
## Autodetecting toolchain
334+
## Local toolchain
335+
336+
It's possible to use a locally installed Python runtime instead of the regular
337+
prebuilt, remotely downloaded ones. A local toolchain contains the Python
338+
runtime metadata (Python version, headers, ABI flags, etc) that the regular
339+
remotely downloaded runtimes contain, which makes it possible to build e.g. C
340+
extensions (unlike the autodetecting and runtime environment toolchains).
341+
342+
For simple cases, some rules are provided that will introspect
343+
a Python installation and create an appropriate Bazel definition from
344+
it. To do this, three pieces need to be wired together:
345+
346+
1. Specify a path or command to a Python interpreter (multiple can be defined).
347+
2. Create toolchains for the runtimes in (1)
348+
3. Register the toolchains created by (2)
349+
350+
The below is an example that will use `python3` from PATH to find the
351+
interpreter, then introspect its installation to generate a full toolchain.
352+
353+
```starlark
354+
# File: MODULE.bazel
355+
356+
local_runtime_repo = use_repo_rule(
357+
"@rules_python//python/local_toolchains:repos.bzl",
358+
"local_runtime_repo",
359+
dev_dependency = True,
360+
)
361+
362+
local_runtime_toolchains_repo = use_repo_rule(
363+
"@rules_python//python/local_toolchains:repos.bzl"
364+
"local_runtime_toolchains_repo"
365+
dev_dependency = True,
366+
)
367+
368+
# Step 1: Define the Python runtime
369+
local_runtime_repo(
370+
name = "local_python3",
371+
interpreter_path = "python3",
372+
on_failure = "fail",
373+
)
374+
375+
# Step 2: Create toolchains for the runtimes
376+
local_runtime_toolchains_repo(
377+
name = "local_toolchains",
378+
runtimes = ["local_python3"],
379+
)
380+
381+
# Step 3: Register the toolchains
382+
register_toolchains("@local_toolchains//:all", dev_dependency = True)
383+
```
384+
385+
Note that `register_toolchains` will insert the local toolchain earlier in the
386+
toolchain ordering, so it will take precedence over other registered toolchains.
387+
388+
:::{important}
389+
Be sure to set `dev_dependency = True`. Using a local toolchain only makes sense
390+
for the root module.
391+
392+
If an intermediate module does it, then the `register_toolchains()` call will
393+
take precedence over the default rules_python toolchains and cause problems for
394+
downstream modules.
395+
:::
396+
397+
Multiple runtimes and/or toolchains can be defined, which allows for multiple
398+
Python versions and/or platforms to be configured in a single `MODULE.bazel`.
399+
400+
## Runtime environment toolchain
401+
402+
The runtime environment toolchain is a minimal toolchain that doesn't provide
403+
information about Python at build time. In particular, this means it is not able
404+
to build C extensions -- doing so requires knowing, at build time, what Python
405+
headers to use.
406+
407+
In effect, all it does is generate a small wrapper script that simply calls e.g.
408+
`/usr/bin/env python3` to run a program. This makes it easy to change what
409+
Python is used to run a program, but also makes it easy to use a Python version
410+
that isn't compatible with build-time assumptions.
411+
412+
```
413+
register_toolchains("@rules_python//python/runtime_env_toolchains:all")
414+
```
415+
416+
Note that this toolchain has no constraints, i.e. it will match any platform,
417+
Python version, etc.
418+
419+
:::{seealso}
420+
[Local toolchain], which creates a more full featured toolchain from a
421+
locally installed Python.
422+
:::
423+
424+
### Autodetecting toolchain
331425

332426
The autodetecting toolchain is a deprecated toolchain that is built into Bazel.
333427
It's name is a bit misleading: it doesn't autodetect anything. All it does is
@@ -345,7 +439,6 @@ To aid migration off the Bazel-builtin toolchain, rules_python provides
345439
{bzl:obj}`@rules_python//python/runtime_env_toolchains:all`. This is an equivalent
346440
toolchain, but is implemented using rules_python's objects.
347441

348-
349442
## Custom toolchains
350443

351444
While rules_python provides toolchains by default, it is not required to use

examples/wheel/BUILD.bazel

+16
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ starlark # Example comment
294294
""".splitlines(),
295295
)
296296

297+
write_file(
298+
name = "empty_requires_file",
299+
out = "empty_requires.txt",
300+
content = [""],
301+
)
302+
297303
write_file(
298304
name = "extra_requires_file",
299305
out = "extra_requires.txt",
@@ -324,6 +330,15 @@ py_wheel(
324330
deps = [":example_pkg"],
325331
)
326332

333+
py_wheel(
334+
name = "empty_requires_files",
335+
distribution = "empty_requires_files",
336+
python_tag = "py3",
337+
requires_file = ":empty_requires.txt",
338+
version = "0.0.1",
339+
deps = [":example_pkg"],
340+
)
341+
327342
# Package just a specific py_libraries, without their dependencies
328343
py_wheel(
329344
name = "minimal_data_files",
@@ -367,6 +382,7 @@ py_test(
367382
":custom_package_root_multi_prefix",
368383
":custom_package_root_multi_prefix_reverse_order",
369384
":customized",
385+
":empty_requires_files",
370386
":extra_requires",
371387
":filename_escaping",
372388
":minimal_data_files",

0 commit comments

Comments
 (0)