Skip to content

Commit 317dab4

Browse files
aignasgroodtrickeylev
authored
docs: rules_python bzlmod GA and 1.0 prep (bazel-contrib#2296)
This is documenting the current state and closing the last remaining TODO items for 1.0 release. Work towards bazel-contrib#1361. --------- Co-authored-by: Greg Roodt <[email protected]> Co-authored-by: Richard Levasseur <[email protected]>
1 parent 787f3cb commit 317dab4

File tree

11 files changed

+129
-67
lines changed

11 files changed

+129
-67
lines changed

BZLMOD_SUPPORT.md

+30-12
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
## `rules_python` `bzlmod` support
44

5-
- Status: Beta
5+
- Status: GA
66
- Full Feature Parity: No
7+
- `rules_python`: Yes
8+
- `rules_python_gazelle_plugin`: No (see below).
79

8-
Some features are missing or broken, and the public APIs are not yet stable.
10+
In general `bzlmod` has more features than `WORKSPACE` and users are encouraged to migrate.
911

1012
## Configuration
1113

@@ -27,15 +29,6 @@ A user does not use `local_path_override` stanza and would define the version in
2729

2830
A second example, in [examples/bzlmod_build_file_generation](examples/bzlmod_build_file_generation) demonstrates the use of `bzlmod` to configure `gazelle` support for `rules_python`.
2931

30-
## Feature parity
31-
32-
This rule set does not have full feature partity with the older `WORKSPACE` type configuration:
33-
34-
1. Gazelle does not support finding deps in sub-modules. For instance we can have a dep like ` "@our_other_module//other_module/pkg:lib",` in a `py_test` definition.
35-
2. We have some features that are still not fully flushed out, and the user interface may change.
36-
37-
Check ["issues"](/bazelbuild/rules_python/issues) for an up to date list.
38-
3932
## Differences in behavior from WORKSPACE
4033

4134
### Default toolchain is not the local system Python
@@ -52,10 +45,35 @@ platforms.
5245
If you want to use the same toolchain as what WORKSPACE used, then manually
5346
register the builtin Bazel Python toolchain by doing
5447
`register_toolchains("@bazel_tools//tools/python:autodetecting_toolchain")`.
55-
**IMPORTANT: this should only be done in a root module, and may intefere with
48+
49+
Note that using this builtin Bazel toolchain is deprecated and unsupported.
50+
See the {obj}`runtime_env_toolchains` docs for a replacement that is marginally
51+
better supported.
52+
**IMPORTANT: this should only be done in a root module, and may interfere with
5653
the toolchains rules_python registers**.
5754

5855
NOTE: Regardless of your toolchain, due to
5956
[#691](https://github.com/bazelbuild/rules_python/issues/691), `rules_python`
6057
still relies on a local Python being available to bootstrap the program before
6158
handing over execution to the toolchain Python.
59+
60+
To override this behaviour see {obj}`--bootstrap_impl=script`, which switches
61+
to `bash`-based bootstrap on UNIX systems.
62+
63+
### Better PyPI package downloading on bzlmod
64+
65+
On `bzlmod` users have the option to use the `bazel_downloader` to download packages
66+
and work correctly when `host` platform is not the same as the `target` platform. This
67+
provides faster package download times and integration with the credentials helper.
68+
69+
### Extra targets in `whl_library` repos
70+
71+
Due to how `bzlmod` is designed and the visibility rules that it enforces, it is best to use
72+
the targets in the `whl` repos as they do not rely on using the `annotations` API to
73+
add extra targets to so-called `spoke` repos. For alternatives that should cover most of the
74+
existing usecases please see:
75+
* {bzl:obj}`py_console_script_binary` to create `entry_point` targets.
76+
* {bzl:obj}`whl_filegroup` to extract filegroups from the `whl` targets (e.g. `@pip//numpy:whl`)
77+
* {bzl:obj}`pip.override` to patch the downloaded `whl` files. Using that you
78+
can change the `METADATA` of the `whl` file that will influence how
79+
`rules_python` code generation behaves.

CONTRIBUTING.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,13 @@ The general process is:
194194
of. The API for the control mechanism can be removed in this release.
195195

196196
Note that the `+1` and `+2` releases are just examples; the steps are not
197-
required to happen in immedially subsequent releases.
197+
required to happen in immediately subsequent releases.
198198

199+
Once The first major version is released, the process will be:
200+
1. In `N.M.0` we introduce the new behaviour, but it is disabled by a feature flag.
201+
2. In `N.M+1.0` we may choose the behaviour to become the default if it is not too
202+
disruptive.
203+
3. In `N+1.0.0` we get rid of the old behaviour.
199204

200205
### How to control breaking changes
201206

docs/getting-started.md

+14-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Getting started
22

3-
This doc is a simplified guide to help get started started quickly. It provides
4-
a simplified introduction to having a working Python program for both bzlmod
3+
This doc is a simplified guide to help get started quickly. It provides
4+
a simplified introduction to having a working Python program for both `bzlmod`
55
and the older way of using `WORKSPACE`.
66

77
It assumes you have a `requirements.txt` file with your PyPI dependencies.
@@ -23,11 +23,11 @@ bazel_dep(name = "rules_python", version = "0.0.0")
2323

2424
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
2525
pip.parse(
26-
hub_name = "my_deps",
26+
hub_name = "pypi",
2727
python_version = "3.11",
2828
requirements_lock = "//:requirements.txt",
2929
)
30-
use_repo(pip, "my_deps")
30+
use_repo(pip, "pypi")
3131
```
3232

3333
## Using a WORKSPACE file
@@ -38,19 +38,14 @@ using Bzlmod. Here is a simplified setup to download the prebuilt runtimes.
3838
```starlark
3939
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4040

41-
42-
# Update the SHA and VERSION to the lastest version available here:
43-
# https://github.com/bazelbuild/rules_python/releases.
44-
45-
SHA="84aec9e21cc56fbc7f1335035a71c850d1b9b5cc6ff497306f84cced9a769841"
46-
47-
VERSION="0.23.1"
41+
# Update the snippet based on the latest release below
42+
# https://github.com/bazelbuild/rules_python/releases
4843

4944
http_archive(
5045
name = "rules_python",
51-
sha256 = SHA,
52-
strip_prefix = "rules_python-{}".format(VERSION),
53-
url = "https://github.com/bazelbuild/rules_python/releases/download/{}/rules_python-{}.tar.gz".format(VERSION,VERSION),
46+
sha256 = "ca77768989a7f311186a29747e3e95c936a41dffac779aff6b443db22290d913",
47+
strip_prefix = "rules_python-0.36.0",
48+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.36.0/rules_python-0.36.0.tar.gz",
5449
)
5550

5651
load("@rules_python//python:repositories.bzl", "py_repositories")
@@ -66,14 +61,12 @@ python_register_toolchains(
6661
python_version = "3.11",
6762
)
6863

69-
load("@python_3_11//:defs.bzl", "interpreter")
70-
7164
load("@rules_python//python:pip.bzl", "pip_parse")
7265

7366
pip_parse(
74-
...
75-
python_interpreter_target = interpreter,
76-
...
67+
name = "pypi",
68+
python_interpreter_target = "@python_3_11_host//:python",
69+
requirements_lock = "//:requirements.txt",
7770
)
7871
```
7972

@@ -89,8 +82,8 @@ py_binary(
8982
name = "main",
9083
srcs = ["main.py"],
9184
deps = [
92-
"@my_deps//foo",
93-
"@my_deps//bar",
85+
"@pypi//foo",
86+
"@pypi//bar",
9487
]
9588
)
9689
```

docs/index.md

+62-23
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,73 @@
11
# Python Rules for Bazel
22

3-
rules_python is the home of the core Python rules -- `py_library`,
4-
`py_binary`, `py_test`, `py_proto_library`, and related symbols that provide the basis for Python
5-
support in Bazel. It also contains package installation rules for integrating with PyPI and other indices.
3+
`rules_python` is the home for 4 major components with varying maturity levels.
64

7-
Documentation for rules_python lives here and in the
8-
[Bazel Build Encyclopedia](https://docs.bazel.build/versions/master/be/python.html).
5+
:::{topic} Core rules
96

10-
Examples are in the {gh-path}`examples` directory.
7+
The core Python rules -- `py_library`, `py_binary`, `py_test`,
8+
`py_proto_library`, and related symbols that provide the basis for Python
9+
support in Bazel.
1110

12-
Currently, the core rules build into the Bazel binary, and the symbols in this
13-
repository are simple aliases. However, we are migrating the rules to Starlark and removing them from the Bazel binary. Therefore, the future-proof way to depend on Python rules is via this repository. See
14-
{ref}`Migrating from the Bundled Rules` below.
11+
When using Bazel 6 (or earlier), the core rules are bundled into the Bazel binary, and the symbols
12+
in this repository are simple aliases. On Bazel 7 and above `rules_python` uses
13+
a separate Starlark implementation,
14+
see {ref}`Migrating from the Bundled Rules` below.
1515

16-
The core rules are stable. Their implementation in Bazel is subject to Bazel's
17-
[backward compatibility policy](https://docs.bazel.build/versions/master/backward-compatibility.html).
18-
Once migrated to rules_python, they may evolve at a different
19-
rate, but this repository will still follow [semantic versioning](https://semver.org).
16+
Once rules_python 1.0 is released, they will follow
17+
[semantic versioning](https://semver.org) and the breaking change policy
18+
outlined in the [support](support) page.
2019

21-
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
22-
{gh-path}`How to contribute <CONTRIBUTING.md>` for information on our development workflow.
20+
:::
2321

24-
## Bzlmod support
22+
:::{topic} PyPI integration
2523

26-
- Status: Beta
27-
- Full Feature Parity: No
24+
Package installation rules for integrating with PyPI and other SimpleAPI
25+
compatible indexes.
2826

29-
See {gh-path}`Bzlmod support <BZLMOD_SUPPORT.md>` for more details
27+
These rules work and can be used in production, but the cross-platform building
28+
that supports pulling PyPI dependencies for a target platform that is different
29+
from the host platform is still in beta and the APIs that are subject to potential
30+
change are marked as `experimental`.
31+
32+
:::
33+
34+
:::{topic} Sphinxdocs
35+
36+
`sphinxdocs` rules allow users to generate documentation using Sphinx powered by Bazel, with additional functionality for documenting
37+
Starlark and Bazel code.
38+
39+
The functionality is exposed because other projects find it useful, but
40+
it is available as is and **the semantic versioning and
41+
compatibility policy used by `rules_python` does not apply**.
42+
43+
:::
44+
45+
:::{topic} Gazelle plugin
46+
47+
`gazelle` plugin for generating `BUILD.bazel` files based on Python source
48+
code.
49+
50+
This is available as is and the semantic versioning used by `rules_python` does
51+
not apply.
52+
53+
:::
54+
55+
The Bazel community maintains this repository. Neither Google nor the Bazel
56+
team provides support for the code. However, this repository is part of the
57+
test suite used to vet new Bazel releases. See {gh-path}`How to contribute
58+
<CONTRIBUTING.md>` for information on our development workflow.
59+
60+
## Examples
61+
62+
This documentation is an example of `sphinxdocs` rules and the rest of the
63+
components have examples in the {gh-path}`examples` directory.
3064

3165
## Migrating from the bundled rules
3266

3367
The core rules are currently available in Bazel as built-in symbols, but this
3468
form is deprecated. Instead, you should depend on rules_python in your
35-
`WORKSPACE` file and load the Python rules from
36-
`@rules_python//python:defs.bzl`.
69+
`WORKSPACE` or `MODULE.bazel` file and load the Python rules from
70+
`@rules_python//python:defs.bzl` or load paths described in the API documentation.
3771

3872
A [buildifier](https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md)
3973
fix is available to automatically migrate `BUILD` and `.bzl` files to add the
@@ -44,13 +78,18 @@ appropriate `load()` statements and rewrite uses of `native.py_*`.
4478
buildifier --lint=fix --warnings=native-py <files>
4579
```
4680

47-
Currently, the `WORKSPACE` file needs to be updated manually as per [Getting
48-
started](getting-started).
81+
Currently, the `WORKSPACE` file needs to be updated manually as per
82+
[Getting started](getting-started).
4983

5084
Note that Starlark-defined bundled symbols underneath
5185
`@bazel_tools//tools/python` are also deprecated. These are not yet rewritten
5286
by buildifier.
5387

88+
## Migrating to bzlmod
89+
90+
See {gh-path}`Bzlmod support <BZLMOD_SUPPORT.md>` for any behaviour differences between
91+
`bzlmod` and `WORKSPACE`.
92+
5493

5594
```{toctree}
5695
:hidden:

examples/bzlmod/MODULE.bazel.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gazelle/manifest/defs.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def gazelle_python_manifest(
3939
manifest, meaning testing it is just as expensive as generating it,
4040
but modifying it is much less likely to result in a merge conflict.
4141
pip_repository_name: the name of the pip_install or pip_repository target.
42-
pip_deps_repository_name: deprecated - the old pip_install target name.
42+
pip_deps_repository_name: deprecated - the old {bzl:obj}`pip_parse` target name.
4343
manifest: the Gazelle manifest file.
4444
defaults to the same value as manifest.
4545
**kwargs: other bazel attributes passed to the generate and test targets

gazelle/manifest/generate/generate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func main() {
5555
&pipRepositoryName,
5656
"pip-repository-name",
5757
"",
58-
"The name of the pip_install or pip_repository target.")
58+
"The name of the pip_parse or pip.parse target.")
5959
flag.StringVar(
6060
&modulesMappingPath,
6161
"modules-mapping",

internal_setup.bzl

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def rules_python_internal_setup():
4343
python_versions = sorted(TOOL_VERSIONS.keys()),
4444
)
4545

46-
# Because we don't use the pip_install rule, we have to call this to fetch its deps
4746
pypi_deps()
4847

4948
bazel_skylib_workspace()

python/extensions/pip.bzl

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"pip module extension for use with bzlmod"
15+
"""
16+
This is the successor to {bzl:obj}`pip_parse` for including third party PyPI dependencies into your bazel module using `bzlmod`.
17+
18+
:::{seealso}
19+
For user documentation see the [PyPI dependencies section](pypi-dependencies).
20+
:::
21+
"""
1622

1723
load("//python/private/pypi:pip.bzl", _pip = "pip")
1824

python/pip.bzl

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ This contains a set of rules that are used to support inclusion of third-party
1717
dependencies via fully locked `requirements.txt` files. Some of the exported
1818
symbols should not be used and they are either undocumented here or marked as
1919
for internal use only.
20+
21+
If you are using a bazel version 7 or above with `bzlmod`, you should only care
22+
about the {bzl:obj}`compile_pip_requirements` macro exposed in this file. The
23+
rest of the symbols are for legacy `WORKSPACE` setups.
2024
"""
2125

2226
load("//python/private:normalize_name.bzl", "normalize_name")

python/private/pypi/extension.bzl

+2-4
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,7 @@ the BUILD files for wheels.
764764
attrs = _pip_parse_ext_attrs(),
765765
doc = """\
766766
This tag class is used to create a pip hub and all of the spokes that are part of that hub.
767-
This tag class reuses most of the pip attributes that are found in
768-
@rules_python//python/pip_install:pip_repository.bzl.
767+
This tag class reuses most of the attributes found in {bzl:obj}`pip_parse`.
769768
The exception is it does not use the arg 'repo_prefix'. We set the repository
770769
prefix for the user and the alias arg is always True in bzlmod.
771770
""",
@@ -814,8 +813,7 @@ the BUILD files for wheels.
814813
),
815814
doc = """\
816815
This tag class is used to create a pypi hub and all of the spokes that are part of that hub.
817-
This tag class reuses most of the pypi attributes that are found in
818-
@rules_python//python/pip_install:pip_repository.bzl.
816+
This tag class reuses most of the attributes found in {bzl:obj}`pip_parse`.
819817
The exception is it does not use the arg 'repo_prefix'. We set the repository
820818
prefix for the user and the alias arg is always True in bzlmod.
821819
""",

0 commit comments

Comments
 (0)