Skip to content

Commit

Permalink
Remove special resolution of go_proto imports (#1807)
Browse files Browse the repository at this point in the history
  • Loading branch information
linzhp authored May 20, 2024
1 parent 47e9c2a commit 61b277b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 104 deletions.
40 changes: 8 additions & 32 deletions language/go/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,9 @@ func (g *generator) maybePublishToolLib(lib *rule.Rule, pkg *goPackage) {
}

// maybeGenerateExtraLib generates extra equivalent library targets for
// certain protobuf libraries. These "_gen" targets depend on Well Known Types
// certain protobuf libraries. Historically, these "_gen" targets depend on Well Known Types
// built with go_proto_library and are used together with go_proto_library.
// The original targets are used when proto rule generation is disabled.
// However, these are no longer needed and are kept as aliases to be backward-compatible
func (g *generator) maybeGenerateExtraLib(lib *rule.Rule, pkg *goPackage) *rule.Rule {
gc := getGoConfig(g.c)
if gc.prefix != "github.com/golang/protobuf" || gc.prefixRel != "" {
Expand All @@ -585,48 +585,24 @@ func (g *generator) maybeGenerateExtraLib(lib *rule.Rule, pkg *goPackage) *rule.
var r *rule.Rule
switch pkg.importPath {
case "github.com/golang/protobuf/descriptor":
r = rule.NewRule("go_library", "go_default_library_gen")
r.SetAttr("srcs", pkg.library.sources.buildFlat())
r.SetAttr("importpath", pkg.importPath)
r = rule.NewRule("alias", "go_default_library_gen")
r.SetAttr("actual", ":go_default_library")
r.SetAttr("visibility", []string{"//visibility:public"})
r.SetAttr("deps", []string{
"//proto:go_default_library",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
"@org_golang_google_protobuf//reflect/protodesc:go_default_library",
"@org_golang_google_protobuf//reflect/protoreflect:go_default_library",
"@org_golang_google_protobuf//runtime/protoimpl:go_default_library",
})

case "github.com/golang/protobuf/jsonpb":
r = rule.NewRule("alias", "go_default_library_gen")
r.SetAttr("actual", ":go_default_library")
r.SetAttr("visibility", []string{"//visibility:public"})

case "github.com/golang/protobuf/protoc-gen-go/generator":
r = rule.NewRule("go_library", "go_default_library_gen")
r.SetAttr("srcs", pkg.library.sources.buildFlat())
r.SetAttr("importpath", pkg.importPath)
r = rule.NewRule("alias", "go_default_library_gen")
r.SetAttr("actual", ":go_default_library")
r.SetAttr("visibility", []string{"//visibility:public"})
r.SetAttr("deps", []string{
"//proto:go_default_library",
"//protoc-gen-go/generator/internal/remap:go_default_library",
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
})

case "github.com/golang/protobuf/ptypes":
r = rule.NewRule("go_library", "go_default_library_gen")
r.SetAttr("srcs", pkg.library.sources.buildFlat())
r.SetAttr("importpath", pkg.importPath)
r = rule.NewRule("alias", "go_default_library_gen")
r.SetAttr("actual", ":go_default_library")
r.SetAttr("visibility", []string{"//visibility:public"})
r.SetAttr("deps", []string{
"//proto:go_default_library",
"@io_bazel_rules_go//proto/wkt:any_go_proto",
"@io_bazel_rules_go//proto/wkt:duration_go_proto",
"@io_bazel_rules_go//proto/wkt:timestamp_go_proto",
"@org_golang_google_protobuf//reflect/protoreflect:go_default_library",
"@org_golang_google_protobuf//reflect/protoregistry:go_default_library",
})
}

return r
Expand Down
52 changes: 12 additions & 40 deletions language/go/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,46 +830,18 @@ go_proto_library(name = "wkts_go_proto")
go_library(
name = "wkts_go_lib",
deps = [
"@com_github_golang_protobuf//protoc-gen-go/descriptor",
"@com_github_golang_protobuf//protoc-gen-go/plugin",
"@com_github_golang_protobuf//ptypes/any",
"@com_github_golang_protobuf//ptypes/duration",
"@com_github_golang_protobuf//ptypes/empty",
"@com_github_golang_protobuf//ptypes/struct",
"@com_github_golang_protobuf//ptypes/timestamp",
"@com_github_golang_protobuf//ptypes/wrappers",
"@org_golang_google_genproto//protobuf/api",
"@org_golang_google_genproto//protobuf/field_mask",
"@org_golang_google_genproto//protobuf/ptype",
"@org_golang_google_genproto//protobuf/source_context",
],
)
`,
}, {
desc: "proto_special_cross_resolve",
old: buildFile{content: `
go_library(
name = "go_default_library",
_imports = [
"github.com/golang/protobuf/proto",
"github.com/golang/protobuf/jsonpb",
"github.com/golang/protobuf/descriptor",
"github.com/golang/protobuf/protoc-gen-go/generator",
"github.com/golang/protobuf/ptypes",
"google.golang.org/grpc",
],
)
`},
want: `
go_library(
name = "go_default_library",
deps = [
"@com_github_golang_protobuf//descriptor:go_default_library_gen",
"@com_github_golang_protobuf//jsonpb:go_default_library_gen",
"@com_github_golang_protobuf//proto:go_default_library",
"@com_github_golang_protobuf//protoc-gen-go/generator:go_default_library_gen",
"@com_github_golang_protobuf//ptypes:go_default_library_gen",
"@org_golang_google_grpc//:go_default_library",
"//vendor/github.com/golang/protobuf/protoc-gen-go/descriptor",
"//vendor/github.com/golang/protobuf/protoc-gen-go/plugin",
"//vendor/github.com/golang/protobuf/ptypes/any",
"//vendor/github.com/golang/protobuf/ptypes/duration",
"//vendor/github.com/golang/protobuf/ptypes/empty",
"//vendor/github.com/golang/protobuf/ptypes/struct",
"//vendor/github.com/golang/protobuf/ptypes/timestamp",
"//vendor/github.com/golang/protobuf/ptypes/wrappers",
"//vendor/google.golang.org/genproto/protobuf/api",
"//vendor/google.golang.org/genproto/protobuf/field_mask",
"//vendor/google.golang.org/genproto/protobuf/ptype",
"//vendor/google.golang.org/genproto/protobuf/source_context",
],
)
`,
Expand Down
25 changes: 0 additions & 25 deletions language/proto/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,30 +176,5 @@ func (*protoLang) CrossResolve(c *config.Config, ix *resolve.RuleIndex, imp reso
return []resolve.FindResult{{Label: l}}
}
}
if imp.Lang == "go" && pc.Mode.ShouldUseKnownImports() {
// These are commonly used libraries that depend on Well Known Types.
// They depend on the generated versions of these protos to avoid conflicts.
// However, since protoc-gen-go depends on these libraries, we generate
// its rules in disable_global mode (to avoid cyclic dependency), so the
// "go_default_library" versions of these libraries depend on the
// pre-generated versions of the proto libraries.
switch imp.Imp {
case "github.com/golang/protobuf/proto":
return []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "proto", "go_default_library")}}
case "github.com/golang/protobuf/jsonpb":
return []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "jsonpb", "go_default_library_gen")}}
case "github.com/golang/protobuf/descriptor":
return []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "descriptor", "go_default_library_gen")}}
case "github.com/golang/protobuf/ptypes":
return []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "ptypes", "go_default_library_gen")}}
case "github.com/golang/protobuf/protoc-gen-go/generator":
return []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "protoc-gen-go/generator", "go_default_library_gen")}}
case "google.golang.org/grpc":
return []resolve.FindResult{{Label: label.New("org_golang_google_grpc", "", "go_default_library")}}
}
if l, ok := knownGoProtoImports[imp.Imp]; ok {
return []resolve.FindResult{{Label: l}}
}
}
return nil
}
7 changes: 0 additions & 7 deletions language/proto/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,6 @@ func TestCrossResolve(t *testing.T) {
lang: "go",
want: nil,
},
{
desc: "go known import",
protoMode: DefaultMode,
imp: resolve.ImportSpec{Lang: "go", Imp: "github.com/golang/protobuf/proto"},
lang: "go",
want: []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "proto", "go_default_library")}},
},
{
desc: "go unknown import",
protoMode: DefaultMode,
Expand Down

0 comments on commit 61b277b

Please sign in to comment.