bazelbuild / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.18k stars 374 forks source link

Gazelle gets confused if directories already contain both BUILD and .pb.go files #1769

Open EdSchouten opened 5 months ago

EdSchouten commented 5 months ago

What version of gazelle are you using?

0.35.0

What version of rules_go are you using?

0.46.0

What version of Bazel are you using?

7.1.1

Does this issue reproduce with the latest releases of all the above?

Yes

What operating system and processor architecture are you using?

macOS Intel

What did you do?

git clone git@github.com:buildbarn/bb-storage.git
cd bb-storage
bazel run //:gazelle
git status

What did you expect to see?

That the Git checkout remains unmodified.

What did you see instead?

I see that Gazelle prints a whole bunch of warnings having this shape:

gazelle: rule //cmd/bb_copy:bb_copy_lib imports "github.com/buildbarn/bb-storage/pkg/proto/configuration/bb_copy" which matches multiple rules: //pkg/proto/configuration/bb_copy:buildbarn_configuration_bb_copy_go_proto and //pkg/proto/configuration/bb_copy. # gazelle:resolve may be used to disambiguate

I also see that all of my BUILD files for packages containing Protobuf files are altered to no longer depend on the go_proto_library(), but on the existing .pb.go files:

diff --git a/pkg/proto/replicator/BUILD.bazel b/pkg/proto/replicator/BUILD.bazel
index 1f327f4..99e9ed0 100644
--- a/pkg/proto/replicator/BUILD.bazel
+++ b/pkg/proto/replicator/BUILD.bazel
@@ -26,7 +26,19 @@ go_proto_library(

 go_library(
     name = "replicator",
-    embed = [":replicator_go_proto"],
+    srcs = [
+        "replicator.pb.go",
+        "replicator_grpc.pb.go",
+    ],
     importpath = "github.com/buildbarn/bb-storage/pkg/proto/replicator",
     visibility = ["//visibility:public"],
+    deps = [
+        "@com_github_bazelbuild_remote_apis//build/bazel/remote/execution/v2:execution",
+        "@org_golang_google_grpc//:go_default_library",
+        "@org_golang_google_grpc//codes",
+        "@org_golang_google_grpc//status",
+        "@org_golang_google_protobuf//reflect/protoreflect",
+        "@org_golang_google_protobuf//runtime/protoimpl",
+        "@org_golang_google_protobuf//types/known/emptypb",
+    ],
 )

Furthermore, all code that depends on these libraries have their dependencies removed.

Oddly enough, if I first remove the BUILD files belonging to the Protobuf files and then run Gazelle, everything works as expected:

rm -f $(find . -name '*.pb.go' | sed -e 's/[^/]*$/BUILD.bazel/')
bazel run //:gazelle
git status

It's really just that if both the BUILD file AND the .pb.go source files are present, that Bazel misbehaves. In my case I want to check in the resulting .pb.go files as well, so that third parties can easily depend on this repository. This did work as expected in previous versions of Bazel/Gazelle (that did not use bzlmod).

EdSchouten commented 2 weeks ago

Note that the description of #1802 says that it fixes this issue, it does not. This issue is still reproducible with the latest version of Gazelle.