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
pull/18435/head
Protobuf Team Bot 6 months ago committed by Copybara-Service
parent ed93fabf8e
commit cb1f518b4e
  1. 6
      bazel/cc_proto_library.bzl
  2. 6
      bazel/java_lite_proto_library.bzl
  3. 6
      bazel/java_proto_library.bzl
  4. 26
      bazel/private/toolchains/BUILD.bazel
  5. 4
      examples/BUILD.bazel

@ -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

@ -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)

@ -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)

@ -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",
)

@ -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

Loading…
Cancel
Save