Skip to content

Commit da0077e

Browse files
zhangskzcopybara-github
authored andcommitted
Use native.proto_library for Bazel 7 and explicitly error for Bazel 6 which is incompatible and out of support.
Reduces skew caused by Bazel 7 using bazel's cc_proto_library, protobuf's proto_library, and bazel's proto info which block changes e.g. cl/743679882 Pins `rules_proto` to `7.1.0` to avoid `Protocol compiler toolchain could not be resolved.` when `--incompatible_toolchain_resolution`. MODULE.bazel otherwise defaults to `rules_proto 7.0.2` (from other deps) which inconsistently uses bazel's toolchain_type but protobuf's toolchain. WORKSPACE otherwise defaults to `rules_proto 5.3.0-21.7` (from bazel) where `@rules_proto//proto:toolchain_type` referenced by Bazel 7 doesn't exist yet. The final state is that toolchains from protobuf are used, instead of @rules_proto which forwards to protobuf after `rules_proto 6.0.0` to avoid breaking change requiring users to register toolchains from `rules_proto` for Bazel 7. PiperOrigin-RevId: 762571203
1 parent 1fd2906 commit da0077e

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

.github/workflows/test_bazel.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
uses: protocolbuffers/protobuf-ci/bazel@v4
6868
with:
6969
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
70-
bazel-cache: examples
70+
bazel-cache: examples-${{ matrix.bazelversion }}-${{ matrix.bzlmod }}-${{ matrix.toolchain_resolution }}
7171
version: ${{ matrix.bazelversion }}
7272
bash: >
7373
cd examples;

MODULE.bazel

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ bazel_dep(name = "rules_license", version = "1.0.0")
4646
bazel_dep(name = "rules_pkg", version = "1.0.1")
4747
bazel_dep(name = "rules_python", version = "1.0.0")
4848

49+
# Pin to rules_proto to 7.1.0 to avoid toolchain incompatibilities when
50+
# --incompatible_enable_proto_toolchain_resolution=true in Bazel 7.
51+
# rules_proto 7.0.2 from deps incorrectly rules_proto's toolchain_type but protobuf's toolchain, but
52+
# 7.1.0 uses protobuf's toolchain and toolchain_type.
53+
# TODO: Restore to dev_dependency once Bazel 7 is dropped.
54+
# rules_proto is needed for @com_google_protobuf_v25 used in //compatibility/... tests
55+
bazel_dep(name = "rules_proto", version = "7.1.0")
56+
4957
bazel_dep(name = "rules_rust", version = "0.56.0", dev_dependency = True)
5058
bazel_dep(name = "rules_ruby", version = "0.17.3", dev_dependency = True)
5159

@@ -82,6 +90,7 @@ ruby.toolchain(
8290
version = "system",
8391
)
8492
use_repo(ruby, "ruby")
93+
8594
ruby.bundle_fetch(
8695
name = "protobuf_bundle",
8796
gem_checksums = {
@@ -227,9 +236,6 @@ bazel_dep(
227236
cc_configure = use_extension("@rules_cc//cc:extensions.bzl", "cc_configure_extension")
228237
use_repo(cc_configure, "local_config_cc")
229238

230-
# rules_proto are needed for @com_google_protobuf_v25 used in //compatibility/... tests
231-
bazel_dep(name = "rules_proto", version = "7.0.2", dev_dependency = True)
232-
233239
# For the Lua upb implementation
234240
bazel_dep(name = "lua", version = "5.4.6", dev_dependency = True)
235241

bazel/proto_library.bzl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ load("@proto_bazel_features//:features.bzl", "bazel_features")
1111
load("//bazel/private:proto_library_rule.bzl", _proto_library = "proto_library")
1212

1313
def proto_library(**kwattrs):
14-
# This condition causes Starlark rules to be used only on Bazel >=7.0.0
15-
if bazel_features.proto.starlark_proto_info:
14+
# Bazel 6 predates Starlark ProtoInfo and is not compatible with Starklark proto_library.
15+
if not bazel_features.proto.starlark_proto_info:
16+
fail("Protobuf proto_library is not compatible with Bazel 6.")
17+
18+
# Only use Starlark rules when they are removed from Bazel.
19+
if not hasattr(native, "proto_library"):
1620
_proto_library(**kwattrs)
1721
else:
1822
# On older Bazel versions keep using native rules, so that mismatch in ProtoInfo doesn't happen

protobuf_deps.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def _github_archive(repo, commit, **kwargs):
5353
def protobuf_deps():
5454
"""Loads common dependencies needed to compile the protobuf library."""
5555

56+
# Pin rules_proto since Bazel 7 otherwise depends on rules_proto 5.3.0-21.7 which is missing
57+
# @rules_proto//proto:toolchain_type used by Bazel.
58+
# 6.0.0 would at least require users to add `register_toolchains` for rules_proto
59+
# TODO: Remove once Bazel 7 is no longer supported.
60+
if not native.existing_rule("rules_proto"):
61+
http_archive(
62+
name = "rules_proto",
63+
strip_prefix = "rules_proto-7.1.0",
64+
url = "https://github.com/bazelbuild/rules_proto/releases/download/7.1.0/rules_proto-7.1.0.tar.gz",
65+
)
5666
if not native.existing_rule("bazel_features"):
5767
http_archive(
5868
name = "bazel_features",

0 commit comments

Comments
 (0)