Skip to content

Commit 3d7eec0

Browse files
authored
Fix proto_repository for @googleapis (stackb#324)
* Include vendor tree in proto_repository_tools_srcs * Rename -override_go_googleapis to -reresolve_known_proto_imports * Add googleapis to CI build * Fix TestOverrideRule
1 parent edb1a99 commit 3d7eec0

File tree

17 files changed

+572
-87
lines changed

17 files changed

+572
-87
lines changed

.bazelci/presubmit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ tasks:
88
platform: ubuntu1804
99
build_targets:
1010
- "@protoapis//..."
11+
- "@googleapis//..."
1112
test_targets:
1213
- //example/...
1314
- //language/...
@@ -23,6 +24,7 @@ tasks:
2324
platform: macos
2425
build_targets:
2526
- "@protoapis//..."
27+
- "@googleapis//..."
2628
test_targets:
2729
- //example/...
2830
- //language/...

README.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,31 @@ Bazel starlark rules for building protocol buffers +/- gRPC :sparkles:.
3030

3131
# Table of Contents
3232

33+
- [`rules_proto (v2)`](#rules_proto-v2)
34+
- [Table of Contents](#table-of-contents)
3335
- [Getting Started](#getting-started)
34-
- [Workspace Boilerplate](#workspace-boilerplate)
36+
- [`WORKSPACE` Boilerplate](#workspace-boilerplate)
3537
- [Gazelle Setup](#gazelle-setup)
3638
- [Gazelle Configuration](#gazelle-configuration)
3739
- [Build Directives](#build-directives)
38-
- [YAML Config File](#yaml-configuration)
40+
- [YAML Configuration](#yaml-configuration)
3941
- [Running Gazelle](#running-gazelle)
40-
- [Build Rules](#build-rules)
41-
- [proto_compile](#proto_compile)
42-
- [proto_plugin](#proto_plugin)
43-
- [proto_compiled_sources](#proto_compiled_sources)
44-
- [Deep dive on the mappings attribute](#the-output_mappings-attribute)
45-
- [Repository Rules](#repository-rules)
46-
- [proto_repository](#proto_repository)
47-
- [proto_gazelle](#proto_gazelle)
48-
- [Plugin Implementations](#plugin-implementations)
49-
- [Rule Implementations](#rule-implementations)
50-
- [Writing Custom Plugins & Rules](#writing-custom-plugins-and-rules)
51-
- [History of this repository](#history)
42+
- [Build Rules](#build-rules)
43+
- [proto\_compile](#proto_compile)
44+
- [proto\_plugin](#proto_plugin)
45+
- [proto\_compiled\_sources](#proto_compiled_sources)
46+
- [proto\_compile\_assets](#proto_compile_assets)
47+
- [The `output_mappings` attribute](#the-output_mappings-attribute)
48+
- [Repository Rules](#repository-rules)
49+
- [proto\_repository](#proto_repository)
50+
- [proto\_gazelle](#proto_gazelle)
51+
- [golden\_filegroup](#golden_filegroup)
52+
- [Plugin Implementations](#plugin-implementations)
53+
- [Rule Implementations](#rule-implementations)
54+
- [Writing Custom Plugins and Rules](#writing-custom-plugins-and-rules)
55+
- [+/- of golang implementations](#--of-golang-implementations)
56+
- [+/- of starlark implementations](#--of-starlark-implementations)
57+
- [History](#history)
5258

5359
# Getting Started
5460

@@ -390,7 +396,7 @@ potential conflicts with other possible gazelle extensions, using the name
390396
The core of `stackb/rules_proto` contains two build rules:
391397

392398
| Rule | Description |
393-
|-----------------|---------------------------------------------------------|
399+
| --------------- | ------------------------------------------------------- |
394400
| `proto_compile` | Executes the `protoc` tool. |
395401
| `proto_plugin` | Provides static `protoc` plugin-specific configuration. |
396402

@@ -590,7 +596,7 @@ proto_repository(
590596
],
591597
build_file_generation = "clean",
592598
build_file_proto_mode = "file",
593-
override_go_googleapis = True,
599+
reresolve_known_proto_imports = True,
594600
proto_language_config_file = "//example:config.yaml",
595601
strip_prefix = "googleapis-02710fa0ea5312d79d7fb986c9c9823fb41049a9",
596602
type = "zip",
@@ -615,7 +621,7 @@ Takeaways:
615621
external workspace.
616622
- `proto_language_config_file` is an optional label pointing to a valid
617623
`config.yaml` file to configure this extension.
618-
- `override_go_googleapis` is a boolean attribute that has special meaning for
624+
- `reresolve_known_proto_imports` is a boolean attribute that has special meaning for
619625
the googleapis repository. Due to the fact that the builtin gazelle "proto"
620626
extension has
621627
[hardcoded logic](https://github.com/bazelbuild/bazel-gazelle/blob/master/language/proto/known_proto_imports.go)
@@ -762,7 +768,7 @@ The plugin name is an opaque string, but by convention they are maven-esqe
762768
artifact identifiers that follow a GitHub org/repo/plugin_name convention.
763769

764770
| Plugin |
765-
|------------------------------------------------------------------------------------------------------------------------|
771+
| ---------------------------------------------------------------------------------------------------------------------- |
766772
| [builtin:cpp](pkg/plugin/builtin/cpp_plugin.go) |
767773
| [builtin:csharp](pkg/plugin/builtin/csharp_plugin.go) |
768774
| [builtin:java](pkg/plugin/builtin/java_plugin.go) |
@@ -798,7 +804,7 @@ The rule name is an opaque string, but by convention they are maven-esqe
798804
artifact identifiers that follow a GitHub org/repo/rule_name convention.
799805

800806
| Plugin |
801-
|---------------------------------------------------------------------------------------------------|
807+
| ------------------------------------------------------------------------------------------------- |
802808
| [stackb:rules_proto:grpc_cc_library](pkg/rule/rules_cc/grpc_cc_library.go) |
803809
| [stackb:rules_proto:grpc_closure_js_library](pkg/rule/rules_closure/grpc_closure_js_library.go) |
804810
| [stackb:rules_proto:grpc_java_library](pkg/rule/rules_java/grpc_java_library.go) |

WORKSPACE

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
workspace(name = "build_stack_rules_proto")
22

3-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4-
53
# gazelle:repo bazel_gazelle
64

75
# ----------------------------------------------------
@@ -99,18 +97,6 @@ go_deps()
9997
# Core gRPC
10098
# ----------------------------------------------------
10199

102-
http_archive(
103-
name = "com_google_absl",
104-
generator_function = "grpc_deps",
105-
generator_name = "com_google_absl",
106-
sha256 = "9a2b5752d7bfade0bdeee2701de17c9480620f8b237e1964c1b9967c75374906",
107-
strip_prefix = "abseil-cpp-20230125.2",
108-
urls = [
109-
"https://storage.googleapis.com/grpc-bazel-mirror/github.com/abseil/abseil-cpp/archive/20230125.2.tar.gz",
110-
"https://github.com/abseil/abseil-cpp/archive/20230125.2.tar.gz",
111-
],
112-
)
113-
114100
load(
115101
"@com_github_grpc_grpc//bazel:grpc_deps.bzl",
116102
"grpc_deps",

docs/proto_repository.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ proto_repository(
8989
build_file_generation = "on",
9090
build_file_proto_mode = "file",
9191
cfgs = ["//:config.yaml"],
92-
override_go_googleapis = True,
92+
reresolve_known_proto_imports = True,
9393
sha256 = "b9dbc65ebc738a486265ef7b708e9449bf361541890091983e946557ee0a4bfc",
9494
strip_prefix = "googleapis-66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe",
9595
type = "zip",

example/golden/testdata/proto_repository/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ A few notes about the test:
3232
to load newer versions of @org_golang_google_grpc before rules_go or gazelle
3333
try and do that.
3434
- proto file mode is used.
35-
- override_go_googleapis = True is needed to rewrite labels that would normally
36-
be in the "go_googleapis" external workspace.
35+
- reresolve_known_proto_imports = True is needed to rewrite labels that would normally
36+
be in the "go_googleapis" and "com_google_protobuf" external workspace.

example/golden/testdata/proto_repository/WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ proto_repository(
1818
build_file_generation = "on",
1919
build_file_proto_mode = "file",
2020
cfgs = ["//:config.yaml"],
21-
override_go_googleapis = True,
21+
reresolve_known_proto_imports = True,
2222
sha256 = "b9dbc65ebc738a486265ef7b708e9449bf361541890091983e946557ee0a4bfc",
2323
strip_prefix = "googleapis-66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe",
2424
type = "zip",

pkg/language/protobuf/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ func (pl *protobufLang) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Co
2929
fs.StringVar(&pl.repoName,
3030
"proto_repo_name", "",
3131
"external name of this repository")
32-
fs.BoolVar(&pl.overrideGoGooleapis,
33-
"override_go_googleapis", false,
34-
"if true, remove hardcoded proto_library deps on go_googleapis")
32+
fs.BoolVar(&pl.reresolveKnownProtoImports,
33+
"reresolve_known_proto_imports", false,
34+
"if true, re-resolve hardcoded proto_library deps on go_googleapis and com_google_protobuf from language/proto from the index")
3535
fs.Var(&pl.starlarkRules,
3636
"proto_rule",
3737
"register custom starlark rule of the form `<file_name>%<rule_name>`")

pkg/language/protobuf/generate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ func (pl *protobufLang) GenerateRules(args language.GenerateArgs) language.Gener
9191

9292
rules := pkg.Rules()
9393

94-
// special case if we want to override go_googleapis deps.
95-
if pl.overrideGoGooleapis && len(protoLibraries) > 0 {
94+
// special case if we want to override com_google_protobuf or go_googleapis
95+
// deps.
96+
if pl.reresolveKnownProtoImports && len(protoLibraries) > 0 {
9697
rules = append(rules, makeProtoOverrideRule(protoLibraries))
9798
}
9899

pkg/language/protobuf/lang.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ type protobufLang struct {
3232
// importsInFiles is a comma-separated list of files that contains proto
3333
// index csv content.
3434
importsInFiles string
35-
// overrideGoGooleapis performs special processing for go_googleapis deps
36-
overrideGoGooleapis bool
35+
// reresolveKnownProtoImports performs an additional resolve step for go_googleapis deps
36+
reresolveKnownProtoImports bool
3737
// the resolver instance used for cross-resolution
3838
resolver protoc.ImportResolver
3939
// starlarkRules stores custom starlark proto rule names in the form filename%rulename

pkg/language/protobuf/override.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ func makeProtoOverrideRule(libs []protoc.ProtoLibrary) *rule.Rule {
3333
return overrideRule
3434
}
3535

36-
func resolveOverrideRule(rel string, overrideRule *rule.Rule, resolver protoc.ImportResolver) {
36+
func resolveOverrideRule(c *config.Config, rel string, overrideRule *rule.Rule, resolver protoc.ImportResolver) {
37+
3738
libs := overrideRule.PrivateAttr(protoLibrariesRuleKey).([]protoc.ProtoLibrary)
3839
if len(libs) == 0 {
3940
return
@@ -42,31 +43,18 @@ func resolveOverrideRule(rel string, overrideRule *rule.Rule, resolver protoc.Im
4243
for _, lib := range libs {
4344
r := lib.Rule()
4445

45-
// filter out go_googleapis dependencies and re-resolve them anew.
46-
keep := make([]label.Label, 0)
47-
48-
for _, dep := range r.AttrStrings("deps") {
49-
lbl, _ := label.Parse(dep)
50-
// log.Printf("override resolve //%s:%s dep %v", rel, r.Name(), lbl)
51-
if lbl.Repo == "go_googleapis" {
52-
continue
53-
}
54-
if lbl.Relative {
55-
// relative labels will be repopulated via resolution (below)
56-
continue
57-
}
58-
keep = append(keep, lbl)
59-
}
46+
// re-resolve dependencies.
47+
deps := make([]label.Label, 0)
6048

6149
imports := r.PrivateAttr(config.GazelleImportsKey)
6250
if imps, ok := imports.([]string); ok {
6351
for _, imp := range imps {
6452
result := resolver.Resolve("proto", "proto", imp)
6553
if len(result) > 0 {
6654
first := result[0]
67-
keep = append(keep, first.Label)
55+
deps = append(deps, first.Label)
6856
if debugOverrides {
69-
log.Println("go_googleapis resolve imports HIT", first.Label)
57+
log.Println("go_googleapis resolve imports HIT", imp, first.Label)
7058
}
7159
} else {
7260
if debugOverrides {
@@ -76,9 +64,9 @@ func resolveOverrideRule(rel string, overrideRule *rule.Rule, resolver protoc.Im
7664
}
7765
}
7866

79-
if len(keep) > 0 {
80-
ss := make([]string, len(keep))
81-
for i, lbl := range keep {
67+
if len(deps) > 0 {
68+
ss := make([]string, len(deps))
69+
for i, lbl := range deps {
8270
ss[i] = lbl.Rel("", rel).String()
8371
}
8472
r.SetAttr("deps", protoc.DeduplicateAndSort(ss))

0 commit comments

Comments
 (0)