@@ -16,67 +16,53 @@ The following documentation covers using bzlmod.
1616## Adding Gazelle to your project
1717
1818First, you'll need to add Gazelle to your ` MODULE.bazel ` file. Get the current
19- version of Gazelle from their [ releases page] [ gazelle-releases ] .
19+ version of [ Gazelle] [ bcr-gazelle ] from the [ Bazel Central Registry] [ bcr ] . Then
20+ do the same for [ ` rules_python ` ] [ bcr-rules-python ] and
21+ [ ` rules_python_gazelle_plugin ` ] [ bcr-rules-python-gazelle-plugin ] .
2022
21- [ gazelle-releases ] : https://github.com/bazel-contrib/bazel-gazelle/releases/
23+ [ bcr-gazelle ] : https://registry.bazel.build/modules/gazelle
24+ [ bcr ] : https://registry.bazel.build/
25+ [ bcr-rules-python ] : https://registry.bazel.build/modules/rules_python
26+ [ bcr-rules-python-gazelle-plugin ] : https://registry.bazel.build/modules/rules_python_gazelle_plugin
2227
23- See the installation ` MODULE.bazel ` snippet on the ` rules_python `
24- [ releases page] [ rules-python-releases ] in order to configure ` rules_python ` .
25-
26- [ rules-python-releases ] : https://github.com/bazel-contrib/rules_python/releases
27-
28- You will also need to add the {bzl: obj }` bazel_dep ` for configuration for
29- ` rules_python_gazelle_plugin ` .
30-
31- Here is a snippet of a ` MODULE.bazel ` file.
28+ Here is a snippet of a ` MODULE.bazel ` file. Note that most of it is just
29+ general config for ` rules_python ` itself - the Gazelle plugin is only two lines
30+ at the end.
3231
3332``` starlark
34- # The following stanza defines the dependency rules_python.
35- bazel_dep(name = " rules_python" , version = " 0.22.0" )
36-
37- # The following stanza defines the dependency rules_python_gazelle_plugin.
38- # For typical setups you set the version.
39- bazel_dep(name = " rules_python_gazelle_plugin" , version = " 0.22.0" )
33+ # ###############################################
34+ # # START rules_python CONFIG ##
35+ # # See the main rules_python docs for details ##
36+ # ###############################################
37+ bazel_dep(name = " rules_python" , version = " 1.5.1" )
4038
41- # The following stanza defines the dependency gazelle.
42- bazel_dep(name = " gazelle" , version = " 0.31.0" , repo_name = " bazel_gazelle" )
39+ python = use_extension(" @rules_python//python/extensions:python.bzl" , " python" )
40+ python.toolchain(python_version = " 3.12.2" )
41+ use_repo(python, " python_3_12_2" )
4342
44- # Import the python repositories generated by the given module extension into
45- # the scope of the current module.
46- use_repo(python, " python3_9" )
47- use_repo(python, " python3_9_toolchains" )
48-
49- # Register an already-defined toolchain so that Bazel can use it during
50- # toolchain resolution.
51- register_toolchains(
52- " @python3_9_toolchains//:all" ,
53- )
54-
55- # Use the pip extension
5643pip = use_extension(" @rules_python//python:extensions.bzl" , " pip" )
57-
58- # Use the extension to call the `pip_repository` rule that invokes `pip`, with
59- # `incremental` set.
60- # Accepts a locked/compiled requirements file and installs the dependencies listed within.
61- # Those dependencies become available in a generated `requirements.bzl` file.
62- # You can instead check this `requirements.bzl` file into your repo.
63- # Because this project has different requirements for windows vs other
64- # operating systems, we have requirements for each.
6544pip.parse(
66- name = " pip" ,
45+ hub_name = " pip" ,
6746 requirements_lock = " //:requirements_lock.txt" ,
6847 requirements_windows = " //:requirements_windows.txt" ,
6948)
70-
71- # Imports the pip toolchain generated by the given module extension into the
72- # scope of the current module.
7349use_repo(pip, " pip" )
50+
51+ # #############################################
52+ # # START rules_python_gazelle_plugin CONFIG ##
53+ # #############################################
54+
55+ # The Gazelle plugin depends on Gazelle.
56+ bazel_dep(name = " gazelle" , version = " 0.33.0" , repo_name = " bazel_gazelle" )
57+
58+ # Typically rules_python_gazelle_plugin is version matched to rules_python.
59+ bazel_dep(name = " rules_python_gazelle_plugin" , version = " 1.5.1" )
7460```
7561
7662Next, we'll fetch metadata about your Python dependencies, so that gazelle can
7763determine which package a given import statement comes from. This is provided
78- by the {bzl : obj } ` modules_mapping ` rule. We'll make a target for consuming this
79- {bzl : obj } ` modules_mapping ` , and writing it as a manifest file for Gazelle to read.
64+ by the ` modules_mapping ` rule. We'll make a target for consuming this
65+ ` modules_mapping ` , and writing it as a manifest file for Gazelle to read.
8066This is checked into the repo for speed, as it takes some time to calculate
8167in a large monorepo.
8268
@@ -90,6 +76,7 @@ To keep the metadata updated, put this in your `BUILD.bazel` file next
9076to ` gazelle_python.yaml ` :
9177
9278``` starlark
79+ # `@pip` is the hub_name from pip.parse in MODULE.bazel.
9380load(" @pip//:requirements.bzl" , " all_whl_requirements" )
9481load(" @rules_python_gazelle_plugin//manifest:defs.bzl" , " gazelle_python_manifest" )
9582load(" @rules_python_gazelle_plugin//modules_mapping:def.bzl" , " modules_mapping" )
@@ -111,22 +98,24 @@ modules_mapping(
11198gazelle_python_manifest(
11299 name = " gazelle_python_manifest" ,
113100 modules_mapping = " :modules_map" ,
114- # This is what we called our `pip_parse` rule, where third-party
101+
102+ # This is what we called our `pip.parse` rule in MODULE.bazel, where third-party
115103 # python libraries are loaded in BUILD files.
116104 pip_repository_name = " pip" ,
105+
117106 # This should point to wherever we declare our python dependencies
118107 # (the same as what we passed to the modules_mapping rule in WORKSPACE)
119108 # This argument is optional. If provided, the `.test` target is very
120109 # fast because it just has to check an integrity field. If not provided,
121110 # the integrity field is not added to the manifest which can help avoid
122111 # merge conflicts in large repos.
123112 requirements = " //:requirements_lock.txt" ,
113+
124114 # include_stub_packages: bool (default: False)
125115 # If set to True, this flag automatically includes any corresponding type stub packages
126116 # for the third-party libraries that are present and used. For example, if you have
127117 # `boto3` as a dependency, and this flag is enabled, the corresponding `boto3-stubs`
128118 # package will be automatically included in the BUILD file.
129- #
130119 # Enabling this feature helps ensure that type hints and stubs are readily available
131120 # for tools like type checkers and IDEs, improving the development experience and
132121 # reducing manual overhead in managing separate stub packages.
@@ -139,16 +128,22 @@ with the `rules_python` extension included. This typically goes in your root
139128` /BUILD.bazel ` file:
140129
141130``` starlark
142- load(" @bazel_gazelle//:def.bzl" , " gazelle" )
131+ load(" @bazel_gazelle//:def.bzl" , " gazelle" , " gazelle_binary" )
132+
133+ gazelle_binary(
134+ name = " gazelle_multilang" ,
135+ languages = [
136+ # List of language plugins.
137+ # If you want to generate py_proto_library targets PR #3057), then
138+ # the proto language plugin _must_ come before the rules_python plugin.
139+ # "@bazel_gazelle//lanugage/proto",
140+ " @rules_python_gazelle_plugin//python" ,
141+ ],
142+ )
143143
144- # Our gazelle target points to the python gazelle binary.
145- # This is the simple case where we only need one language supported.
146- # If you also had proto, go, or other gazelle-supported languages,
147- # you would also need a gazelle_binary rule.
148- # See https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.rst#example
149144gazelle(
150145 name = " gazelle" ,
151- gazelle = " @rules_python_gazelle_plugin//python:gazelle_binary " ,
146+ gazelle = " :gazelle_multilang " ,
152147)
153148```
154149
0 commit comments