Skip to content

Commit 2ed714f

Browse files
authored
fix(gazelle): Do not build proto targets with default Gazelle (#3216)
Fixes #3209. Revert the change to `//:gazelle_binary` so that it once again only generates python code. We then create a new, private target `//:_gazelle_binary_with_proto` that gets used by tests. Update docs accordingly. Longer term, I'd like to adjust the `test.yaml` file to include a section: ```yaml config: gazelle_binary: _gazelle_binary_with_proto ``` So that test cases that need to generate `(py_)proto_library` targets can use the multi-lang Gazelle binary and that tests that do _not_ need to generate proto targets can use the single-lang Gazelle binary. However, there were some minor roadblocks in doing so and thus I'm doing this quick-to-implement method instead.
1 parent cebfc9d commit 2ed714f

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ END_UNRELEASED_TEMPLATE
182182
dep is not added to the {obj}`py_test` target.
183183
* (gazelle) New directive `gazelle:python_generate_proto`; when `true`,
184184
Gazelle generates `py_proto_library` rules for `proto_library`. `false` by default.
185+
* Note: Users must manually configure their Gazelle target to support the
186+
proto language.
185187
* (gazelle) New directive `gazelle:python_proto_naming_convention`; controls
186188
naming of `py_proto_library` rules.
187189

gazelle/docs/directives.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,30 @@ the configured name for the `@protobuf` / `@com_google_protobuf` repo in your
636636
`MODULE.bazel`, and otherwise falling back to `@com_google_protobuf` for
637637
compatibility with `WORKSPACE`.
638638

639+
:::{note}
640+
In order to use this, you must manually configure Gazelle to target multiple
641+
languages. Place this in your root `BUILD.bazel` file:
642+
643+
```
644+
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")
645+
646+
gazelle_binary(
647+
name = "gazelle_multilang",
648+
languages = [
649+
"@bazel_gazelle//language/proto",
650+
# The python gazelle plugin must be listed _after_ the proto language.
651+
"@rules_python_gazelle_plugin//python",
652+
],
653+
)
654+
655+
gazelle(
656+
name = "gazelle",
657+
gazelle = "//:gazelle_multilang",
658+
)
659+
```
660+
:::
661+
662+
639663
For example, in a package with `# gazelle:python_generate_proto true` and a
640664
`foo.proto`, if you have both the proto extension and the Python extension
641665
loaded into Gazelle, you'll get something like:

gazelle/python/BUILD.bazel

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ gazelle_test(
7070
name = "python_test",
7171
srcs = ["python_test.go"],
7272
data = [
73+
":_gazelle_binary_with_proto",
7374
":gazelle_binary",
7475
],
7576
test_dirs = glob(
@@ -90,11 +91,18 @@ gazelle_test(
9091

9192
gazelle_binary(
9293
name = "gazelle_binary",
94+
languages = [":python"],
95+
visibility = ["//visibility:public"],
96+
)
97+
98+
# Only used by testing
99+
gazelle_binary(
100+
name = "_gazelle_binary_with_proto",
93101
languages = [
94102
"@bazel_gazelle//language/proto",
95103
":python",
96104
],
97-
visibility = ["//visibility:public"],
105+
visibility = ["//visibility:private"],
98106
)
99107

100108
filegroup(

gazelle/python/python_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
const (
3939
extensionDir = "python" + string(os.PathSeparator)
4040
testDataPath = extensionDir + "testdata" + string(os.PathSeparator)
41-
gazelleBinaryName = "gazelle_binary"
41+
gazelleBinaryName = "_gazelle_binary_with_proto"
4242
)
4343

4444
func TestGazelleBinary(t *testing.T) {

0 commit comments

Comments
 (0)