Skip to content

Commit e1700f0

Browse files
authored
fix: bzlmod users don't need to register toolchain (#75)
* fix: bzlmod users don't need to register toolchain The toolchains_protoc MODULE file registers the protoc toolchain itself. * docs: clarify ordering
1 parent f467eb3 commit e1700f0

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

.github/workflows/release_prep.sh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,16 @@ cat << EOF
2222
2. Add to your \`MODULE.bazel\` file:
2323
2424
\`\`\`starlark
25+
# NB: this must come BEFORE bazel_dep(name = "protobuf") because they register the from-source toolchain,
26+
# and the first registration wins.
2527
bazel_dep(name = "toolchains_protoc", version = "${TAG:1}")
2628
2729
# Optional: choose a version of protoc rather than the latest.
2830
protoc = use_extension("@toolchains_protoc//protoc:extensions.bzl", "protoc")
2931
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",
3332
# Pin to any version of protoc
3433
version = "v26.0",
3534
)
36-
use_repo(protoc, "com_google_protobuf", "toolchains_protoc_hub")
37-
38-
register_toolchains("@toolchains_protoc_hub//:all")
3935
\`\`\`
4036
4137
## Using WORKSPACE

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ Enable the toolchain support by adding this to `.bazelrc`:
5656
common --incompatible_enable_proto_toolchain_resolution
5757
```
5858

59+
Make sure this module wins the toolchain registration for the
60+
[`@rules_proto//proto:toolchain_type` symbol](https://github.com/bazelbuild/rules_proto/blob/74961e561111a3510d5c25233477b950379ae06d/proto/BUILD#L58), either by ensuring the `bazel_dep` for `toolchains_protoc` is listed BEFORE the one for `protobuf`, or explicitly register the toolchain in your own `MODULE.bazel`:
61+
62+
```
63+
# Override the toolchain registration from the protobuf module
64+
protoc = use_extension("@toolchains_protoc//protoc:extensions.bzl", "protoc")
65+
use_repo(protoc, "toolchains_protoc_hub")
66+
register_toolchains("@toolchains_protoc_hub//:all")
67+
```
68+
5969
### For each language, follow these steps
6070

6171
Since the user installs the proto runtimes through their existing package manager setup,
@@ -99,8 +109,11 @@ See `examples` for several language rules like `py_proto_library` and `java_prot
99109

100110
### Troubleshooting
101111

102-
What if you still see that protoc is compiling? This means that there is still a transitive dependency on the
103-
`com_google_protobuf` module, likely from some macro call in your `WORKSPACE` file.
112+
What if you still see that protoc is compiling?
113+
114+
1. Check that toolchains_protoc `bazel_dep` is BEFORE `protobuf`, see Installation above.
115+
1. This could mean that there is still a transitive dependency on the
116+
`com_google_protobuf` module, likely from some macro call in your `WORKSPACE` file.
104117

105118
> TODO: explain how to track down where the `com_google_protobuf` dependency is coming from.
106119

examples/.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ common --host_platform=//tools:no_cgo_host_platform
77
common --@aspect_rules_ts//ts:skipLibCheck=always
88
common --@aspect_rules_ts//ts:default_to_tsc_transpiler
99

10+
common --java_runtime_version=remotejdk_11
11+
1012
# Ensure that we don't accidentally build protobuf or gRPC
1113
common --per_file_copt=external/.*protobuf.*@--PROTOBUF_WAS_NOT_SUPPOSED_TO_BE_BUILT
1214
common --host_per_file_copt=external/.*protobuf.*@--PROTOBUF_WAS_NOT_SUPPOSED_TO_BE_BUILT

examples/MODULE.bazel

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ protoc.toolchain(
3737
# Demonstrate overriding the default version
3838
version = "v28.0",
3939
)
40-
use_repo(protoc, "com_google_protobuf", "toolchains_protoc_hub")
41-
42-
register_toolchains("@toolchains_protoc_hub//:all")
40+
use_repo(protoc, "com_google_protobuf")
4341

4442
# NB: the `:all` here is critical, because `proto_lang_toolchain` expands into two targets:
4543
# - proto_lang_toolchain rule [name]

0 commit comments

Comments
 (0)