From 549842deccc58476c2f697be57eca3ecbcb93d60 Mon Sep 17 00:00:00 2001 From: maleo Date: Thu, 4 Jan 2024 14:49:35 +0000 Subject: [PATCH] Prefer go_grpc_library This allows for easier compiler upgrades via rules_go upgrades --- language/go/config.go | 4 ++-- language/go/constants.go | 6 +++--- language/go/fix.go | 10 +++++----- language/go/generate.go | 17 +++++++++++------ 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/language/go/config.go b/language/go/config.go index e23ed5437..b11e06d72 100644 --- a/language/go/config.go +++ b/language/go/config.go @@ -147,8 +147,8 @@ const ( ) var ( - defaultGoProtoCompilers = []string{"@io_bazel_rules_go//proto:go_proto"} - defaultGoGrpcCompilers = []string{"@io_bazel_rules_go//proto:go_grpc"} + defaultGoProtoCompilers = []string{} + defaultGoGrpcCompilers = []string{} ) func (m testMode) String() string { diff --git a/language/go/constants.go b/language/go/constants.go index 0affc48a9..5bb33e68d 100644 --- a/language/go/constants.go +++ b/language/go/constants.go @@ -31,9 +31,9 @@ const ( // mode for libraries that contained .pb.go files and .proto files. legacyProtoFilegroupName = "go_default_library_protos" - // grpcCompilerLabel is the label for the gRPC compiler plugin, used in the - // "compilers" attribute of go_proto_library rules. - grpcCompilerLabel = "@io_bazel_rules_go//proto:go_grpc" + // oldGrpcCompilerLabel is the label for the old gRPC compiler plugin, used + // in the "compilers" attribute of go_proto_library rules. + oldGrpcCompilerLabel = "@io_bazel_rules_go//proto:go_grpc" // goProtoSuffix is the suffix applied to the labels of all generated // go_proto_library targets. diff --git a/language/go/fix.go b/language/go/fix.go index 6d31add74..c3f0fa4e0 100644 --- a/language/go/fix.go +++ b/language/go/fix.go @@ -171,15 +171,15 @@ func migrateLibraryEmbed(c *config.Config, f *rule.File) { } } -// migrateGrpcCompilers converts "go_grpc_library" rules into "go_proto_library" -// rules with a "compilers" attribute. +// migrateGrpcCompilers converts "go_proto_library" rules with a "compilers" +// attribute equal to oldGrpcCompilerLabel into "go_grpc_library" rules. func migrateGrpcCompilers(c *config.Config, f *rule.File) { for _, r := range f.Rules { - if r.Kind() != "go_grpc_library" || r.ShouldKeep() || r.Attr("compilers") != nil { + if r.Kind() != "go_proto_library" || r.ShouldKeep() || r.Attr("compilers") == nil || len(r.Attr("compilers")) != 1 || r.Attr("compilers")[0] != oldGrpcCompilerLabel { continue } - r.SetKind("go_proto_library") - r.SetAttr("compilers", []string{grpcCompilerLabel}) + r.SetKind("go_grpc_library") + r.SetAttr("compilers", []string{}) } } diff --git a/language/go/generate.go b/language/go/generate.go index 53e397a12..a0f480f7e 100644 --- a/language/go/generate.go +++ b/language/go/generate.go @@ -454,14 +454,19 @@ func (g *generator) generateProto(mode proto.Mode, target protoTarget, importPat } } - goProtoLibrary := rule.NewRule("go_proto_library", goProtoName) + var goProtoLibrary *rule.Rule + if target.hasServices && !gc.goGrpcCompilersSet { + goProtoLibrary := rule.NewRule("go_grpc_library", goProtoName) + } else { + goProtoLibrary := rule.NewRule("go_proto_library", goProtoName) + if gc.goProtoCompilersSet { + goProtoLibrary.SetAttr("compilers", gc.goProtoCompilers) + } else if gc.goGrpcCompilersSet { + goProtoLibrary.SetAttr("compilers", gc.goGrpcCompilers) + } + } goProtoLibrary.SetAttr("proto", ":"+protoName) g.setImportAttrs(goProtoLibrary, importPath) - if target.hasServices { - goProtoLibrary.SetAttr("compilers", gc.goGrpcCompilers) - } else if gc.goProtoCompilersSet { - goProtoLibrary.SetAttr("compilers", gc.goProtoCompilers) - } if g.shouldSetVisibility { goProtoLibrary.SetAttr("visibility", visibility) }