Skip to content

Commit d75cdb6

Browse files
committed
Merge branch 'main' into exp/pypi-platform-config
2 parents dac4b6b + e73dccf commit d75cdb6

Some content is hidden

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

60 files changed

+981
-192
lines changed

CHANGELOG.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ END_UNRELEASED_TEMPLATE
6666
* (rules) On Windows, {obj}`--bootstrap_impl=system_python` is forced. This
6767
allows setting `--bootstrap_impl=script` in bazelrc for mixed-platform
6868
environments.
69-
* (rules) {obj}`pip_compile` now generates a `.test` target. The `_test` target is deprecated
70-
and will be removed in the next major release.
69+
* (rules) {obj}`compile_pip_requirements` now generates a `.test` target. The
70+
`_test` target is deprecated and will be removed in the next major release.
7171
([#2794](https://github.com/bazel-contrib/rules_python/issues/2794)
7272
* (py_wheel) py_wheel always creates zip64-capable wheel zips
7373

@@ -91,6 +91,7 @@ END_UNRELEASED_TEMPLATE
9191
also retrieved from the URL as opposed to only the `--hash` parameter. Fixes
9292
[#2363](https://github.com/bazel-contrib/rules_python/issues/2363).
9393
* (pypi) `whl_library` now infers file names from its `urls` attribute correctly.
94+
* (py_test, py_binary) Allow external files to be used for main
9495

9596
{#v0-0-0-added}
9697
### Added
@@ -192,7 +193,7 @@ END_UNRELEASED_TEMPLATE
192193
packages through SimpleAPI unless they are pulled through direct URL
193194
references. Fixes [#2023](https://github.com/bazel-contrib/rules_python/issues/2023).
194195
In case you see issues with `rules_python` being too eager to fetch the SimpleAPI
195-
metadata, you can use the newly added {attr}`pip.parse.experimental_skip_sources`
196+
metadata, you can use the newly added {attr}`pip.parse.simpleapi_skip`
196197
to skip metadata fetching for those packages.
197198
* (uv) A {obj}`lock` rule that is the replacement for the
198199
{obj}`compile_pip_requirements`. This may still have rough corners
@@ -253,7 +254,7 @@ END_UNRELEASED_TEMPLATE
253254

254255
{#v1-3-0-added}
255256
### Added
256-
* (python) {attr}`python.defaults` has been added to allow users to
257+
* (python) {obj}`python.defaults` has been added to allow users to
257258
set the default python version in the root module by reading the
258259
default version number from a file or an environment variable.
259260
* {obj}`//python/bin:python`: convenience target for directly running an
@@ -273,7 +274,7 @@ END_UNRELEASED_TEMPLATE
273274
and py_library rules
274275
([#1647](https://github.com/bazel-contrib/rules_python/issues/1647))
275276
* (rules) Added env-var to allow additional interpreter args for stage1 bootstrap.
276-
See {obj}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` environment variable.
277+
See {any}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` environment variable.
277278
Only applicable for {obj}`--bootstrap_impl=script`.
278279
* (rules) Added {obj}`interpreter_args` attribute to `py_binary` and `py_test`,
279280
which allows pass arguments to the interpreter before the regular args.
@@ -379,7 +380,7 @@ END_UNRELEASED_TEMPLATE
379380
values. Fixes [#2466](https://github.com/bazel-contrib/rules_python/issues/2466).
380381
* (py_proto_library) Fix import paths in Bazel 8.
381382
* (whl_library) Now the changes to the dependencies are correctly tracked when
382-
PyPI packages used in {bzl:obj}`whl_library` during the `repository_rule` phase
383+
PyPI packages used in `whl_library` during the repository rule phase
383384
change. Fixes [#2468](https://github.com/bazel-contrib/rules_python/issues/2468).
384385
+ (gazelle) Gazelle no longer ignores `setup.py` files by default. To restore
385386
this behavior, apply the `# gazelle:python_ignore_files setup.py` directive.
@@ -398,7 +399,7 @@ END_UNRELEASED_TEMPLATE
398399
* (pypi) Freethreaded packages are now fully supported in the
399400
{obj}`experimental_index_url` usage or the regular `pip.parse` usage.
400401
To select the free-threaded interpreter in the repo phase, please use
401-
the documented [env](/environment-variables.html) variables.
402+
the documented [env](environment-variables) variables.
402403
Fixes [#2386](https://github.com/bazel-contrib/rules_python/issues/2386).
403404
* (toolchains) Use the latest astrahl-sh toolchain release [20241206] for Python versions:
404405
* 3.9.21
@@ -492,7 +493,7 @@ Other changes:
492493
for the latest toolchain versions for each minor Python version. You can control
493494
the toolchain selection by using the
494495
{bzl:obj}`//python/config_settings:py_linux_libc` build flag.
495-
* (providers) Added {obj}`py_runtime_info.site_init_template` and
496+
* (providers) Added {obj}`PyRuntimeInfo.site_init_template` and
496497
{obj}`PyRuntimeInfo.site_init_template` for specifying the template to use to
497498
initialize the interpreter via venv startup hooks.
498499
* (runfiles) (Bazel 7.4+) Added support for spaces and newlines in runfiles paths
@@ -690,8 +691,8 @@ Other changes:
690691
* (bzlmod) The default value for the {obj}`--python_version` flag will now be
691692
always set to the default python toolchain version value.
692693
* (bzlmod) correctly wire the {attr}`pip.parse.extra_pip_args` all the
693-
way to {obj}`whl_library`. What is more we will pass the `extra_pip_args` to
694-
{obj}`whl_library` for `sdist` distributions when using
694+
way to `whl_library`. What is more we will pass the `extra_pip_args` to
695+
`whl_library` for `sdist` distributions when using
695696
{attr}`pip.parse.experimental_index_url`. See
696697
[#2239](https://github.com/bazel-contrib/rules_python/issues/2239).
697698
* (whl_filegroup): Provide per default also the `RECORD` file
@@ -739,8 +740,8 @@ Other changes:
739740

740741
{#v0-37-0-removed}
741742
### Removed
742-
* (precompiling) {obj}`--precompile_add_to_runfiles` has been removed.
743-
* (precompiling) {obj}`--pyc_collection` has been removed. The `pyc_collection`
743+
* (precompiling) `--precompile_add_to_runfiles` has been removed.
744+
* (precompiling) `--pyc_collection` has been removed. The `pyc_collection`
744745
attribute now bases its default on {obj}`--precompile`.
745746
* (precompiling) The {obj}`precompile=if_generated_source` value has been removed.
746747
* (precompiling) The {obj}`precompile_source_retention=omit_if_generated_source` value has been removed.
@@ -792,7 +793,7 @@ Other changes:
792793
in extra_requires in py_wheel rule.
793794
* (rules) Prevent pytest from trying run the generated stage2
794795
bootstrap .py file when using {obj}`--bootstrap_impl=script`
795-
* (toolchain) The {bzl:obj}`gen_python_config_settings` has been fixed to include
796+
* (toolchain) The `gen_python_config_settings` has been fixed to include
796797
the flag_values from the platform definitions.
797798

798799
{#v0-36-0-added}
@@ -1207,9 +1208,9 @@ Other changes:
12071208
depend on legacy labels instead of the hub repo aliases and you use the
12081209
`experimental_requirement_cycles`, now is a good time to migrate.
12091210

1210-
[python_default_visibility]: gazelle/README.md#directive-python_default_visibility
1211+
[python_default_visibility]: https://github.com/bazel-contrib/rules_python/tree/main/gazelle/README.md#directive-python_default_visibility
12111212
[test_file_pattern_issue]: https://github.com/bazel-contrib/rules_python/issues/1816
1212-
[test_file_pattern_docs]: gazelle/README.md#directive-python_test_file_pattern
1213+
[test_file_pattern_docs]: https://github.com/bazel-contrib/rules_python/tree/main/gazelle/README.md#directive-python_test_file_pattern
12131214
[20240224]: https://github.com/indygreg/python-build-standalone/releases/tag/20240224.
12141215
[20240415]: https://github.com/indygreg/python-build-standalone/releases/tag/20240415.
12151216

docs/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ sphinx_stardocs(
113113
"//python/private:builders_util_bzl",
114114
"//python/private:py_binary_rule_bzl",
115115
"//python/private:py_cc_toolchain_rule_bzl",
116+
"//python/private:py_info_bzl",
116117
"//python/private:py_library_rule_bzl",
117118
"//python/private:py_runtime_rule_bzl",
118119
"//python/private:py_test_rule_bzl",

docs/_includes/py_console_script_binary.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ py_console_script_binary(
4848
)
4949
```
5050

51+
#### Adding a Shebang Line
52+
53+
You can specify a shebang line for the generated binary, useful for Unix-like
54+
systems where the shebang line determines which interpreter is used to execute
55+
the script, per [PEP441]:
56+
57+
```starlark
58+
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
59+
60+
py_console_script_binary(
61+
name = "black",
62+
pkg = "@pip//black",
63+
shebang = "#!/usr/bin/env python3",
64+
)
65+
```
66+
67+
Note that to execute via the shebang line, you need to ensure the specified
68+
Python interpreter is available in the environment.
69+
70+
5171
#### Using a specific Python Version directly from a Toolchain
5272
:::{deprecated} 1.1.0
5373
The toolchain specific `py_binary` and `py_test` symbols are aliases to the regular rules.
@@ -70,4 +90,5 @@ py_console_script_binary(
7090
```
7191

7292
[specification]: https://packaging.python.org/en/latest/specifications/entry-points/
73-
[`py_console_script_binary.binary_rule`]: #py_console_script_binary_binary_rule
93+
[`py_console_script_binary.binary_rule`]: #py_console_script_binary_binary_rule
94+
[PEP441]: https://peps.python.org/pep-0441/#minimal-tooling-the-zipapp-module

docs/api/rules_python/python/bin/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
A target to directly run a Python interpreter.
1111

1212
By default, it uses the Python version that toolchain resolution matches
13-
(typically the one marked `is_default=True` in `MODULE.bazel`).
13+
(typically the one set with `python.defaults(python_version = ...)` in
14+
`MODULE.bazel`).
1415

1516
This runs a Python interpreter in a similar manner as when running `python3`
1617
on the command line. It can be invoked using `bazel run`. Remember that in

docs/conf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@
125125
primary_domain = None # The default is 'py', which we don't make much use of
126126
nitpicky = True
127127

128+
nitpick_ignore_regex = [
129+
# External xrefs aren't setup: ignore missing xref warnings
130+
# External xrefs to sphinx isn't setup: ignore missing xref warnings
131+
("py:.*", "(sphinx|docutils|ast|enum|collections|typing_extensions).*"),
132+
]
133+
128134
# --- Intersphinx configuration
129135

130136
intersphinx_mapping = {

DEVELOPING.md renamed to docs/devguide.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# For Developers
1+
# Dev Guide
22

33
This document covers tips and guidance for working on the rules_python code
44
base. A primary audience for it is first time contributors.
@@ -39,7 +39,7 @@ more important for tests to balance understandability and maintainability.
3939

4040
### sh_py_run_test
4141

42-
The [`sh_py_run_test`](tests/support/sh_py_run_test.bzl) rule is a helper to
42+
The {gh-path}`sh_py_run_test <tests/support/sh_py_run_test.bzl` rule is a helper to
4343
make it easy to run a Python program with custom build settings using a shell
4444
script to perform setup and verification. This is best to use when verifying
4545
behavior needs certain environment variables or directory structures to
@@ -56,6 +56,8 @@ Python binaries and tests with custom build flags. This is best to use when
5656
verifying behavior that requires specific flags to be set and when the program
5757
itself can verify the desired state.
5858

59+
They are located in {gh-path}`tests/support/py_reconfig.bzl`
60+
5961
When adding a test, you may find the flag you need to set isn't supported by
6062
the rule. To have it support setting a new flag:
6163

docs/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and the older way of using `WORKSPACE`.
77
It assumes you have a `requirements.txt` file with your PyPI dependencies.
88

99
For more details information about configuring `rules_python`, see:
10-
* [Configuring the runtime](toolchains)
10+
* [Configuring the runtime](configuring-toolchains)
1111
* [Configuring third party dependencies (pip/pypi)](pypi-dependencies)
1212
* [API docs](api/index)
1313

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ gazelle
104104
REPL <repl>
105105
Extending <extending>
106106
Contributing <contributing>
107+
devguide
107108
support
108109
Changelog <changelog>
109110
api/index

docs/toolchains.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
:::{default-domain} bzl
22
:::
33

4+
(configuring-toolchains)=
45
# Configuring Python toolchains and runtimes
56

67
This documents how to configure the Python toolchain and runtimes for different
@@ -43,7 +44,8 @@ you should read the dev-only library module section.
4344
bazel_dep(name="rules_python", version=...)
4445
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
4546
46-
python.toolchain(python_version = "3.12", is_default = True)
47+
python.defaults(python_version = "3.12")
48+
python.toolchain(python_version = "3.12")
4749
```
4850

4951
### Library modules
@@ -71,7 +73,8 @@ python = use_extension(
7173
dev_dependency = True
7274
)
7375
74-
python.toolchain(python_version = "3.12", is_default=True)
76+
python.defaults(python_version = "3.12")
77+
python.toolchain(python_version = "3.12")
7578
```
7679

7780
#### Library modules without version constraints
@@ -160,9 +163,13 @@ Multiple versions can be specified and used within a single build.
160163
# MODULE.bazel
161164
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
162165

166+
python.defaults(
167+
# The environment variable takes precedence if set.
168+
python_version = "3.11",
169+
python_version_env = "BAZEL_PYTHON_VERSION",
170+
)
163171
python.toolchain(
164172
python_version = "3.11",
165-
is_default = True,
166173
)
167174

168175
python.toolchain(
@@ -263,7 +270,8 @@ bazel_dep(name = "rules_python", version = "0.40.0")
263270

264271
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
265272

266-
python.toolchain(is_default = True, python_version = "3.10")
273+
python.defaults(python_version = "3.10")
274+
python.toolchain(python_version = "3.10")
267275

268276
use_repo(python, "python_3_10", "python_3_10_host")
269277
```

examples/bzlmod/MODULE.bazel

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ bazel_dep(name = "rules_rust", version = "0.54.1")
2828
# We next initialize the python toolchain using the extension.
2929
# You can set different Python versions in this block.
3030
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
31+
python.defaults(
32+
# Use python.defaults if you have defined multiple toolchain versions.
33+
python_version = "3.9",
34+
python_version_env = "BAZEL_PYTHON_VERSION",
35+
)
3136
python.toolchain(
3237
configure_coverage_tool = True,
33-
# Only set when you have multiple toolchain versions.
34-
is_default = True,
3538
python_version = "3.9",
3639
)
3740

0 commit comments

Comments
 (0)