Skip to content

Commit 4dcfd21

Browse files
authored
docs(gazelle): Add details about python_root directive. (#1782)
While figuring out how to use gazelle with a `src` dir (see #1775) (I figured it out by the way, hence this PR), I noticed that the docs were a little unclear. This PR updates the `gazelle` docs to include details about the `python_root` directive and how to use it. I also fix a the default value for the `python_generation_mode_per_file_include_init` directive, as it was incorrectly listed as "package".
1 parent 627830e commit 4dcfd21

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

gazelle/README.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ Python-specific directives are as follows:
176176
|--------------------------------------|-------------------|
177177
| `# gazelle:python_extension` | `enabled` |
178178
| Controls whether the Python extension is enabled or not. Sub-packages inherit this value. Can be either "enabled" or "disabled". | |
179-
| `# gazelle:python_root` | n/a |
180-
| Sets a Bazel package as a Python root. This is used on monorepos with multiple Python projects that don't share the top-level of the workspace as the root. | |
179+
| [`# gazelle:python_root`](#directive-python_root) | n/a |
180+
| Sets a Bazel package as a Python root. This is used on monorepos with multiple Python projects that don't share the top-level of the workspace as the root. See [Directive: `python_root`](#directive-python_root) below. | |
181181
| `# gazelle:python_manifest_file_name`| `gazelle_python.yaml` |
182182
| Overrides the default manifest file name. | |
183183
| `# gazelle:python_ignore_files` | n/a |
@@ -188,7 +188,7 @@ Python-specific directives are as follows:
188188
| Controls whether the Python import statements should be validated. Can be "true" or "false" | |
189189
| `# gazelle:python_generation_mode`| `package` |
190190
| Controls the target generation mode. Can be "file", "package", or "project" | |
191-
| `# gazelle:python_generation_mode_per_file_include_init`| `package` |
191+
| `# gazelle:python_generation_mode_per_file_include_init`| `false` |
192192
| Controls whether `__init__.py` files are included as srcs in each generated target when target generation mode is "file". Can be "true", or "false" | |
193193
| `# gazelle:python_library_naming_convention`| `$package_name$` |
194194
| Controls the `py_library` naming convention. It interpolates `$package_name$` with the Bazel package name. E.g. if the Bazel package name is `foo`, setting this to `$package_name$_my_lib` would result in a generated target named `foo_my_lib`. | |
@@ -199,6 +199,43 @@ Python-specific directives are as follows:
199199
| `# gazelle:resolve py ...` | n/a |
200200
| Instructs the plugin what target to add as a dependency to satisfy a given import statement. The syntax is `# gazelle:resolve py import-string label` where `import-string` is the symbol in the python `import` statement, and `label` is the Bazel label that Gazelle should write in `deps`. | |
201201

202+
203+
#### Directive: `python_root`:
204+
205+
Set this directive within the Bazel package that you want to use as the Python root.
206+
For example, if using a `src` dir (as recommended by the [Python Packaging User
207+
Guide][python-packaging-user-guide]), then set this directive in `src/BUILD.bazel`:
208+
209+
```starlark
210+
# ./src/BUILD.bazel
211+
# Tell gazelle that are python root is the same dir as this Bazel package.
212+
# gazelle:python_root
213+
```
214+
215+
Note that the directive does not have any arguments.
216+
217+
Gazelle will then add the necessary `imports` attribute to all targets that it
218+
generates:
219+
220+
```starlark
221+
# in ./src/foo/BUILD.bazel
222+
py_libary(
223+
...
224+
imports = [".."], # Gazelle adds this
225+
...
226+
)
227+
228+
# in ./src/foo/bar/BUILD.bazel
229+
py_libary(
230+
...
231+
imports = ["../.."], # Gazelle adds this
232+
...
233+
)
234+
```
235+
236+
[python-packaging-user-guide]: https://github.com/pypa/packaging.python.org/blob/4c86169a/source/tutorials/packaging-projects.rst
237+
238+
202239
### Libraries
203240

204241
Python source files are those ending in `.py` but not ending in `_test.py`.

0 commit comments

Comments
 (0)