Skip to content

Commit

Permalink
Prefer go_grpc_library
Browse files Browse the repository at this point in the history
This allows for easier compiler upgrades via rules_go upgrades
  • Loading branch information
mering committed Jan 4, 2024
1 parent e7e69c4 commit d7eceaf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions language/go/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions language/go/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions language/go/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 containing exactly 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.AttrStrings("compilers")) != 1 || strListAttrContains(r, "compilers", oldGrpcCompilerLabel) {
continue
}
r.SetKind("go_proto_library")
r.SetAttr("compilers", []string{grpcCompilerLabel})
r.SetKind("go_grpc_library")
r.SetAttr("compilers", []string{})
}
}

Expand Down
17 changes: 11 additions & 6 deletions language/go/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit d7eceaf

Please sign in to comment.