Skip to content

Commit

Permalink
Migrate go_grpc_library to go_proto_library with compilers (bazel-con…
Browse files Browse the repository at this point in the history
…trib#48)

* If a .proto file has a service definition, we will now generate a
  go_proto_library rule with a compilers dependency on
  "@io_bazel_rules_go//proto:go_grpc" instead of a go_grpc_library.
* Existing go_grpc_library rules will be fixed. This is a minor fix,
  since it doesn't rename, move, or delete rules, so it will run in
  update mode.

Fixes bazel-contrib#8
  • Loading branch information
jayconrod authored Dec 22, 2017
1 parent 18645a3 commit 334c311
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 40 deletions.
15 changes: 8 additions & 7 deletions cmd/gazelle/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,25 +788,26 @@ service {}
path: config.DefaultValidBuildFileNames[0],
content: `
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
proto_library(
name = "repo_proto",
srcs = ["foo.proto"],
visibility = ["//visibility:public"],
)
go_library(
name = "go_default_library",
embed = [":repo_go_proto"],
go_proto_library(
name = "repo_go_proto",
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
importpath = "example.com/repo",
proto = ":repo_proto",
visibility = ["//visibility:public"],
)
go_grpc_library(
name = "repo_go_proto",
go_library(
name = "go_default_library",
embed = [":repo_go_proto"],
importpath = "example.com/repo",
proto = ":repo_proto",
visibility = ["//visibility:public"],
)
`,
Expand Down
4 changes: 4 additions & 0 deletions config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const (
// DefaultCgoLibName is the name of the default cgo_library rule in a Go package directory.
DefaultCgoLibName = "cgo_default_library"

// 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"

// WellKnownTypesProtoRepo is the repository containing proto_library rules
// for the Well Known Types.
WellKnownTypesProtoRepo = "com_google_protobuf"
Expand Down
35 changes: 34 additions & 1 deletion merger/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ func removeLegacyProto(c *config.Config, oldFile *bf.File) *bf.File {
// FixLoads should be called after this, since it will fix load
// statements that may be broken by transformations applied by this function.
func FixFileMinor(c *config.Config, oldFile *bf.File) *bf.File {
return migrateLibraryEmbed(c, oldFile)
fixedFile := migrateLibraryEmbed(c, oldFile)
return migrateGrpcCompilers(c, fixedFile)
}

// migrateLibraryEmbed converts "library" attributes to "embed" attributes,
Expand Down Expand Up @@ -361,6 +362,38 @@ func migrateLibraryEmbed(c *config.Config, oldFile *bf.File) *bf.File {
return &fixedFile
}

// migrateGrpcCompilers converts "go_grpc_library" rules into "go_proto_library"
// rules with a "compilers" attribute.
func migrateGrpcCompilers(c *config.Config, oldFile *bf.File) *bf.File {
fixed := false
fixedFile := *oldFile
for i, stmt := range fixedFile.Stmt {
call, ok := stmt.(*bf.CallExpr)
if !ok {
continue
}
rule := bf.Rule{Call: call}
if rule.Kind() != "go_grpc_library" || shouldKeep(stmt) || rule.Attr("compilers") != nil {
continue
}

fixedCall := *call
fixedCall.List = make([]bf.Expr, len(call.List))
copy(fixedCall.List, call.List)
rule.Call = &fixedCall
rule.SetKind("go_proto_library")
rule.SetAttr("compilers", &bf.ListExpr{
List: []bf.Expr{&bf.StringExpr{Value: config.GrpcCompilerLabel}},
})
fixedFile.Stmt[i] = &fixedCall
fixed = true
}
if !fixed {
return oldFile
}
return &fixedFile
}

// FixLoads removes loads of unused go rules and adds loads of newly used rules.
// This should be called after FixFile and MergeWithExisting, since symbols
// may be introduced that aren't loaded.
Expand Down
37 changes: 36 additions & 1 deletion merger/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package merger
import (
"testing"

bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/bazel-gazelle/config"
bf "github.com/bazelbuild/buildtools/build"
)

type fixTestCase struct {
Expand Down Expand Up @@ -247,6 +247,41 @@ go_test(
srcs = ["foo_test.go"],
embed = [":go_default_library"],
)
`,
},
// migrateGrpcCompilers tests
{
desc: "go_grpc_library migrated to compilers",
old: `load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library")
proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
visibility = ["//visibility:public"],
)
go_grpc_library(
name = "foo_go_proto",
importpath = "example.com/repo",
proto = ":foo_proto",
visibility = ["//visibility:public"],
)
`,
want: `load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library")
proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
visibility = ["//visibility:public"],
)
go_proto_library(
name = "foo_go_proto",
importpath = "example.com/repo",
proto = ":foo_proto",
visibility = ["//visibility:public"],
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
)
`,
},
} {
Expand Down
18 changes: 7 additions & 11 deletions merger/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,8 @@ func init() {
"go_binary",
"go_test",
"go_proto_library",
"go_grpc_library",
}
goProtoKinds := []string{
"go_proto_library",
"go_grpc_library",
}
allKinds := append(append(goKinds, goProtoKinds...), "proto_library")
allKinds := append(goKinds, "proto_library")

preResolveCommonAttrs := []string{"srcs"}
preResolveGoAttrs := []string{
Expand All @@ -71,7 +66,10 @@ func init() {
"embed",
"importpath",
}
preResolveGoProtoAttrs := []string{"proto"}
preResolveGoProtoAttrs := []string{
"compilers",
"proto",
}

PreResolveAttrs = make(MergeableAttrs)
for _, kind := range allKinds {
Expand All @@ -85,10 +83,8 @@ func init() {
PreResolveAttrs[kind][attr] = true
}
}
for _, kind := range goProtoKinds {
for _, attr := range preResolveGoProtoAttrs {
PreResolveAttrs[kind][attr] = true
}
for _, attr := range preResolveGoProtoAttrs {
PreResolveAttrs["go_proto_library"][attr] = true
}

postResolveCommonAttrs := []string{
Expand Down
19 changes: 4 additions & 15 deletions rules/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func (g *Generator) generateProto(pkg *packages.Package) (string, []bf.Expr) {
EmptyRule("filegroup", filegroupName),
EmptyRule("proto_library", protoName),
EmptyRule("go_proto_library", goProtoName),
EmptyRule("go_grpc_library", goProtoName),
}
}

Expand All @@ -126,26 +125,16 @@ func (g *Generator) generateProto(pkg *packages.Package) (string, []bf.Expr) {
{"proto", ":" + protoName},
{"importpath", pkg.ImportPath(g.c)},
}
if pkg.Proto.HasServices {
goProtoAttrs = append(goProtoAttrs, KeyValue{"compilers", []string{"@io_bazel_rules_go//proto:go_grpc"}})
}
if g.shouldSetVisibility {
goProtoAttrs = append(goProtoAttrs, KeyValue{"visibility", visibility})
}
if !imports.IsEmpty() {
goProtoAttrs = append(goProtoAttrs, KeyValue{config.GazelleImportsKey, imports})
}

// If a developer adds or removes services from existing protos, this
// will create a new rule and delete the old one, along with any custom
// attributes (assuming no keep comments). We can't currently merge
// rules unless both kind and name match.
if pkg.Proto.HasServices {
rules = append(rules,
NewRule("go_grpc_library", goProtoAttrs),
EmptyRule("go_proto_library", goProtoName))
} else {
rules = append(rules,
NewRule("go_proto_library", goProtoAttrs),
EmptyRule("go_grpc_library", goProtoName))
}
rules = append(rules, NewRule("go_proto_library", goProtoAttrs))

return goProtoName, rules
}
Expand Down
4 changes: 1 addition & 3 deletions rules/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"path/filepath"
"testing"

bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/merger"
"github.com/bazelbuild/bazel-gazelle/packages"
"github.com/bazelbuild/bazel-gazelle/resolve"
"github.com/bazelbuild/bazel-gazelle/rules"
bf "github.com/bazelbuild/buildtools/build"
)

func testConfig(repoRoot, goPrefix string) *config.Config {
Expand Down Expand Up @@ -116,8 +116,6 @@ proto_library(name = "foo_proto")
go_proto_library(name = "foo_go_proto")
go_grpc_library(name = "foo_go_proto")
go_library(name = "go_default_library")
go_binary(name = "repo")
Expand Down
5 changes: 3 additions & 2 deletions testdata/repo/service/BUILD.want
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

proto_library(
name = "service_proto",
Expand All @@ -11,12 +11,13 @@ proto_library(
visibility = ["//visibility:public"],
)

go_grpc_library(
go_proto_library(
name = "service_go_proto",
_gazelle_imports = [
"google/protobuf/any.proto",
"service/sub/sub.proto",
],
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
importpath = "example.com/repo/service",
proto = ":service_proto",
visibility = ["//visibility:public"],
Expand Down

0 comments on commit 334c311

Please sign in to comment.