Skip to content

Commit d7eceaf

Browse files
committed
Prefer go_grpc_library
This allows for easier compiler upgrades via rules_go upgrades
1 parent e7e69c4 commit d7eceaf

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

language/go/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ const (
147147
)
148148

149149
var (
150-
defaultGoProtoCompilers = []string{"@io_bazel_rules_go//proto:go_proto"}
151-
defaultGoGrpcCompilers = []string{"@io_bazel_rules_go//proto:go_grpc"}
150+
defaultGoProtoCompilers = []string{}
151+
defaultGoGrpcCompilers = []string{}
152152
)
153153

154154
func (m testMode) String() string {

language/go/constants.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ const (
3131
// mode for libraries that contained .pb.go files and .proto files.
3232
legacyProtoFilegroupName = "go_default_library_protos"
3333

34-
// grpcCompilerLabel is the label for the gRPC compiler plugin, used in the
35-
// "compilers" attribute of go_proto_library rules.
36-
grpcCompilerLabel = "@io_bazel_rules_go//proto:go_grpc"
34+
// oldGrpcCompilerLabel is the label for the old gRPC compiler plugin, used
35+
// in the "compilers" attribute of go_proto_library rules.
36+
oldGrpcCompilerLabel = "@io_bazel_rules_go//proto:go_grpc"
3737

3838
// goProtoSuffix is the suffix applied to the labels of all generated
3939
// go_proto_library targets.

language/go/fix.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,15 @@ func migrateLibraryEmbed(c *config.Config, f *rule.File) {
171171
}
172172
}
173173

174-
// migrateGrpcCompilers converts "go_grpc_library" rules into "go_proto_library"
175-
// rules with a "compilers" attribute.
174+
// migrateGrpcCompilers converts "go_proto_library" rules with a "compilers"
175+
// attribute containing exactly oldGrpcCompilerLabel into "go_grpc_library" rules.
176176
func migrateGrpcCompilers(c *config.Config, f *rule.File) {
177177
for _, r := range f.Rules {
178-
if r.Kind() != "go_grpc_library" || r.ShouldKeep() || r.Attr("compilers") != nil {
178+
if r.Kind() != "go_proto_library" || r.ShouldKeep() || r.Attr("compilers") == nil || len(r.AttrStrings("compilers")) != 1 || strListAttrContains(r, "compilers", oldGrpcCompilerLabel) {
179179
continue
180180
}
181-
r.SetKind("go_proto_library")
182-
r.SetAttr("compilers", []string{grpcCompilerLabel})
181+
r.SetKind("go_grpc_library")
182+
r.SetAttr("compilers", []string{})
183183
}
184184
}
185185

language/go/generate.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,19 @@ func (g *generator) generateProto(mode proto.Mode, target protoTarget, importPat
454454
}
455455
}
456456

457-
goProtoLibrary := rule.NewRule("go_proto_library", goProtoName)
457+
var goProtoLibrary *rule.Rule
458+
if target.hasServices && !gc.goGrpcCompilersSet {
459+
goProtoLibrary = rule.NewRule("go_grpc_library", goProtoName)
460+
} else {
461+
goProtoLibrary = rule.NewRule("go_proto_library", goProtoName)
462+
if gc.goProtoCompilersSet {
463+
goProtoLibrary.SetAttr("compilers", gc.goProtoCompilers)
464+
} else if gc.goGrpcCompilersSet {
465+
goProtoLibrary.SetAttr("compilers", gc.goGrpcCompilers)
466+
}
467+
}
458468
goProtoLibrary.SetAttr("proto", ":"+protoName)
459469
g.setImportAttrs(goProtoLibrary, importPath)
460-
if target.hasServices {
461-
goProtoLibrary.SetAttr("compilers", gc.goGrpcCompilers)
462-
} else if gc.goProtoCompilersSet {
463-
goProtoLibrary.SetAttr("compilers", gc.goProtoCompilers)
464-
}
465470
if g.shouldSetVisibility {
466471
goProtoLibrary.SetAttr("visibility", visibility)
467472
}

0 commit comments

Comments
 (0)