Skip to content

Commit a78c49d

Browse files
authored
proto_repository: add attributes to write a proto_repository_info provider (#328)
* Use non-zip archive for @protoapis dep * Add proto_repository_info provider, rule, and github_proto_repository macro * Upgrade to rules_go 0.39.1 * Bump minimum bazel version to 5.4.0 * Add test for ProtoRepositoryInfo provider * Bump bazelci version
1 parent 361cff2 commit a78c49d

17 files changed

+197
-71
lines changed

.bazelci/presubmit.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
buildifier:
33
version: 5.1.0
44
warnings: "-function-docstring,-function-docstring-header,-module-docstring,-function-docstring-args,-list-append"
5-
bazel: 5.1.1
5+
bazel: 5.4.0
66
tasks:
77
default_workspace_ubuntu1804:
88
platform: ubuntu1804

.bazelrc

-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ build:bazelci --deleted_packages=docs
1313

1414
# workaround for scala
1515
build --incompatible_java_common_parameters=false
16-

.bazelversion

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.1.1
1+
5.4.0

deps/BUILD.bazel

+8-8
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ depsgen(
9797
deps = ["//deps:io_grpc_grpc_java"],
9898
)
9999

100-
# Release: v0.35.0
101-
# TargetCommitish: release-0.35
102-
# Date: 2022-09-11 15:59:49 +0000 UTC
103-
# URL: https://github.com/bazelbuild/rules_go/releases/tag/v0.35.0
104-
# Size: 931734 (932 kB)
100+
# Release: v0.39.1
101+
# TargetCommitish: release-0.39
102+
# Date: 2023-04-20 04:35:08 +0000 UTC
103+
# URL: https://github.com/bazelbuild/rules_go/releases/tag/v0.39.1
104+
# Size: 1759832 (1.8 MB)
105105
proto_dependency(
106106
name = "io_bazel_rules_go",
107107
repository_rule = "http_archive",
108-
sha256 = "cc027f11f98aef8bc52c472ced0714994507a16ccd3a0820b2df2d6db695facd",
109-
strip_prefix = "rules_go-0.35.0",
110-
urls = ["https://github.com/bazelbuild/rules_go/archive/v0.35.0.tar.gz"],
108+
sha256 = "473a064d502e89d11c497a59f9717d1846e01515a3210bd169f22323161c076e",
109+
strip_prefix = "rules_go-0.39.1",
110+
urls = ["https://github.com/bazelbuild/rules_go/archive/v0.39.1.tar.gz"],
111111
visibility = ["//visibility:public"],
112112
)
113113

deps/core_deps.bzl

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ def io_bazel_rules_go():
1919
_maybe(
2020
http_archive,
2121
name = "io_bazel_rules_go",
22-
sha256 = "cc027f11f98aef8bc52c472ced0714994507a16ccd3a0820b2df2d6db695facd",
23-
strip_prefix = "rules_go-0.35.0",
22+
sha256 = "473a064d502e89d11c497a59f9717d1846e01515a3210bd169f22323161c076e",
23+
strip_prefix = "rules_go-0.39.1",
2424
urls = [
25-
"https://github.com/bazelbuild/rules_go/archive/v0.35.0.tar.gz",
25+
"https://github.com/bazelbuild/rules_go/archive/v0.39.1.tar.gz",
2626
],
2727
)
2828

example/golden/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ func TestRunGazelle(t *testing.T) {
109109
if err := bazel_testing.RunBazel("build", "..."); err != nil {
110110
t.Fatal(err)
111111
}
112+
if err := bazel_testing.RunBazel("test", "..."); err != nil {
113+
t.Fatal(err)
114+
}
112115
}
113116
""",
114117
strip_prefix = "example/golden/testdata/proto_repository",

example/golden/testdata/proto_repository/BUILD.in

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")
33
load("@build_stack_rules_proto//rules:proto_gazelle.bzl", "DEFAULT_LANGUAGES", "proto_gazelle")
4+
load("//:proto_repository_info_test.bzl", "proto_repository_info_test")
45

56
# gazelle:prefix github.com/corp/repo
67
# gazelle:go_generate_proto false
@@ -19,4 +20,14 @@ proto_gazelle(
1920
imports = ["@googleapis//:imports.csv"],
2021
)
2122

23+
proto_repository_info_test(
24+
name = "proto_repository_info_test",
25+
info = "@googleapis//:proto_repository",
26+
want_source_host = "github.com",
27+
want_source_owner = "googleapis",
28+
want_source_repo = "googleapis",
29+
want_source_commit = "66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe",
30+
want_source_prefix = "",
31+
)
32+
2233
exports_files(["config.yaml"])

example/golden/testdata/proto_repository/BUILD.out

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
load("@bazel_gazelle//:def.bzl", "gazelle_binary")
33
load("@build_stack_rules_proto//rules:proto_gazelle.bzl", "DEFAULT_LANGUAGES", "proto_gazelle")
4+
load("//:proto_repository_info_test.bzl", "proto_repository_info_test")
45

56
# gazelle:prefix github.com/corp/repo
67
# gazelle:go_generate_proto false
@@ -19,4 +20,14 @@ proto_gazelle(
1920
imports = ["@googleapis//:imports.csv"],
2021
)
2122

23+
proto_repository_info_test(
24+
name = "proto_repository_info_test",
25+
info = "@googleapis//:proto_repository",
26+
want_source_commit = "66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe",
27+
want_source_host = "github.com",
28+
want_source_owner = "googleapis",
29+
want_source_prefix = "",
30+
want_source_repo = "googleapis",
31+
)
32+
2233
exports_files(["config.yaml"])

example/golden/testdata/proto_repository/WORKSPACE

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ proto_repository(
2020
cfgs = ["//:config.yaml"],
2121
reresolve_known_proto_imports = True,
2222
sha256 = "b9dbc65ebc738a486265ef7b708e9449bf361541890091983e946557ee0a4bfc",
23+
source_commit = "66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe",
24+
source_owner = "googleapis",
25+
source_repo = "googleapis",
2326
strip_prefix = "googleapis-66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe",
2427
type = "zip",
2528
urls = ["https://codeload.github.com/googleapis/googleapis/zip/66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe"],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
load("@build_stack_rules_proto//rules:providers.bzl", "ProtoRepositoryInfo")
2+
3+
def _proto_repository_info_test_impl(ctx):
4+
info = ctx.attr.info[ProtoRepositoryInfo]
5+
6+
must_attr(info, ctx.attr, "source_host")
7+
must_attr(info, ctx.attr, "source_owner")
8+
must_attr(info, ctx.attr, "source_repo")
9+
must_attr(info, ctx.attr, "source_commit")
10+
must_attr(info, ctx.attr, "source_prefix")
11+
12+
ctx.actions.write(ctx.outputs.json, info.to_json())
13+
14+
# we're checking attr values in the provider, so the script really does not
15+
# need to do anything
16+
ctx.actions.write(ctx.outputs.executable, "echo PASS")
17+
18+
return [DefaultInfo(
19+
files = depset([ctx.outputs.json, ctx.outputs.executable]),
20+
)]
21+
22+
proto_repository_info_test = rule(
23+
implementation = _proto_repository_info_test_impl,
24+
attrs = {
25+
"info": attr.label(
26+
providers = [ProtoRepositoryInfo],
27+
mandatory = True,
28+
),
29+
"want_source_host": attr.string(),
30+
"want_source_owner": attr.string(),
31+
"want_source_repo": attr.string(),
32+
"want_source_commit": attr.string(),
33+
"want_source_prefix": attr.string(),
34+
},
35+
outputs = {
36+
"json": "%{name}.json",
37+
},
38+
test = True,
39+
)
40+
41+
def must_attr(info, attr, name):
42+
got = getattr(info, name)
43+
want = getattr(attr, "want_" + name)
44+
if got != want:
45+
fail(".%s: want %s, got %s" % (name, want, got))

pkg/protoc/starlark_rule.go

-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ func (s *starlarkRuleProvider) Rule(othergen ...*rule.Rule) *rule.Rule {
295295

296296
// Resolve implements part of the RuleProvider interface.
297297
func (s *starlarkRuleProvider) Resolve(c *config.Config, ix *resolve.RuleIndex, r *rule.Rule, imports []string, from label.Label) {
298-
299298
}
300299

301300
// Imports implements part of the RuleProvider interface.

proto_repositories.bzl

+2-3
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ def proto_repositories():
5454
"google/protobuf/compiler/cpp/test_bad_identifiers.proto",
5555
],
5656
reresolve_known_proto_imports = True,
57+
sha256 = "087c2ec84a07308318d35e0e39717e2037e05d14e628244602a2c78fbe203fa5",
5758
strip_prefix = "protobuf-a74f54b724bdc2fe0bfc271f4dc0ceb159805625/src",
58-
type = "zip",
59-
# https://github.com/protocolbuffers/protobuf/releases/tag/v23.2
60-
urls = ["https://codeload.github.com/protocolbuffers/protobuf/zip/a74f54b724bdc2fe0bfc271f4dc0ceb159805625"],
59+
urls = ["https://github.com/protocolbuffers/protobuf/archive/a74f54b724bdc2fe0bfc271f4dc0ceb159805625.tar.gz"],
6160
)
6261

6362
proto_repository(

rules/BUILD.bazel

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
filegroup(
22
name = "all_files",
33
testonly = True,
4-
srcs = [
4+
srcs = glob(["*.bzl"]) + [
55
"BUILD.bazel",
6-
"depsgen.bzl",
7-
"example.bzl",
8-
"proto_compile.bzl",
9-
"proto_compile_gencopy.bzl",
10-
"proto_compiled_source_update.bzl",
11-
"proto_compiled_sources.bzl",
12-
"proto_dependency.bzl",
13-
"proto_descriptor_set.bzl",
14-
"proto_gazelle.bzl",
15-
"proto_plugin.bzl",
16-
"protogenrule.bzl",
17-
"providers.bzl",
186
"//rules/cc:all_files",
197
"//rules/closure:all_files",
208
"//rules/go:all_files",

rules/private/proto_repository_tools_srcs.bzl

-39
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
""" Code generated by list_repository_tools_srcs.go; DO NOT EDIT."""
22
PROTO_REPOSITORY_TOOLS_SRCS = [
33
"@build_stack_rules_proto//:BUILD.bazel",
4-
"@build_stack_rules_proto//cmd/depsgen:BUILD.bazel",
5-
"@build_stack_rules_proto//cmd/depsgen:config.go",
6-
"@build_stack_rules_proto//cmd/depsgen:depsgen.go",
7-
"@build_stack_rules_proto//cmd/depsgen:generator.go",
8-
"@build_stack_rules_proto//cmd/depsgen:template.go",
9-
"@build_stack_rules_proto//cmd/examplegen:BUILD.bazel",
10-
"@build_stack_rules_proto//cmd/examplegen:config.go",
11-
"@build_stack_rules_proto//cmd/examplegen:examplegen.go",
12-
"@build_stack_rules_proto//cmd/examplegen:generator.go",
13-
"@build_stack_rules_proto//cmd/examplegen:linewriter.go",
14-
"@build_stack_rules_proto//cmd/examplegen:template.go",
154
"@build_stack_rules_proto//cmd/gazelle:BUILD.bazel",
165
"@build_stack_rules_proto//cmd/gazelle:diff.go",
176
"@build_stack_rules_proto//cmd/gazelle:fix-update.go",
@@ -26,34 +15,9 @@ PROTO_REPOSITORY_TOOLS_SRCS = [
2615
"@build_stack_rules_proto//cmd/gazelle:print.go",
2716
"@build_stack_rules_proto//cmd/gazelle:update-repos.go",
2817
"@build_stack_rules_proto//cmd/gazelle:wspace.go",
29-
"@build_stack_rules_proto//cmd/gencopy:BUILD.bazel",
30-
"@build_stack_rules_proto//cmd/gencopy:gencopy.go",
3118
"@build_stack_rules_proto//deps:BUILD.bazel",
32-
"@build_stack_rules_proto//docs:BUILD.bazel",
33-
"@build_stack_rules_proto//example:BUILD.bazel",
34-
"@build_stack_rules_proto//example/golden:BUILD.bazel",
35-
"@build_stack_rules_proto//example/person:BUILD.bazel",
36-
"@build_stack_rules_proto//example/place:BUILD.bazel",
37-
"@build_stack_rules_proto//example/routeguide:BUILD.bazel",
38-
"@build_stack_rules_proto//example/routeguide/cc:BUILD.bazel",
39-
"@build_stack_rules_proto//example/routeguide/closure:BUILD.bazel",
40-
"@build_stack_rules_proto//example/routeguide/java:BUILD.bazel",
41-
"@build_stack_rules_proto//example/routeguide/nodejs:BUILD.bazel",
42-
"@build_stack_rules_proto//example/routeguide/scala:BUILD.bazel",
43-
"@build_stack_rules_proto//example/thing:BUILD.bazel",
44-
"@build_stack_rules_proto//example/toolchain/prebuilt:BUILD.bazel",
45-
"@build_stack_rules_proto//example/toolchain/standard:BUILD.bazel",
46-
"@build_stack_rules_proto//language/example:BUILD.bazel",
47-
"@build_stack_rules_proto//language/example:example.go",
4819
"@build_stack_rules_proto//language/protobuf:BUILD.bazel",
49-
"@build_stack_rules_proto//language/protobuf/oldtestdata/gogo:BUILD.bazel",
50-
"@build_stack_rules_proto//language/protobuf/oldtestdata/java:BUILD.bazel",
5120
"@build_stack_rules_proto//language/protobuf:protobuf.go",
52-
"@build_stack_rules_proto//pkg:BUILD.bazel",
53-
"@build_stack_rules_proto//pkg/goldentest:BUILD.bazel",
54-
"@build_stack_rules_proto//pkg/goldentest:cases.go",
55-
"@build_stack_rules_proto//pkg/language/noop:BUILD.bazel",
56-
"@build_stack_rules_proto//pkg/language/noop:noop.go",
5721
"@build_stack_rules_proto//pkg/language/protobuf:BUILD.bazel",
5822
"@build_stack_rules_proto//pkg/language/protobuf:config.go",
5923
"@build_stack_rules_proto//pkg/language/protobuf:fix.go",
@@ -171,8 +135,6 @@ PROTO_REPOSITORY_TOOLS_SRCS = [
171135
"@build_stack_rules_proto//plugin/grpc/grpc-go:BUILD.bazel",
172136
"@build_stack_rules_proto//plugin/grpc/grpc-java:BUILD.bazel",
173137
"@build_stack_rules_proto//plugin/grpc/grpc-node:BUILD.bazel",
174-
"@build_stack_rules_proto//plugin/grpc/grpc-web:BUILD.bazel",
175-
"@build_stack_rules_proto//plugin/grpc-ecosystem/grpc-gateway:BUILD.bazel",
176138
"@build_stack_rules_proto//plugin/scalapb/scalapb:BUILD.bazel",
177139
"@build_stack_rules_proto//plugin/stackb/grpc_js:BUILD.bazel",
178140
"@build_stack_rules_proto//plugin/stephenh/ts-proto:BUILD.bazel",
@@ -187,7 +149,6 @@ PROTO_REPOSITORY_TOOLS_SRCS = [
187149
"@build_stack_rules_proto//rules/proto:BUILD.bazel",
188150
"@build_stack_rules_proto//rules/py:BUILD.bazel",
189151
"@build_stack_rules_proto//rules/scala:BUILD.bazel",
190-
"@build_stack_rules_proto//rules/ts:BUILD.bazel",
191152
"@build_stack_rules_proto//toolchain:BUILD.bazel",
192153
"@build_stack_rules_proto//vendor/github.com/bazelbuild/bazel-gazelle/config:config.go",
193154
"@build_stack_rules_proto//vendor/github.com/bazelbuild/bazel-gazelle/config:constants.go",

rules/proto/proto_repository.bzl

+56-1
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,23 @@ def _proto_repository_impl(ctx):
190190

191191
# Populate Gazelle directive at root build file and
192192
lines = ["# " + d for d in ctx.attr.build_directives] + [
193-
"",
193+
'load("@build_stack_rules_proto//rules:proto_repository_info.bzl", "proto_repository_info")',
194194
'exports_files(["%s"])' % ctx.attr.imports_out,
195+
"proto_repository_info(",
196+
" name = 'proto_repository',",
197+
" commit = '%s'," % ctx.attr.commit,
198+
" tag = '%s'," % ctx.attr.tag,
199+
" vcs = '%s'," % ctx.attr.vcs,
200+
" urls = %s," % ctx.attr.urls,
201+
" sha256 = '%s'," % ctx.attr.sha256,
202+
" strip_prefix = '%s'," % ctx.attr.strip_prefix,
203+
" source_host = '%s'," % ctx.attr.source_host,
204+
" source_owner = '%s'," % ctx.attr.source_owner,
205+
" source_repo = '%s'," % ctx.attr.source_repo,
206+
" source_prefix = '%s'," % ctx.attr.source_prefix,
207+
" source_commit = '%s'," % ctx.attr.source_commit,
208+
" visibility = ['//visibility:public'],",
209+
")",
195210
]
196211
ctx.file(
197212
build_file_name,
@@ -262,6 +277,7 @@ def _proto_repository_impl(ctx):
262277
go_repository = repository_rule(
263278
implementation = _proto_repository_impl,
264279
attrs = {
280+
265281
# Fundamental attributes of a go repository
266282
"importpath": attr.string(mandatory = False), # True in go_repository
267283

@@ -286,6 +302,13 @@ go_repository = repository_rule(
286302
"type": attr.string(),
287303
"sha256": attr.string(),
288304

305+
# Attributes for a repository that is publically hosted by github
306+
"source_host": attr.string(default = "github.com"),
307+
"source_owner": attr.string(),
308+
"source_repo": attr.string(),
309+
"source_prefix": attr.string(),
310+
"source_commit": attr.string(),
311+
289312
# Attributes for a module that should be downloaded with the Go toolchain.
290313
"version": attr.string(),
291314
"sum": attr.string(),
@@ -383,3 +406,35 @@ def patch(ctx):
383406
if st.return_code:
384407
fail("Error applying patch command %s:\n%s%s" %
385408
(cmd, st.stdout, st.stderr))
409+
410+
def github_proto_repository(name, owner, repo, commit, prefix = "", build_file_expunge = True, build_file_proto_mode = "file", **kwargs):
411+
"""github_proto_repository is a macro for a proto_repository hosted at github.com
412+
413+
Args:
414+
name: the name of the rule
415+
owner: the github owner (e.g. 'protocolbuffers')
416+
repo: the github repo name (e.g. 'protobuf')
417+
prefix: the strip_prefix value for the repo (e.g. 'src')
418+
commit: the git commit (required for this macro)
419+
build_file_expunge: defaults to true for this macro.
420+
build_file_proto_mode: defaults to 'file' for this macro.
421+
**kwargs: the kwargs accumulator
422+
423+
"""
424+
strip_prefix = "%s-%s" % (repo, commit)
425+
if prefix:
426+
strip_prefix += "/" + prefix
427+
428+
proto_repository(
429+
name = name,
430+
source_host = "github.com",
431+
source_owner = owner,
432+
source_repo = repo,
433+
source_commit = commit,
434+
source_prefix = prefix,
435+
strip_prefix = strip_prefix,
436+
build_file_expunge = build_file_expunge,
437+
build_file_proto_mode = build_file_proto_mode,
438+
urls = ["https://%s/%s/archive/%s.tar.gz" % (owner, repo, commit)],
439+
**kwargs
440+
)

0 commit comments

Comments
 (0)