feat(toolchain): support --incompatible_enable_proto_toolchain_resolution - #428
Open
kangnak wants to merge 1 commit into
Open
feat(toolchain): support --incompatible_enable_proto_toolchain_resolution#428kangnak wants to merge 1 commit into
kangnak wants to merge 1 commit into
Conversation
…tion When --incompatible_enable_proto_toolchain_resolution is enabled, resolve protoc from the registered proto toolchain (@rules_proto//proto:toolchain_type) rather than from //toolchain:protoc. The //toolchain:compiled toolchain hardcodes @com_google_protobuf//:protoc. Since bzlmod repo mappings are per-module, a user that swaps @com_google_protobuf for a prebuilt protoc repo (e.g. via toolchains_protoc) only changes the mapping of their own root module; this ruleset still resolves @com_google_protobuf to the protobuf module and protoc gets built from source. Resolution order for proto_compile is now: 1. the 'protoc' attribute, if set 2. the proto toolchain, when the flag is enabled and one is registered 3. the //toolchain:protoc toolchain With the flag disabled, behavior is unchanged: //toolchain:protoc stays a mandatory toolchain of the rule. With the flag enabled, both toolchain types are optional, so registering @build_stack_rules_proto//toolchain:standard is no longer required (registering it remains harmless). The compile action now passes the resolved FilesToRunProvider as a tool, so runfiles of a protoc wrapper are made available to the action. Fixes stackb#402 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #402
Problem
//toolchain:compiledhardcodes@com_google_protobuf//:protoc. Because bzlmod repo mappings are per-module, a user who swaps@com_google_protobuffor a prebuilt protoc repo — e.g. with toolchains_protoc — only changes the mapping of their own root module.build_stack_rules_protostill resolves@com_google_protobufto the protobuf module, so protoc is built from source. With the--per_file_coptguards recommended by toolchains_protoc, that surfaces as:Change
When
--incompatible_enable_proto_toolchain_resolutionis enabled,proto_compilenow sources protoc from the registered proto toolchain (@rules_proto//proto:toolchain_type) — the same toolchainproto_libraryitself uses, and the one toolchains_protoc registers.Resolution order:
protocrule attribute, if set//toolchain:protoctoolchainWith the flag disabled, behavior is unchanged:
//toolchain:protocremains a mandatory toolchain of the rule. With the flag enabled, both toolchain types are declared optional, soregister_toolchains("@build_stack_rules_proto//toolchain:standard")is no longer required — and keeping it registered is harmless, since the proto toolchain takes precedence.The compile action also passes the resolved
FilesToRunProvideras a tool now (instead of just the executableFile), so runfiles of a protoc wrapper are available to the action.Files
toolchain/toolchain.bzl— addsINCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION,use_protoc_toolchains()andfind_protoc().rules/proto_compile.bzl— uses the helpers.get_protoc_executable()is kept as a deprecated shim.docs/TOOLCHAINS.md— documents the flag and a toolchains_protoc setup.Testing
A stub toolchain of type
//toolchain:protocwhose protoc always exits non-zero was registered via--extra_toolchains, to prove which protoc is actually selected://toolchain:protocAlso verified:
bazel build //...andbazel test //...pass (29/29, including//tools:buildifier.check)bazel build --incompatible_enable_proto_toolchain_resolution //example/... //google/...passes🤖 Generated with Claude Code