Skip to content

Commit 7d7cc26

Browse files
authored
feat: default to latest protobuf rather than pinning (#12)
* feat: use protobuf v27 * chore: update docs * fix: check in requirements.in * chore: more docs * Update release_prep.sh
1 parent 4455860 commit 7d7cc26

File tree

9 files changed

+62
-62
lines changed

9 files changed

+62
-62
lines changed

.github/workflows/release_prep.sh

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ cat << EOF
2323
2424
\`\`\`starlark
2525
bazel_dep(name = "toolchains_protoc", version = "${TAG:1}")
26+
27+
# Optional: choose a version of protoc rather than the latest.
28+
protoc = use_extension("@toolchains_protoc//protoc:extensions.bzl", "protoc")
29+
protoc.toolchain(
30+
# Creates a repository to satisfy well-known-types dependencies such as
31+
# deps=["@com_google_protobuf//:any_proto"]
32+
google_protobuf = "com_google_protobuf",
33+
# Pin to any version of protoc
34+
version = "v26.0",
35+
)
2636
\`\`\`
2737
2838
## Using WORKSPACE

MODULE.bazel

+7-15
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ bazel_dep(name = "platforms", version = "0.0.8")
1313
protoc = use_extension("//protoc:extensions.bzl", "protoc")
1414
protoc.toolchain(
1515
google_protobuf = "com_google_protobuf",
16-
version = "v25.3",
16+
version = "LATEST",
1717
)
1818
use_repo(protoc, "com_google_protobuf", "toolchains_protoc_hub")
1919

@@ -23,32 +23,24 @@ bazel_dep(name = "aspect_bazel_lib", version = "1.32.1", dev_dependency = True)
2323
bazel_dep(name = "buildifier_prebuilt", version = "6.1.2", dev_dependency = True)
2424
bazel_dep(name = "aspect_rules_py", version = "0.7.1", dev_dependency = True)
2525
bazel_dep(name = "rules_java", version = "7.4.0", dev_dependency = True)
26-
bazel_dep(name = "rules_python", version = "0.31.0", dev_dependency = True)
27-
28-
# # Update to include
29-
# https://github.com/bazelbuild/rules_python/pull/1577
30-
git_override(
31-
module_name = "rules_python",
32-
commit = "3edcae33dcc43051ca6f200c2d0cb3692df79d03",
33-
remote = "https://github.com/bazelbuild/rules_python.git",
34-
)
26+
bazel_dep(name = "rules_python", version = "0.32.2", dev_dependency = True)
27+
bazel_dep(name = "rules_go", version = "0.48.0", dev_dependency = True)
28+
bazel_dep(name = "rules_uv", version = "0.10.0", dev_dependency = True)
3529

3630
register_toolchains(
3731
"//examples/lang_toolchains:all",
3832
dev_dependency = True,
3933
)
4034

4135
# Shows how a typical Python user fetches all the dependencies of their app, including the protobuf runtime
42-
pip = use_extension(
36+
dev_pip = use_extension(
4337
"@rules_python//python/extensions:pip.bzl",
4438
"pip",
4539
dev_dependency = True,
4640
)
47-
pip.parse(
41+
dev_pip.parse(
4842
hub_name = "pypi",
4943
python_version = "3.11",
5044
requirements_lock = "//examples/lang_toolchains:requirements.txt",
5145
)
52-
use_repo(pip, "pypi")
53-
54-
bazel_dep(name = "rules_go", version = "0.48.0")
46+
use_repo(dev_pip, "pypi")

README.md

+14-24
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ Using Protocol Buffers with Bazel has always been difficult.
1818
## Support matrix
1919

2020
Minimum versions:
21+
2122
- Bazel: 7.0.0
2223
- rules_proto: 6.0.0
2324

24-
| Language | Support | Example or Issue |
25-
|----------|---------|------------------|
26-
| Java | Yes | [example](./examples/java) |
25+
| Language | Support | Example or Issue |
26+
| -------- | ------- | ---------------------------- |
27+
| Java | Yes | [example](./examples/java) |
2728
| Python | Yes | [example](./examples/python) |
28-
| Rust | | https://github.com/bazelbuild/rules_rust/issues/2627 |
29-
| Go | | https://github.com/bazelbuild/rules_go/issues/3895 |
29+
| Go | Yes | [example](./examples/go) |
3030

31-
Your language missing? Please file the upstream issue and send a PR to update this table!
31+
For all other languages, see https://github.com/bazelbuild/rules_proto/discussions/213
3232

3333
## Installation
3434

@@ -56,43 +56,33 @@ using whatever Bazel rule you chose for interacting with package managers
5656
- JavaScript: https://www.npmjs.com/package/protobufjs
5757
- Go: https://pkg.go.dev/google.golang.org/protobuf/runtime
5858

59-
Second, declare the toolchain in a `BUILD` file. It looks like the following
60-
(where `LANG`, `--flag_to_protoc`, and `runtime` are replaced
59+
For rulesets that need a "lang toolchain", declare one in a `BUILD` file.
60+
It looks like the following (where `LANG`, `--flag_to_protoc`, and `runtime` are replaced
6161
with appropriate values for the language and the label of the runtime you installed).
6262

63-
You can choose a Bazel package where this goes; we recommend `/tools/protoc/BUILD.bazel`.
63+
You can choose a Bazel package where this goes; we recommend `/tools/toolchains/BUILD.bazel`.
6464

6565
```starlark
6666
load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain")
67+
6768
proto_lang_toolchain(
6869
name = "protoc_LANG_toolchain",
6970
command_line = "--flag_to_protoc=%s",
7071
progress_message = "Generating LANG proto_library %{label}",
7172
runtime = "@some-external//lib",
72-
)
73-
```
74-
75-
Finally, in the same `BUILD` file, declare the registration target...
76-
77-
```starlark
78-
toolchain(
79-
name = "protoc_LANG_toolchain.registration",
80-
toolchain = ":protoc_LANG_toolchain",
81-
# This type should be declared by the language rules:
73+
# This target should be declared by the language rules:
8274
toolchain_type = "@rules_LANG//path/to/proto:toolchain_type",
8375
)
8476
```
8577

86-
...and then register it, either in `MODULE.bazel` or `WORKSPACE`:
78+
Then register the toolchains, either in `MODULE.bazel` or `WORKSPACE`:
8779

8880
```starlark
89-
register_toolchains("//tools/protoc:all")
81+
register_toolchains("//tools/toolchains:all")
9082
```
9183

9284
See `examples` for several language rules like `py_proto_library` and `java_proto_library`.
9385

94-
Note that there is NO dependency on `@com_google_protobuf` anywhere.
95-
9686
### Troubleshooting
9787

9888
What if you still see that protoc is compiling? This means that there is still a transitive dependency on the
@@ -110,7 +100,7 @@ What if you still see that protoc is compiling? This means that there is still a
110100
1. That distribution includes the "well known types" such as `timestamp.proto`
111101
1. The protobuf runtimes for each language are distributed to the appropriate package manager such as npm or PyPI.
112102
1. Bazel 7 introduced `--incompatible_enable_proto_toolchain_resolution` to allow us fetch `protoc` rather than re-build it!
113-
That flag ALSO decouples how each built-in language rule (Java, Python, C++, etc.) locates the runtime.
103+
That flag ALSO decouples how each built-in language rule (Java, Python, C++, etc.) locates the runtime.
114104

115105
Thanks to that flag, this repo simply contains a toolchain that resolves those pre-built binaries.
116106
In the user's repository, there's a small BUILD file where the toolchain is configured.

WORKSPACE.bazel

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")
55
# Note: this is simpler than using rules_jvm_external with a maven installation,
66
# but users would probably get this JAR file that way.
77
http_jar(
8-
name = "protobuf-java_3_25_3",
9-
sha256 = "e90d8ddb963b20a972a6a59b5093ade2b07cbe546cab3279aaf4383260385f58",
10-
urls = ["https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.25.3/protobuf-java-3.25.3.jar"],
8+
name = "protobuf-java",
9+
sha256 = "0532ad1024d62361561acaedb974d7d16889e7670b36e23e9321dd6b9d334ef9",
10+
urls = ["https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/4.27.0-RC3/protobuf-java-4.27.0-RC3.jar"],
1111
)

examples/java/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ java_binary(
44
main_class = "Main",
55
deps = [
66
"//examples:foo_java_proto",
7-
"@protobuf-java_3_25_3//jar",
7+
"@protobuf-java//jar",
88
],
99
)

examples/lang_toolchains/BUILD

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain")
2+
load("@rules_uv//uv:pip.bzl", "pip_compile")
3+
4+
pip_compile(
5+
name = "generate_requirements_txt",
6+
requirements_in = "requirements.in",
7+
requirements_txt = "requirements.txt",
8+
)
29

310
# Configure protoc to have the right arguments for generating Python stubs.
411
proto_lang_toolchain(
@@ -13,6 +20,6 @@ proto_lang_toolchain(
1320
name = "protoc_java_toolchain",
1421
command_line = "--java_out=%s",
1522
progress_message = "Generating Java proto_library %{label}",
16-
runtime = "@protobuf-java_3_25_3//jar",
23+
runtime = "@protobuf-java//jar",
1724
toolchain_type = "@rules_java//java/proto:toolchain_type",
1825
)
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
protobuf==5.27.0
+16-18
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
#
2-
# This file is autogenerated by pip-compile with Python 3.11
3-
# by the following command:
4-
#
5-
# bazel run //examples/lang_toolchains:requirements.update
6-
#
7-
protobuf==5.26.0 \
8-
--hash=sha256:2c334550e1cb4efac5c8a3987384bf13a4334abaf5ab59e40479e7b70ecd6b19 \
9-
--hash=sha256:6ee9d1aa02f951c5ce10bf8c6cfb7604133773038e33f913183c8b5201350600 \
10-
--hash=sha256:7e47c57303466c867374a17b2b5e99c5a7c8b72a94118e2f28efb599f19b4069 \
11-
--hash=sha256:82f5870d74c99addfe4152777bdf8168244b9cf0ac65f8eccf045ddfa9d80d9b \
12-
--hash=sha256:8eef61a90631c21b06b4f492a27e199a269827f046de3bb68b61aa84fcf50905 \
13-
--hash=sha256:a49b6c5359bf34fb7bf965bf21abfab4476e4527d822ab5289ee3bf73f291159 \
14-
--hash=sha256:ca825f4eecb8c342d2ec581e6a5ad1ad1a47bededaecd768e0d3451ae4aaac2b \
15-
--hash=sha256:dfd29f6eb34107dccf289a93d44fb6b131e68888d090b784b691775ac84e8213 \
16-
--hash=sha256:e184175276edc222e2d5e314a72521e10049938a9a4961fe4bea9b25d073c03f \
17-
--hash=sha256:efd4f5894c50bd76cbcfdd668cd941021333861ed0f441c78a83d8254a01cc9f \
18-
--hash=sha256:f9ecc8eb6f18037e0cbf43256db0325d4723f429bca7ef5cd358b7c29d65f628
1+
# This file was autogenerated by uv via the following command:
2+
# bazel run @@//examples/lang_toolchains:generate_requirements_txt
3+
--index-url https://pypi.org/simple
4+
5+
protobuf==5.27.0 \
6+
--hash=sha256:07f2b9a15255e3cf3f137d884af7972407b556a7a220912b252f26dc3121e6bf \
7+
--hash=sha256:2f83bf341d925650d550b8932b71763321d782529ac0eaf278f5242f513cc04e \
8+
--hash=sha256:56937f97ae0dcf4e220ff2abb1456c51a334144c9960b23597f044ce99c29c89 \
9+
--hash=sha256:587be23f1212da7a14a6c65fd61995f8ef35779d4aea9e36aad81f5f3b80aec5 \
10+
--hash=sha256:673ad60f1536b394b4fa0bcd3146a4130fcad85bfe3b60eaa86d6a0ace0fa374 \
11+
--hash=sha256:744489f77c29174328d32f8921566fb0f7080a2f064c5137b9d6f4b790f9e0c1 \
12+
--hash=sha256:7cb65fc8fba680b27cf7a07678084c6e68ee13cab7cace734954c25a43da6d0f \
13+
--hash=sha256:a17f4d664ea868102feaa30a674542255f9f4bf835d943d588440d1f49a3ed15 \
14+
--hash=sha256:aabbbcf794fbb4c692ff14ce06780a66d04758435717107c387f12fb477bf0d8 \
15+
--hash=sha256:b276e3f477ea1eebff3c2e1515136cfcff5ac14519c45f9b4aa2f6a87ea627c4 \
16+
--hash=sha256:f51f33d305e18646f03acfdb343aac15b8115235af98bc9f844bf9446573827b
1917
# via -r examples/lang_toolchains/requirements.in

protoc/private/prebuilt_protoc_toolchain.bzl

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ GOOGLE_PROTOBUF_DEP_EDGES = {
2424

2525
def _prebuilt_protoc_repo_impl(rctx):
2626
release_version = rctx.attr.version
27+
if release_version == "LATEST":
28+
release_version = PROTOC_VERSIONS.keys()[0]
2729

2830
filename = "{}-{}-{}.zip".format(
2931
"protoc",

0 commit comments

Comments
 (0)