SIGN IN SIGN UP

Protocol Buffers - Google's data interchange format

0 0 58 C++

Fix _proto_gen to use prebuilt protoc (fixes #25759) (#25760)

## Summary

- **Fixes #25759**: `py_proto_library` (and other internal `*_proto_library` macros) now correctly use the prebuilt protoc when `--@protobuf//bazel/toolchains:prefer_prebuilt_protoc` is set, avoiding ~1091 unnecessary CppCompile actions.
- The deprecated `_proto_gen` rule bypassed Bazel toolchain resolution and always depended on `//:protoc` (the cc_binary) via a legacy `_proto_compiler` attribute. This change makes `_proto_gen` always participate in toolchain resolution.
- No behavioral regression when `prefer_prebuilt_protoc` is not set — the from-source `PROTO_TOOLCHAIN` (registered without constraints) always matches as a fallback, resolving to the same `//:protoc` binary.

### Root cause

`INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION` evaluates to `False` in Bazel 8 (`proto_common_do_not_use` no longer exposes this attribute). In this legacy mode, `_proto_gen` used a `_proto_compiler` attribute defaulting to `configuration_field("proto", "proto_compiler")` → `@bazel_tools//tools/proto:protoc` → `//:protoc`, and declared **no** toolchain dependency. The `prefer_prebuilt_protoc` flag only works via toolchain resolution, which `_proto_gen` never invoked.

### Changes in `protobuf.bzl`

1. **Always declare `PROTO_TOOLCHAIN` dependency** (`mandatory=False` for safety)
2. **Remove legacy `_proto_compiler` attribute** (eliminates the hard dependency on the cc_binary)
3. **Simplify `_protoc_files_to_run`** to always use toolchain resolution
4. **Remove unused `proto_common` import**

### Verification

```
# With prefer_prebuilt_protoc: no path to //:protoc
$ bazel cquery --//bazel/toolchains:prefer_prebuilt_protoc \
    'somepath(//python:well_known_types_py_pb2, //:protoc)'
→ INFO: Empty query results  ✓

# Without prefer_prebuilt_protoc: //:protoc still reachable via toolchain (no regression)
$ bazel cquery 'somepath(//python:well_known_types_py_pb2, //:protoc)'
→ //python:well_known_types_py_pb2 → ...toolchains:protoc_sources → //:protoc  ✓

# With prebuilt: 15 total actions, 0 CppCompile
$ bazel build --//bazel/toolchains:prefer_prebuilt_protoc //python:well_known_types_py_pb2
→ 15 processes, 0 CppCompile  ✓

# Without prebuilt: builds normally (no regression)
$ bazel build //python:well_known_types_py_pb2
→ 684 processes, Build completed successfully  ✓
```

## Test plan

- [x] `bazel cquery` confirms no path to `//:protoc` with `prefer_prebuilt_protoc`
- [x] `bazel cquery` confirms `//:protoc` reachable via toolchain without the flag
- [x] Build `//python:well_known_types_py_pb2` with prebuilt — 0 CppCompile actions
- [x] Build `//python:well_known_types_py_pb2` without prebuilt — works as before
- [x] `//python/...` — 26/26 tests pass
- [x] `//src/...` — 103/103 tests pass
- [x] `//upb/...` — 41/41 tests pass
- [x] `//bazel/tests:py_proto_library_test_suite` + other rule tests — 45/45 pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Closes #25760

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/25760 from mkanat:fix/proto-gen-use-prebuilt-protoc 0191a259c9f422a7fc4b845a264f5439ee41e18b
PiperOrigin-RevId: 917200931
M
Max Kanat-Alexander committed
a73807577434261242c4203fe4102a9536aabe7f
Parent: bb940d7
Committed by Copybara-Service <copybara-worker@google.com> on 5/18/2026, 1:49:42 PM