Skip to content

Commit 03ebeb7

Browse files
authored
docs: Updating documentation for bzlmod (#1149)
- Updated primary README.md to include documentation for using bzlmod or a WORKSPACE file. - Updated gazelle/README.md to include documentation for only using bzlmod and provided a link to the older docs. - Included other general updates for the gazelle documentation.
1 parent 64684ae commit 03ebeb7

File tree

2 files changed

+130
-25
lines changed

2 files changed

+130
-25
lines changed

README.md

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,47 @@ contribute](CONTRIBUTING.md) page for information on our development workflow.
3333

3434
## Getting started
3535

36+
The next two sections cover using `rules_python` with bzlmod and
37+
the older way of configuring bazel with a `WORKSPACE` file.
38+
39+
### Using bzlmod
40+
41+
To import rules_python in your project, you first need to add it to your
42+
`MODULES.bazel` file, using the snippet provided in the
43+
[release you choose](https://github.com/bazelbuild/rules_python/releases).
44+
45+
#### Toolchain registration with bzlmod
46+
47+
To register a hermetic Python toolchain rather than rely on a system-installed interpreter for runtime execution, you can add to the `MODULES.bazel` file:
48+
49+
```python
50+
# Find the latest version number here: https://github.com/bazelbuild/rules_python/releases
51+
# and change the version number if needed in the line below.
52+
bazel_dep(name = "rules_python", version = "0.20.0")
53+
54+
# You do not have to use pip for the toolchain, but most people
55+
# will use it for the dependency management.
56+
pip = use_extension("@rules_python//python:extensions.bzl", "pip")
57+
58+
pip.parse(
59+
name = "pip",
60+
requirements_lock = "//:requirements_lock.txt",
61+
)
62+
63+
use_repo(pip, "pip")
64+
65+
# Register a specific python toolchain instead of using the host version
66+
python = use_extension("@rules_python//python:extensions.bzl", "python")
67+
68+
use_repo(python, "python3_10_toolchains")
69+
70+
register_toolchains(
71+
"@python3_10_toolchains//:all",
72+
)
73+
```
74+
75+
### Using a WORKSPACE file
76+
3677
To import rules_python in your project, you first need to add it to your
3778
`WORKSPACE` file, using the snippet provided in the
3879
[release you choose](https://github.com/bazelbuild/rules_python/releases)
@@ -53,7 +94,7 @@ http_archive(
5394
)
5495
```
5596

56-
### Toolchain registration
97+
#### Toolchain registration
5798

5899
To register a hermetic Python toolchain rather than rely on a system-installed interpreter for runtime execution, you can add to the `WORKSPACE` file:
59100

@@ -118,6 +159,22 @@ target in the appropriate wheel repo.
118159

119160
### Installing third_party packages
120161

162+
#### Using bzlmod
163+
164+
To add pip dependencies to your `MODULES.bazel` file, use the `pip.parse` extension, and call it to create the
165+
central external repo and individual wheel external repos.
166+
167+
```python
168+
pip.parse(
169+
name = "my_deps",
170+
requirements_lock = "//:requirements_lock.txt",
171+
)
172+
173+
use_repo(pip, "my_deps")
174+
```
175+
176+
#### Using a WORKSPACE file
177+
121178
To add pip dependencies to your `WORKSPACE`, load the `pip_parse` function, and call it to create the
122179
central external repo and individual wheel external repos.
123180

@@ -137,14 +194,15 @@ load("@my_deps//:requirements.bzl", "install_deps")
137194
install_deps()
138195
```
139196

197+
#### pip rules
198+
140199
Note that since `pip_parse` is a repository rule and therefore executes pip at WORKSPACE-evaluation time, Bazel has no
141200
information about the Python toolchain and cannot enforce that the interpreter
142201
used to invoke pip matches the interpreter used to run `py_binary` targets. By
143202
default, `pip_parse` uses the system command `"python3"`. This can be overridden by passing the
144203
`python_interpreter` attribute or `python_interpreter_target` attribute to `pip_parse`.
145204

146-
You can have multiple `pip_parse`s in the same workspace. This will create multiple external repos that have no relation to
147-
one another, and may result in downloading the same wheels multiple times.
205+
You can have multiple `pip_parse`s in the same workspace. This will create multiple external repos that have no relation to one another, and may result in downloading the same wheels multiple times.
148206

149207
As with any repository rule, if you would like to ensure that `pip_parse` is
150208
re-executed in order to pick up a non-hermetic change to your environment (e.g.,

gazelle/README.md

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
11
# Python Gazelle plugin
22

3+
[Gazelle](https://github.com/bazelbuild/bazel-gazelle)
4+
is a build file generator for Bazel projects. It can create new BUILD.bazel files for a project that follows language conventions, and it can update existing build files to include new sources, dependencies, and options.
5+
6+
Gazelle may be run by Bazel using the gazelle rule, or it may be installed and run as a command line tool.
7+
38
This directory contains a plugin for
49
[Gazelle](https://github.com/bazelbuild/bazel-gazelle)
5-
that generates BUILD file content for Python code.
10+
that generates BUILD files content for Python code.
11+
12+
The following instructions are for when you use [bzlmod](https://docs.bazel.build/versions/5.0.0/bzlmod.html).
13+
Please refer to older documentation that includes instructions on how to use Gazelle
14+
without using bzlmod as your dependency manager.
15+
16+
## Example
617

7-
It requires Go 1.16+ to compile.
18+
We have an example of using Gazelle with Python located [here](https://github.com/bazelbuild/rules_python/tree/main/examples/build_file_generation).
819

9-
## Installation
20+
## Adding Gazelle to your project
1021

11-
First, you'll need to add Gazelle to your `WORKSPACE` file.
12-
Follow the instructions at https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
22+
First, you'll need to add Gazelle to your `MODULES.bazel` file.
23+
Get the current version of Gazelle from there releases here: https://github.com/bazelbuild/bazel-gazelle/releases/.
1324

14-
Next, we need to fetch the third-party Go libraries that the python extension
15-
depends on.
1625

17-
See the installation `WORKSPACE` snippet on the Releases page:
18-
https://github.com/bazelbuild/rules_python/releases
26+
See the installation `MODULE.bazel` snippet on the Releases page:
27+
https://github.com/bazelbuild/rules_python/releases in order to configure rules_python.
28+
29+
You will also need to add the `bazel_dep` for configuration for `rules_python_gazelle_plugin`.
30+
31+
Here is a snippet of a `MODULE.bazel` file.
32+
33+
```starlark
34+
# The following stanza defines the dependency rules_python.
35+
bazel_dep(name = "rules_python", version = "0.20.0")
36+
37+
# The following stanza defines the dependency rules_python.
38+
# For typical setups you set the version.
39+
bazel_dep(name = "rules_python_gazelle_plugin", version = "0.20.0")
40+
41+
# The following stanza defines the dependency rules_python.
42+
bazel_dep(name = "gazelle", version = "0.30.0", repo_name = "bazel_gazelle")
43+
```
44+
You will also need to do the other usual configuration for `rules_python` in your
45+
`MODULE.bazel` file.
1946

2047
Next, we'll fetch metadata about your Python dependencies, so that gazelle can
2148
determine which package a given import statement comes from. This is provided
@@ -157,11 +184,29 @@ Next, all source files are collected into the `srcs` of the `py_library`.
157184
Finally, the `import` statements in the source files are parsed, and
158185
dependencies are added to the `deps` attribute.
159186

160-
### Tests
187+
### Unit Tests
188+
189+
A `py_test` target is added to the BUILD file when gazelle encounters
190+
a file named `__test__.py`.
191+
Often, Python unit test files are named with the suffix `_test`.
192+
For example, if we had a folder that is a package named "foo" we could have a Python file named `foo_test.py`
193+
and gazelle would create a `py_test` block for the file.
161194

162-
Python test files are those ending in `_test.py`.
195+
The following is an example of a `py_test` target that gazelle would add when
196+
it encounters a file named `__test__.py`.
197+
198+
```starlark
199+
py_test(
200+
name = "build_file_generation_test",
201+
srcs = ["__test__.py"],
202+
main = "__test__.py",
203+
deps = [":build_file_generation"],
204+
)
205+
```
163206

164-
A `py_test` target is added containing all test files as `srcs`.
207+
You can control the naming convention for test targets by adding a gazelle directive named
208+
`# gazelle:python_test_naming_convention`. See the instructions in the section above that
209+
covers directives.
165210

166211
### Binaries
167212

@@ -170,16 +215,18 @@ of a Python program.
170215

171216
A `py_binary` target will be created, named `[package]_bin`.
172217

173-
## Developing on the extension
218+
## Developer Notes
174219

175-
Gazelle extensions are written in Go. Ours is a hybrid, which also spawns
176-
a Python interpreter as a subprocess to parse python files.
220+
Gazelle extensions are written in Go. This gazelle plugin is a hybrid, as it uses Go to execute a
221+
Python interpreter as a subprocess to parse Python source files.
222+
See the gazelle documentation https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.md
223+
for more information on extending Gazelle.
177224

178-
The Go dependencies are managed by the go.mod file.
179-
After changing that file, run `go mod tidy` to get a `go.sum` file,
180-
then run `bazel run //:update_go_deps` to convert that to the `gazelle/deps.bzl` file.
181-
The latter is loaded in our `/WORKSPACE` to define the external repos
182-
that we can load Go dependencies from.
225+
If you add new Go dependencies to the plugin source code, you need to "tidy" the go.mod file.
226+
After changing that file, run `go mod tidy` or `bazel run @go_sdk//:bin/go -- mod tidy`
227+
to update the go.mod and go.sum files. Then run `bazel run //:update_go_deps` to have gazelle
228+
add the new dependenies to the deps.bzl file. The deps.bzl file is used as defined in our /WORKSPACE
229+
to include the external repos Bazel loads Go dependencies from.
183230

184-
Then after editing Go code, run `bazel run //:gazelle` to generate/update
185-
go_* rules in the BUILD.bazel files in our repo.
231+
Then after editing Go code, run `bazel run //:gazelle` to generate/update the rules in the
232+
BUILD.bazel files in our repo.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy