From cb1f518b4e291051d60b5d5c39548dda84a38ffe Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Tue, 24 Sep 2024 16:54:01 -0700 Subject: [PATCH] Fix mixing rules from builtins and protobuf The rules in protobuf and built-in Bazel don't mix well. Different aspects may cause actions conflicts. The safest way is to use built-in rules whenever they are still present. proto_library should be safe, because it's not using an aspect. py_proto_library never had a built-in implementation, so it's also safe. This still makes it possible to use the rules on Bazel 7, but only with --incompatible_autoload_externally enabled. PiperOrigin-RevId: 678446311 --- bazel/cc_proto_library.bzl | 6 ++---- bazel/java_lite_proto_library.bzl | 6 ++---- bazel/java_proto_library.bzl | 6 ++---- bazel/private/toolchains/BUILD.bazel | 26 ++++++++++++++++++++++++++ examples/BUILD.bazel | 4 +++- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/bazel/cc_proto_library.bzl b/bazel/cc_proto_library.bzl index ccbdae9741..e1d4ad4144 100644 --- a/bazel/cc_proto_library.bzl +++ b/bazel/cc_proto_library.bzl @@ -1,12 +1,10 @@ """cc_proto_library rule""" -load("@proto_bazel_features//:features.bzl", "bazel_features") load("//bazel/private:bazel_cc_proto_library.bzl", _cc_proto_library = "cc_proto_library") # buildifier: disable=bzl-visibility def cc_proto_library(**kwattrs): - # This condition causes Starlark rules to be used only on Bazel >=7.0.0 - if bazel_features.proto.starlark_proto_info: + # Only use Starlark rules when they are removed from Bazel + if not hasattr(native, "cc_proto_library"): _cc_proto_library(**kwattrs) else: - # On older Bazel versions keep using native rules, so that mismatch in ProtoInfo doesn't happen native.cc_proto_library(**kwattrs) # buildifier: disable=native-cc-proto diff --git a/bazel/java_lite_proto_library.bzl b/bazel/java_lite_proto_library.bzl index 9332455db8..f3ed40803a 100644 --- a/bazel/java_lite_proto_library.bzl +++ b/bazel/java_lite_proto_library.bzl @@ -6,13 +6,11 @@ # https://developers.google.com/open-source/licenses/bsd """java_lite_proto_library rule""" -load("@proto_bazel_features//:features.bzl", "bazel_features") load("//bazel/private:java_lite_proto_library.bzl", _java_lite_proto_library = "java_lite_proto_library") # buildifier: disable=bzl-visibility def java_lite_proto_library(**kwattrs): - # This condition causes Starlark rules to be used only on Bazel >=7.0.0 - if bazel_features.proto.starlark_proto_info: + # Only use Starlark rules when they are removed from Bazel + if not hasattr(native, "java_lite_proto_library"): _java_lite_proto_library(**kwattrs) else: - # On older Bazel versions keep using native rules, so that mismatch in ProtoInfo doesn't happen native.java_lite_proto_library(**kwattrs) diff --git a/bazel/java_proto_library.bzl b/bazel/java_proto_library.bzl index bc101909b3..57af8c48f3 100644 --- a/bazel/java_proto_library.bzl +++ b/bazel/java_proto_library.bzl @@ -6,13 +6,11 @@ # https://developers.google.com/open-source/licenses/bsd """java_proto_library rule""" -load("@proto_bazel_features//:features.bzl", "bazel_features") load("//bazel/private:bazel_java_proto_library_rule.bzl", _java_proto_library = "java_proto_library") # buildifier: disable=bzl-visibility def java_proto_library(**kwattrs): - # This condition causes Starlark rules to be used only on Bazel >=7.0.0 - if bazel_features.proto.starlark_proto_info: + # Only use Starlark rules when they are removed from Bazel + if not hasattr(native, "java_proto_library"): _java_proto_library(**kwattrs) else: - # On older Bazel versions keep using native rules, so that mismatch in ProtoInfo doesn't happen native.java_proto_library(**kwattrs) diff --git a/bazel/private/toolchains/BUILD.bazel b/bazel/private/toolchains/BUILD.bazel index 1f30c35d65..b727a4a975 100644 --- a/bazel/private/toolchains/BUILD.bazel +++ b/bazel/private/toolchains/BUILD.bazel @@ -46,3 +46,29 @@ toolchain( toolchain = "//python:python_toolchain", toolchain_type = "//bazel/private:python_toolchain_type", ) + +# Following toolchain registrations are for builtin Bazel 7 rules +# which defined them in other repositories. +toolchain( + name = "cc_source_toolchain_bazel7", + exec_compatible_with = [], + target_compatible_with = [], + toolchain = "//:cc_toolchain", + toolchain_type = "@rules_cc//cc/proto:toolchain_type", +) + +toolchain( + name = "java_source_toolchain_bazel7", + exec_compatible_with = [], + target_compatible_with = [], + toolchain = "//java/core:toolchain", + toolchain_type = "@rules_java//java/proto:toolchain_type", +) + +toolchain( + name = "javalite_source_toolchain_bazel7", + exec_compatible_with = [], + target_compatible_with = [], + toolchain = "//java/lite:toolchain", + toolchain_type = "@rules_java//java/proto:lite_toolchain_type", +) diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel index ff4256e3aa..c7d4ff300b 100644 --- a/examples/BUILD.bazel +++ b/examples/BUILD.bazel @@ -5,7 +5,6 @@ # example. load("@bazel_skylib//rules:build_test.bzl", "build_test") -load("@protobuf//bazel:cc_proto_library.bzl", "cc_proto_library") load("@protobuf//bazel:java_lite_proto_library.bzl", "java_lite_proto_library") load("@protobuf//bazel:java_proto_library.bzl", "java_proto_library") load("@protobuf//bazel:proto_library.bzl", "proto_library") @@ -14,6 +13,9 @@ load("@rules_cc//cc:defs.bzl", "cc_binary") load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix") load("@rules_python//python:py_binary.bzl", "py_binary") +# cc_proto_library is intentionally not loaded, to test Bazel's built-in implementation +# against Protobuf's implementation (already used building protoc) + # For each .proto file, a proto_library target should be defined. This target # is not bound to any particular language. Instead, it defines the dependency # graph of the .proto files (i.e., proto imports) and serves as the provider