Skip to content

Commit

Permalink
do not generate an empty rule if a go_proto_library was already gener… (
Browse files Browse the repository at this point in the history
#1927)

**What type of PR is this?**
> Bug fix

**What package or component does this PR mostly affect?**

> language/go

**What does this PR do? Why is it needed?**

Prevents overwriting of a generated `go_proto_library` in the case where
the package name matches the proto file name. See issue for more
details.

**Which issues(s) does this PR fix?**
Fixes #1926

**Other notes for review**
Unclear exactly how to write a test for it, but did local testing at
least.
  • Loading branch information
davidbyttow authored Oct 3, 2024
1 parent 293a67e commit 27bbbc6
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 1 deletion.
27 changes: 26 additions & 1 deletion language/go/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,36 @@ func (gl *goLang) GenerateRules(args language.GenerateArgs) language.GenerateRes
}
}

var genGoProtoRules []string
for _, r := range rules {
if r.Kind() == "go_proto_library" {
genGoProtoRules = append(genGoProtoRules, r.Name())
}
}

// Generate Go rules.
if protoName == "" {
// Empty proto rules for deletion.
_, rs := g.generateProto(pcMode, []protoTarget{pkg.proto}, pkg.importPath)
rules = append(rules, rs...)

// Do not generate empty rule if already created above
found := false
for _, r := range rs {
if r.Kind() == "go_proto_library" {
for _, pr := range genGoProtoRules {
if pr == r.Name() {
found = true
break
}
}
if found {
break
}
}
}
if !found {
rules = append(rules, rs...)
}
}
lib := g.generateLib(pkg, protoEmbeds)
var libName string
Expand Down
9 changes: 9 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ gazelle_binary(
visibility = ["//visibility:private"],
)

gazelle_binary(
name = "gazelle_with_proto_and_go_languages",
languages = [
"//language/proto:go_default_library",
"//language/go:go_default_library",
],
visibility = ["//visibility:private"],
)

[gazelle_generation_test(
# Name the test the path to the directory containing the WORKSPACE file.
name = file[0:-len("/WORKSPACE")],
Expand Down
1 change: 1 addition & 0 deletions tests/fix_package_proto_name_match/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:proto file
1 change: 1 addition & 0 deletions tests/fix_package_proto_name_match/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:proto file
3 changes: 3 additions & 0 deletions tests/fix_package_proto_name_match/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Fix go_proto_library dependencies

Fix go_proto_library dependencies when package and proto name match
Empty file.
Empty file.
32 changes: 32 additions & 0 deletions tests/fix_package_proto_name_match/example/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

proto_library(
name = "example_proto",
srcs = ["example.proto"],
visibility = ["//visibility:public"],
deps = ["//google/type:type_proto"],
)

proto_library(
name = "other_proto",
srcs = ["other.proto"],
visibility = ["//visibility:public"],
deps = ["//google/type:type_proto"],
)

go_proto_library(
name = "example_go_proto",
importpath = "github.com/bazelbuild/bazel-gazelle/fix_package_proto_name_match/example/example",
proto = ":example_proto",
visibility = ["//visibility:public"],
deps = ["//google/type:date_proto"],
)

go_proto_library(
name = "other_go_proto",
importpath = "github.com/bazelbuild/bazel-gazelle/fix_package_proto_name_match/example/other",
proto = ":other_proto",
visibility = ["//visibility:public"],
deps = ["//google/type:date_proto"],
)
12 changes: 12 additions & 0 deletions tests/fix_package_proto_name_match/example/example.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

syntax = "proto3";

package proto.example.example;

option go_package = "github.com/bazelbuild/bazel-gazelle/fix_package_proto_name_match/example/example";

import "google/type/date.proto";

message Example {
google.type.Date date = 1;
}
11 changes: 11 additions & 0 deletions tests/fix_package_proto_name_match/example/other.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package proto.example.other;

option go_package = "github.com/bazelbuild/bazel-gazelle/fix_package_proto_name_match/example/other";

import "google/type/date.proto";

message Other {
google.type.Date date = 1;
}
Empty file.
3 changes: 3 additions & 0 deletions tests/tools.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ def get_binary(target):
if "loads_from_flag" in target:
return ":gazelle_with_language_loads_from_flag"

if "fix_package_proto_name_match" in target:
return ":gazelle_with_proto_and_go_languages"

return ":gazelle"

0 comments on commit 27bbbc6

Please sign in to comment.