-
-
Notifications
You must be signed in to change notification settings - Fork 617
docs(gazelle): Migrate Gazelle docs to ReadTheDocs, part 2/5: installation and usage #3132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dougthor42
merged 2 commits into
bazel-contrib:main
from
dougthor42:u/dthor/gazelle-refactor-docs-pt2-install
Aug 4, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
# Installation and Usage | ||
|
||
## Example | ||
|
||
Examples of using Gazelle with Python can be found in the `rules_python` | ||
repo: | ||
|
||
* bzlmod: {gh-path}`examples/bzlmod_build_file_generation` | ||
* WORKSPACE: {gh-path}`examples/build_file_generation` | ||
|
||
:::{note} | ||
The following documentation covers using bzlmod. | ||
::: | ||
|
||
|
||
## Adding Gazelle to your project | ||
|
||
First, you'll need to add Gazelle to your `MODULE.bazel` file. Get the current | ||
version of Gazelle from their [releases page][gazelle-releases]. | ||
|
||
[gazelle-releases]: https://github.com/bazel-contrib/bazel-gazelle/releases/ | ||
|
||
See the installation `MODULE.bazel` snippet on the `rules_python` | ||
[releases page][rules-python-releases] in order to configure `rules_python`. | ||
|
||
[rules-python-releases]: https://github.com/bazel-contrib/rules_python/releases | ||
|
||
You will also need to add the {bzl:obj}`bazel_dep` for configuration for | ||
`rules_python_gazelle_plugin`. | ||
|
||
Here is a snippet of a `MODULE.bazel` file. | ||
|
||
```starlark | ||
# The following stanza defines the dependency rules_python. | ||
bazel_dep(name = "rules_python", version = "0.22.0") | ||
|
||
# The following stanza defines the dependency rules_python_gazelle_plugin. | ||
# For typical setups you set the version. | ||
bazel_dep(name = "rules_python_gazelle_plugin", version = "0.22.0") | ||
|
||
# The following stanza defines the dependency gazelle. | ||
bazel_dep(name = "gazelle", version = "0.31.0", repo_name = "bazel_gazelle") | ||
aignas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Import the python repositories generated by the given module extension into | ||
# the scope of the current module. | ||
use_repo(python, "python3_9") | ||
use_repo(python, "python3_9_toolchains") | ||
|
||
# Register an already-defined toolchain so that Bazel can use it during | ||
# toolchain resolution. | ||
register_toolchains( | ||
"@python3_9_toolchains//:all", | ||
) | ||
|
||
# Use the pip extension | ||
pip = use_extension("@rules_python//python:extensions.bzl", "pip") | ||
|
||
# Use the extension to call the `pip_repository` rule that invokes `pip`, with | ||
# `incremental` set. | ||
# Accepts a locked/compiled requirements file and installs the dependencies listed within. | ||
# Those dependencies become available in a generated `requirements.bzl` file. | ||
# You can instead check this `requirements.bzl` file into your repo. | ||
# Because this project has different requirements for windows vs other | ||
# operating systems, we have requirements for each. | ||
pip.parse( | ||
name = "pip", | ||
requirements_lock = "//:requirements_lock.txt", | ||
requirements_windows = "//:requirements_windows.txt", | ||
) | ||
|
||
# Imports the pip toolchain generated by the given module extension into the | ||
# scope of the current module. | ||
use_repo(pip, "pip") | ||
``` | ||
|
||
Next, we'll fetch metadata about your Python dependencies, so that gazelle can | ||
determine which package a given import statement comes from. This is provided | ||
by the {bzl:obj}`modules_mapping` rule. We'll make a target for consuming this | ||
{bzl:obj}`modules_mapping`, and writing it as a manifest file for Gazelle to read. | ||
dougthor42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This is checked into the repo for speed, as it takes some time to calculate | ||
in a large monorepo. | ||
|
||
Gazelle will walk up the filesystem from a Python file to find this metadata, | ||
looking for a file called `gazelle_python.yaml` in an ancestor folder | ||
of the Python code. Create an empty file with this name. It might be next | ||
to your `requirements.txt` file. (You can just use {command}`touch` at | ||
this point, it just needs to exist.) | ||
|
||
To keep the metadata updated, put this in your `BUILD.bazel` file next | ||
to `gazelle_python.yaml`: | ||
|
||
```starlark | ||
load("@pip//:requirements.bzl", "all_whl_requirements") | ||
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest") | ||
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping") | ||
|
||
# This rule fetches the metadata for python packages we depend on. That data is | ||
# required for the gazelle_python_manifest rule to update our manifest file. | ||
modules_mapping( | ||
name = "modules_map", | ||
wheels = all_whl_requirements, | ||
) | ||
|
||
# Gazelle python extension needs a manifest file mapping from | ||
# an import to the installed package that provides it. | ||
# This macro produces two targets: | ||
# - //:gazelle_python_manifest.update can be used with `bazel run` | ||
# to recalculate the manifest | ||
# - //:gazelle_python_manifest.test is a test target ensuring that | ||
# the manifest doesn't need to be updated | ||
gazelle_python_manifest( | ||
name = "gazelle_python_manifest", | ||
modules_mapping = ":modules_map", | ||
# This is what we called our `pip_parse` rule, where third-party | ||
# python libraries are loaded in BUILD files. | ||
pip_repository_name = "pip", | ||
# This should point to wherever we declare our python dependencies | ||
# (the same as what we passed to the modules_mapping rule in WORKSPACE) | ||
# This argument is optional. If provided, the `.test` target is very | ||
# fast because it just has to check an integrity field. If not provided, | ||
# the integrity field is not added to the manifest which can help avoid | ||
# merge conflicts in large repos. | ||
requirements = "//:requirements_lock.txt", | ||
# include_stub_packages: bool (default: False) | ||
# If set to True, this flag automatically includes any corresponding type stub packages | ||
# for the third-party libraries that are present and used. For example, if you have | ||
# `boto3` as a dependency, and this flag is enabled, the corresponding `boto3-stubs` | ||
# package will be automatically included in the BUILD file. | ||
# | ||
# Enabling this feature helps ensure that type hints and stubs are readily available | ||
# for tools like type checkers and IDEs, improving the development experience and | ||
# reducing manual overhead in managing separate stub packages. | ||
include_stub_packages = True | ||
) | ||
``` | ||
|
||
Finally, you create a target that you'll invoke to run the Gazelle tool | ||
with the `rules_python` extension included. This typically goes in your root | ||
`/BUILD.bazel` file: | ||
|
||
```starlark | ||
load("@bazel_gazelle//:def.bzl", "gazelle") | ||
|
||
# Our gazelle target points to the python gazelle binary. | ||
# This is the simple case where we only need one language supported. | ||
# If you also had proto, go, or other gazelle-supported languages, | ||
# you would also need a gazelle_binary rule. | ||
# See https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.rst#example | ||
gazelle( | ||
name = "gazelle", | ||
gazelle = "@rules_python_gazelle_plugin//python:gazelle_binary", | ||
) | ||
``` | ||
|
||
That's it, now you can finally run `bazel run //:gazelle` anytime | ||
you edit Python code, and it should update your `BUILD` files correctly. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.