Skip to content

Commit dea4e53

Browse files
authored
chore: add rules_buf (#997)
it comes from aspect-forks until bufbuild/rules_buf#42 is merged, hopefully for the next release
1 parent cea9064 commit dea4e53

5 files changed

Lines changed: 219 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"Bazel module definition for bzlmod"
2+
module(
3+
name = "rules_buf",
4+
version = "0.1.1", # Replaced when publishing
5+
compatibility_level = 1,
6+
)
7+
8+
bazel_dep(name = "platforms", version = "0.0.4")
9+
# Only needed because rules_proto doesn't provide the protoc toolchain yet.
10+
# TODO(alex/sahin): remove in the future
11+
bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf")
12+
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
13+
14+
ext = use_extension("//buf:extensions.bzl", "ext")
15+
use_repo(ext, "rules_buf_toolchains")
16+
register_toolchains("@rules_buf_toolchains//:all")
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
commit 99af4b12716531021b9160fa2c622df4c5e8a0de
2+
Author: Alex Eagle <alex@aspect.dev>
3+
Date: Wed Oct 11 15:04:35 2023 -0700
4+
5+
feat: support bzlmod
6+
7+
Bazel modules are moving away from setup in WORKSPACE files, towards a 'package manager' called bzlmod.
8+
Such modules are published on https://registry.bazel.build and are easier for developers to consume in their Bazel projects.
9+
10+
diff --git a/MODULE.bazel b/MODULE.bazel
11+
new file mode 100644
12+
index 0000000..973e2ba
13+
--- /dev/null
14+
+++ b/MODULE.bazel
15+
@@ -0,0 +1,16 @@
16+
+"Bazel module definition for bzlmod"
17+
+module(
18+
+ name = "rules_buf",
19+
+ version = "0.1.1", # Replaced when publishing
20+
+ compatibility_level = 1,
21+
+)
22+
+
23+
+bazel_dep(name = "platforms", version = "0.0.4")
24+
+# Only needed because rules_proto doesn't provide the protoc toolchain yet.
25+
+# TODO(alex/sahin): remove in the future
26+
+bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf")
27+
+bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
28+
+
29+
+ext = use_extension("//buf:extensions.bzl", "ext")
30+
+use_repo(ext, "rules_buf_toolchains")
31+
+register_toolchains("@rules_buf_toolchains//:all")
32+
diff --git a/buf/extensions.bzl b/buf/extensions.bzl
33+
new file mode 100644
34+
index 0000000..472dfad
35+
--- /dev/null
36+
+++ b/buf/extensions.bzl
37+
@@ -0,0 +1,16 @@
38+
+"""Define module extensions for using rules_buf with bzlmod.
39+
+See https://bazel.build/docs/bzlmod#extension-definition
40+
+"""
41+
+
42+
+load("//buf/internal:toolchain.bzl", "buf_download_releases")
43+
+
44+
+def _extension_impl(module_ctx):
45+
+ buf_download_releases(
46+
+ name = "rules_buf_toolchains",
47+
+ # TODO: get desired version from the attr
48+
+ version = "v1.27.0",
49+
+ )
50+
+
51+
+ext = module_extension(
52+
+ implementation = _extension_impl,
53+
+)
54+
diff --git a/buf/internal/toolchain.bzl b/buf/internal/toolchain.bzl
55+
index 33d3564..6fda129 100644
56+
--- a/buf/internal/toolchain.bzl
57+
+++ b/buf/internal/toolchain.bzl
58+
@@ -63,7 +63,7 @@ def declare_buf_toolchains(os, cpu, rules_buf_repo_name):
59+
native.toolchain(
60+
name = cmd + "_toolchain",
61+
toolchain = ":" + toolchain_impl,
62+
- toolchain_type = "@{}//tools/{}:toolchain_type".format(rules_buf_repo_name, cmd),
63+
+ toolchain_type = "@@{}//tools/{}:toolchain_type".format(rules_buf_repo_name, cmd),
64+
exec_compatible_with = [
65+
"@platforms//os:" + os,
66+
"@platforms//cpu:" + cpu,
67+
@@ -199,7 +199,7 @@ def _buf_download_releases_impl(ctx):
68+
)
69+
return update_attrs(ctx.attr, ["version"], {"version": version})
70+
71+
-_buf_download_releases = repository_rule(
72+
+buf_download_releases = repository_rule(
73+
implementation = _buf_download_releases_impl,
74+
attrs = {
75+
"version": attr.string(
76+
@@ -217,7 +217,7 @@ def rules_buf_toolchains(name = _TOOLCHAINS_REPO, version = None):
77+
version: Release version, eg: `v.1.0.0-rc12`. If `None` defaults to latest
78+
"""
79+
80+
- _buf_download_releases(name = name, version = version)
81+
+ buf_download_releases(name = name, version = version)
82+
83+
_register_toolchains(name, "buf")
84+
_register_toolchains(name, "protoc-gen-buf-breaking")
85+
diff --git a/examples/bzlmod/.bazelrc b/examples/bzlmod/.bazelrc
86+
new file mode 100644
87+
index 0000000..3ce91d2
88+
--- /dev/null
89+
+++ b/examples/bzlmod/.bazelrc
90+
@@ -0,0 +1 @@
91+
+common --enable_bzlmod
92+
diff --git a/examples/bzlmod/BUILD.bazel b/examples/bzlmod/BUILD.bazel
93+
new file mode 100644
94+
index 0000000..762ad1e
95+
--- /dev/null
96+
+++ b/examples/bzlmod/BUILD.bazel
97+
@@ -0,0 +1,20 @@
98+
+load("@rules_buf//buf:defs.bzl", "buf_lint_test")
99+
+
100+
+exports_files(["buf.yaml"], visibility = ["//visibility:public"])
101+
+
102+
+proto_library(
103+
+ name = "unused",
104+
+ srcs = ["unused.proto"],
105+
+)
106+
+
107+
+proto_library(
108+
+ name = "foo_proto",
109+
+ srcs = ["file.proto"],
110+
+ deps = [":unused"],
111+
+)
112+
+
113+
+buf_lint_test(
114+
+ name = "foo_proto_lint",
115+
+ targets = [":foo_proto"],
116+
+ config = "buf.yaml",
117+
+)
118+
diff --git a/examples/bzlmod/MODULE.bazel b/examples/bzlmod/MODULE.bazel
119+
new file mode 100644
120+
index 0000000..74cdc32
121+
--- /dev/null
122+
+++ b/examples/bzlmod/MODULE.bazel
123+
@@ -0,0 +1,10 @@
124+
+"Bazel dependencies"
125+
+bazel_dep(name = "rules_buf", dev_dependency = True, version = "0.0.0")
126+
+
127+
+local_path_override(
128+
+ module_name = "rules_buf",
129+
+ path = "../..",
130+
+)
131+
+# Only needed because rules_proto doesn't provide the protoc toolchain yet.
132+
+# TODO(alex/sahin): remove in the future
133+
+bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf")
134+
diff --git a/examples/bzlmod/WORKSPACE b/examples/bzlmod/WORKSPACE
135+
new file mode 100644
136+
index 0000000..9f0c4f0
137+
--- /dev/null
138+
+++ b/examples/bzlmod/WORKSPACE
139+
@@ -0,0 +1 @@
140+
+# Marker that this folder is the root of a Bazel workspace
141+
diff --git a/examples/bzlmod/buf.yaml b/examples/bzlmod/buf.yaml
142+
new file mode 100644
143+
index 0000000..29fd418
144+
--- /dev/null
145+
+++ b/examples/bzlmod/buf.yaml
146+
@@ -0,0 +1,4 @@
147+
+version: v1
148+
+lint:
149+
+ use:
150+
+ - IMPORT_USED
151+
diff --git a/examples/bzlmod/file.proto b/examples/bzlmod/file.proto
152+
new file mode 100644
153+
index 0000000..eef262a
154+
--- /dev/null
155+
+++ b/examples/bzlmod/file.proto
156+
@@ -0,0 +1,3 @@
157+
+syntax = "proto3";
158+
+
159+
+import "unused.proto";
160+
diff --git a/examples/bzlmod/unused.proto b/examples/bzlmod/unused.proto
161+
new file mode 100644
162+
index 0000000..d95660e
163+
--- /dev/null
164+
+++ b/examples/bzlmod/unused.proto
165+
@@ -0,0 +1 @@
166+
+syntax = "proto3";
167+
\ No newline at end of file
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
bcr_test_module:
2+
module_path: 'examples/bzlmod'
3+
matrix:
4+
platform: ['ubuntu2004']
5+
tasks:
6+
run_tests:
7+
name: 'Run test module'
8+
platform: ${{ platform }}
9+
build_targets:
10+
- '//...'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"integrity": "sha256-/cvkfvtor1wk3+dSdJqwsqDBrAIV49KM9unuKODCrvs=",
3+
"strip_prefix": "rules_buf-abbbfce7c3fccf1d4b87afa28140d9ce53f80057",
4+
"patch_strip": 1,
5+
"patches": {
6+
"bzlmod.patch": "sha256-D5PYozjx10XcIqo0mMyoGIqO3PJHJtxLQ1vo3zesDS0="
7+
},
8+
"url": "https://github.com/bufbuild/rules_buf/archive/abbbfce7c3fccf1d4b87afa28140d9ce53f80057.zip"
9+
}

modules/rules_buf/metadata.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"homepage": "https://github.com/bufbuild/rules_buf",
3+
"maintainers": [
4+
{
5+
"email": "alex@aspect.dev",
6+
"github": "alexeagle",
7+
"name": "Alex Eagle"
8+
}
9+
],
10+
"repository": [
11+
"github:bufbuild/rules_buf"
12+
],
13+
"versions": [
14+
"0.1.1"
15+
],
16+
"yanked_versions": {}
17+
}

0 commit comments

Comments
 (0)