diff --git a/bazel/flags/BUILD b/bazel/flags/BUILD index 393fab95d63ee..f5a261ec69c98 100644 --- a/bazel/flags/BUILD +++ b/bazel/flags/BUILD @@ -1,5 +1,6 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag", "string_list_flag") +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") +load("//bazel/private:compat_flag.bzl", "compat_bool_flag", "compat_string_list_flag") package( default_applicable_licenses = ["//:license"], @@ -25,9 +26,10 @@ filegroup( ], ) -bool_flag( +compat_bool_flag( name = "experimental_proto_descriptor_sets_include_source_info", build_setting_default = False, + native_flag = "experimental_proto_descriptor_sets_include_source_info", scope = "universal", ) @@ -36,9 +38,10 @@ label_flag( build_setting_default = "@bazel_tools//tools/proto:protoc", ) -string_list_flag( +compat_string_list_flag( name = "protocopt", build_setting_default = [], + fragment_field = "experimental_protoc_opts", scope = "universal", ) @@ -75,39 +78,45 @@ config_setting( ) # TODO: deprecate this flag. -string_flag( +compat_bool_flag( name = "strict_proto_deps", - build_setting_default = "error", + build_setting_default = True, + native_flag = "strict_proto_deps", scope = "universal", - values = [ - "off", - "OFF", - "warn", - "WARN", - "error", - "ERROR", - "strict", - "STRICT", - "default", - "DEFAULT", - ], + values = { + False: [ + "off", + "OFF", + ], + True: [ + "warn", + "WARN", + "error", + "ERROR", + "strict", + "STRICT", + ], + }, ) # TODO: deprecate this flag. -string_flag( +compat_bool_flag( name = "strict_public_imports", - build_setting_default = "off", + build_setting_default = False, + native_flag = "strict_public_imports", scope = "universal", - values = [ - "off", - "OFF", - "warn", - "WARN", - "error", - "ERROR", - "strict", - "STRICT", - "default", - "DEFAULT", - ], + values = { + False: [ + "off", + "OFF", + ], + True: [ + "warn", + "WARN", + "error", + "ERROR", + "strict", + "STRICT", + ], + }, ) diff --git a/bazel/flags/cc/BUILD b/bazel/flags/cc/BUILD index a1a282ead4af2..697ad825df51a 100644 --- a/bazel/flags/cc/BUILD +++ b/bazel/flags/cc/BUILD @@ -1,5 +1,6 @@ -load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_list_flag") +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") load("@rules_shell//shell:sh_binary.bzl", "sh_binary") +load("//bazel/private:compat_flag.bzl", "compat_string_list_flag") package( default_applicable_licenses = ["//:license"], @@ -19,14 +20,16 @@ alias( deprecation = "Use //bazel/flags:protocopt instead.", ) -string_list_flag( +compat_string_list_flag( name = "cc_proto_library_header_suffixes", build_setting_default = [".pb.h"], + fragment_field = "cc_proto_library_header_suffixes", scope = "universal", ) -string_list_flag( +compat_string_list_flag( name = "cc_proto_library_source_suffixes", build_setting_default = [".pb.cc"], + fragment_field = "cc_proto_library_source_suffixes", scope = "universal", ) diff --git a/bazel/flags/flags.bzl b/bazel/flags/flags.bzl index 8ef051a9193c4..0a67f20c89e59 100644 --- a/bazel/flags/flags.bzl +++ b/bazel/flags/flags.bzl @@ -8,75 +8,22 @@ visibility([ "//third_party/grpc/bazel", ]) -# Maps flag names to their native reference -_FLAGS = { - "protocopt": struct( - native = lambda ctx: getattr(ctx.fragments.proto, "experimental_protoc_opts"), - default = [], - ), - "experimental_proto_descriptor_sets_include_source_info": struct( - native = lambda ctx: getattr(ctx.attr, "_experimental_proto_descriptor_sets_include_source_info_native")[BuildSettingInfo].value, - default = False, - ), - "proto_compiler": struct(native = lambda ctx: getattr(ctx.attr, "_proto_compiler_native")[BuildSettingInfo].value, default = "@bazel_tools//tools/proto:protoc"), - "strict_proto_deps": struct( - native = lambda ctx: getattr(ctx.attr, "_strict_proto_deps_native")[BuildSettingInfo].value, - default = "error", - ), - "strict_public_imports": struct( - native = lambda ctx: getattr(ctx.attr, "_strict_public_imports_native")[BuildSettingInfo].value, - default = "off", - ), - "cc_proto_library_header_suffixes": struct( - native = lambda ctx: getattr(ctx.fragments.proto, "cc_proto_library_header_suffixes"), - default = [".pb.h"], - ), - "cc_proto_library_source_suffixes": struct( - native = lambda ctx: getattr(ctx.fragments.proto, "cc_proto_library_source_suffixes"), - default = [".pb.cc"], - ), - "proto_toolchain_for_java": struct( - native = lambda ctx: "//:java_toolchain", - default = "//:java_toolchain", - ), - "proto_toolchain_for_javalite": struct( - native = lambda ctx: "//:javalite_toolchain", - default = "//:javalite_toolchain", - ), - "proto_toolchain_for_cc": struct( - native = lambda ctx: "//:cc_toolchain", - default = "//:cc_toolchain", - ), -} - def get_flag_value(ctx, flag_name): - """Returns the value of the given flag in Starlark if it's set, otherwise reads the Java flag value, if the proto fragment exists. + """Returns the value of the given flag attribute from rule context. Args: ctx: The rule context. flag_name: The name of the flag to get the value for. Returns: - The value of the flag. + The value of the flag. If the value is a list, returns a mutable copy. """ - - # We probably got here from toolchains.find_toolchain. Leave the attribute alone. - if flag_name not in _FLAGS: - return getattr(ctx.attr, "_" + flag_name) - - starlark_flag = getattr(ctx.attr, "_" + flag_name) - - # Label flags don't have a BuildSettingInfo, just get the value. - if "toolchain" in flag_name: - starlark_flag_is_set = starlark_flag.label != _FLAGS[flag_name].default - starlark_value = starlark_flag - else: - starlark_flag_is_set = starlark_flag[BuildSettingInfo].value != _FLAGS[flag_name].default - starlark_value = starlark_flag[BuildSettingInfo].value - - # Starlark flags take precedence over native flags. - # Also of course, use the Starlark value if the proto fragment no longer exists. - if starlark_flag_is_set or not hasattr(ctx.fragments, "proto"): - return starlark_value - else: - return _FLAGS[flag_name].native(ctx) + attr_val = getattr(ctx.attr, "_" + flag_name) + if BuildSettingInfo in attr_val: + val = attr_val[BuildSettingInfo].value + if type(val) == "list": + return list(val) + return val + if type(attr_val) == "list": + return list(attr_val) + return attr_val diff --git a/bazel/private/BUILD b/bazel/private/BUILD index fc28f6ba46055..bb1774667fc9e 100644 --- a/bazel/private/BUILD +++ b/bazel/private/BUILD @@ -1,5 +1,4 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -load(":native_bool_flag.bzl", "native_bool_flag") package(default_applicable_licenses = ["//:license"]) @@ -168,32 +167,9 @@ bzl_library( ], ) -native_bool_flag( - name = "experimental_proto_descriptor_sets_include_source_info", - flag = "experimental_proto_descriptor_sets_include_source_info", - match_value = "true", - visibility = ["//bazel:__subpackages__"], -) - -native_bool_flag( - name = "strict_proto_deps", - flag = "strict_proto_deps", - match_value = "off", - result = False, - visibility = ["//bazel:__subpackages__"], -) - -native_bool_flag( - name = "strict_public_imports", - flag = "strict_public_imports", - match_value = "off", - result = False, - visibility = ["//bazel:__subpackages__"], -) - bzl_library( - name = "native_bool_flag_bzl", - srcs = ["native_bool_flag.bzl"], + name = "compat_flag_bzl", + srcs = ["compat_flag.bzl"], visibility = ["//visibility:private"], deps = ["@bazel_skylib//rules:common_settings"], ) @@ -203,8 +179,8 @@ filegroup( testonly = True, srcs = [ "BUILD", + ":compat_flag_bzl", ":java_proto_library_bzl", - ":native_bool_flag_bzl", ":toolchain_helpers_bzl", "//bazel:for_bazel_tests", "//bazel/private/oss/toolchains:for_bazel_tests", diff --git a/bazel/private/compat_flag.bzl b/bazel/private/compat_flag.bzl new file mode 100644 index 0000000000000..15f31ae2dda1c --- /dev/null +++ b/bazel/private/compat_flag.bzl @@ -0,0 +1,173 @@ +# Protocol Buffers - Google's data interchange format +# Copyright 2008 Google Inc. All rights reserved. +# +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file or at +# https://developers.google.com/open-source/licenses/bsd +"""Helper rules and macros for custom proto flags.""" + +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo", "string_flag") + +def _compat_bool_rule_impl(ctx): + starlark_val = ctx.attr.starlark_flag[BuildSettingInfo].value + + # 1. Starlark flag takes precedence if explicitly passed (not "default") + if starlark_val != "default": + if starlark_val in ctx.attr.true_values: + return [BuildSettingInfo(value = True)] + if starlark_val in ctx.attr.false_values: + return [BuildSettingInfo(value = False)] + allowed = ctx.attr.true_values + ctx.attr.false_values + ["default"] + fail("Invalid value '%s' for flag %s. Allowed values are: %s" % ( + starlark_val, + ctx.label, + allowed, + )) + + # 2. Native flag fallback (if matched via config_setting) + if ctx.attr.native_true_matched: + return [BuildSettingInfo(value = True)] + if ctx.attr.native_false_matched: + return [BuildSettingInfo(value = False)] + + # 3. Default fallback + return [BuildSettingInfo(value = ctx.attr.default_value)] + +_compat_bool_rule = rule( + implementation = _compat_bool_rule_impl, + attrs = { + "starlark_flag": attr.label(mandatory = True), + "native_true_matched": attr.bool(default = False), + "native_false_matched": attr.bool(default = False), + "true_values": attr.string_list(default = []), + "false_values": attr.string_list(default = []), + "default_value": attr.bool(default = False), + }, +) + +def _make_config_settings_select(prefix, flag_name, match_list): + if not match_list or not flag_name: + return False + select_cases = {} + for i, val in enumerate(match_list): + setting_name = "%s_%d" % (prefix, i) + native.config_setting( + name = setting_name, + values = {flag_name: val}, + visibility = ["//visibility:private"], + ) + select_cases[":" + setting_name] = True + select_cases["//conditions:default"] = False + return select(select_cases) + +def compat_bool_flag( + *, + name, + native_flag = None, + build_setting_default = False, + default = None, + values = None, + visibility = None, + **kwargs): + """Creates a custom build setting flag and its reconciled compat target. + + Args: + name: The target name for the Starlark build setting flag. + native_flag: The native command-line option name (e.g. "strict_proto_deps"), if any. + build_setting_default: Fallback default boolean value if neither Starlark nor native flag is set. + default: Deprecated alias for build_setting_default. + values: Dict mapping booleans (True/False) to lists of accepted string flag values. + Defaults to {True: ["true", "TRUE", "1"], False: ["false", "FALSE", "0"]}. + visibility: Target visibility list. + **kwargs: Additional rule arguments (such as `scope`). + """ + if default != None: + build_setting_default = default + + if values == None: + values = { + True: ["true", "TRUE", "1"], + False: ["false", "FALSE", "0"], + } + + true_vals = values.get(True, []) + false_vals = values.get(False, []) + + starlark_true_vals = [v for v in true_vals if v != "default"] + starlark_false_vals = [v for v in false_vals if v != "default"] + + all_values = {v: True for v in true_vals + false_vals + ["default", "DEFAULT"]}.keys() + string_flag( + name = name, + build_setting_default = "default", + values = all_values, + visibility = visibility, + **kwargs + ) + + _compat_bool_rule( + name = name + "_compat", + starlark_flag = ":" + name, + native_true_matched = _make_config_settings_select( + name + "_native_true", + native_flag, + true_vals, + ), + native_false_matched = _make_config_settings_select( + name + "_native_false", + native_flag, + false_vals, + ), + true_values = starlark_true_vals, + false_values = starlark_false_vals, + default_value = build_setting_default, + visibility = ["//bazel:__subpackages__"], + ) + +def _compat_string_list_rule_impl(ctx): + starlark_val = ctx.build_setting_value + if starlark_val: + return [BuildSettingInfo(value = starlark_val)] + if hasattr(ctx.fragments, "proto") and ctx.attr.fragment_field: + val = getattr(ctx.fragments.proto, ctx.attr.fragment_field) + return [BuildSettingInfo(value = val)] + return [BuildSettingInfo(value = ctx.attr.default_value)] + +_compat_string_list_rule = rule( + implementation = _compat_string_list_rule_impl, + build_setting = config.string_list(flag = True, repeatable = True), + fragments = ["proto"], + attrs = { + "fragment_field": attr.string(), + "default_value": attr.string_list(default = []), + "scope": attr.string(), + }, +) + +def compat_string_list_flag( + *, + name, + fragment_field = None, + build_setting_default = None, + default = None, + **kwargs): + """Creates a custom string-list build setting reconciling Starlark/fragments. + + Args: + name: The target name for the Starlark build setting flag. + fragment_field: The field name in ctx.fragments.proto (e.g. "experimental_protoc_opts"), if any. + build_setting_default: Fallback default string list value if neither Starlark nor fragment is set. + default: Deprecated alias for build_setting_default. + **kwargs: Additional rule arguments (such as `scope`). + """ + if default != None: + build_setting_default = default + default_vals = build_setting_default if build_setting_default != None else [] + + _compat_string_list_rule( + name = name, + build_setting_default = default_vals, + fragment_field = fragment_field, + default_value = default_vals, + **kwargs + ) diff --git a/bazel/private/native_bool_flag.bzl b/bazel/private/native_bool_flag.bzl deleted file mode 100644 index 960fbed5128e7..0000000000000 --- a/bazel/private/native_bool_flag.bzl +++ /dev/null @@ -1,35 +0,0 @@ -# Protocol Buffers - Google's data interchange format -# Copyright 2008 Google Inc. All rights reserved. -# -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file or at -# https://developers.google.com/open-source/licenses/bsd -""" -A helper rule that reads a native boolean flag. -""" - -load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") - -def _impl(ctx): - return [BuildSettingInfo(value = ctx.attr.value)] - -_native_bool_flag_rule = rule( - implementation = _impl, - attrs = {"value": attr.bool()}, -) - -def native_bool_flag(*, name, flag, match_value = "true", result = True, **kwargs): - _native_bool_flag_rule( - name = name, - value = select({ - name + "_setting": result, - "//conditions:default": not result, - }), - **kwargs - ) - - native.config_setting( - name = name + "_setting", - values = {flag: match_value}, - visibility = ["//visibility:private"], - ) diff --git a/bazel/private/oss/cc_proto_library.bzl b/bazel/private/oss/cc_proto_library.bzl index 3d4444cce2eb3..f0f44023b81ca 100644 --- a/bazel/private/oss/cc_proto_library.bzl +++ b/bazel/private/oss/cc_proto_library.bzl @@ -7,11 +7,11 @@ # """Bazel's implementation of cc_proto_library""" +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("@rules_cc//cc:find_cc_toolchain.bzl", "use_cc_toolchain") load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") load("//bazel/common:proto_common.bzl", "proto_common") load("//bazel/common:proto_info.bzl", "ProtoInfo") -load("//bazel/flags:flags.bzl", "get_flag_value") load("//bazel/private:cc_proto_support.bzl", "cc_proto_compile_and_link") load("//bazel/private:toolchain_helpers.bzl", "toolchains") @@ -59,8 +59,8 @@ def _aspect_impl(target, ctx): if should_generate_code: if len(proto_info.direct_sources) != 0: - source_suffixes = get_flag_value(ctx, "cc_proto_library_source_suffixes") - header_suffixes = get_flag_value(ctx, "cc_proto_library_header_suffixes") + source_suffixes = ctx.attr._cc_proto_library_source_suffixes[BuildSettingInfo].value + header_suffixes = ctx.attr._cc_proto_library_header_suffixes[BuildSettingInfo].value sources = _get_output_files(ctx.actions, proto_info, source_suffixes) headers = _get_output_files(ctx.actions, proto_info, header_suffixes) header_provider = _ProtoCcHeaderInfo(headers = depset(headers)) diff --git a/bazel/private/proto_lang_toolchain_rule.bzl b/bazel/private/proto_lang_toolchain_rule.bzl index 0ac0515168069..00492b7304b9b 100644 --- a/bazel/private/proto_lang_toolchain_rule.bzl +++ b/bazel/private/proto_lang_toolchain_rule.bzl @@ -7,11 +7,11 @@ # """Implementation of the proto_lang_toolchain rule.""" +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("@proto_bazel_features//:features.bzl", "bazel_features") load("//bazel/common:proto_common.bzl", "proto_common") load("//bazel/common:proto_info.bzl", "ProtoInfo") load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo") -load("//bazel/flags:flags.bzl", "get_flag_value") load("//bazel/private:toolchain_helpers.bzl", "toolchains") def _rule_impl(ctx): @@ -34,7 +34,7 @@ def _rule_impl(ctx): protoc_opts = ctx.toolchains[toolchains.PROTO_TOOLCHAIN].proto.protoc_opts else: proto_compiler = ctx.attr._proto_compiler.files_to_run - protoc_opts = get_flag_value(ctx, "protocopt") + protoc_opts = list(ctx.attr._protocopt[BuildSettingInfo].value) if ctx.attr.protoc_minimal_do_not_use: proto_compiler = ctx.attr.protoc_minimal_do_not_use.files_to_run diff --git a/bazel/private/proto_library_rule.bzl b/bazel/private/proto_library_rule.bzl index 41083703ef375..fcd28a5145593 100644 --- a/bazel/private/proto_library_rule.bzl +++ b/bazel/private/proto_library_rule.bzl @@ -9,10 +9,10 @@ Implementation of proto_library rule. """ load("@bazel_skylib//lib:paths.bzl", "paths") +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("@proto_bazel_features//:features.bzl", "bazel_features") load("//bazel/common:proto_common.bzl", "proto_common") load("//bazel/common:proto_info.bzl", "ProtoInfo") -load("//bazel/flags:flags.bzl", "get_flag_value") load("//bazel/private:toolchain_helpers.bzl", "toolchains") DIRECT_DEPS_FLAG_TEMPLATE = ( @@ -177,15 +177,11 @@ def _write_descriptor_set(ctx, proto_info, deps, option_deps, exports, descripto args = ctx.actions.args() - if get_flag_value(ctx, "experimental_proto_descriptor_sets_include_source_info"): + if ctx.attr._experimental_proto_descriptor_sets_include_source_info[BuildSettingInfo].value: args.add("--include_source_info") args.add("--retain_options") - strict_deps = get_flag_value(ctx, "strict_proto_deps") - - # Need to check for off because the starlark flag value doesn't have the sneaky - # mapping from "off" to false. - if strict_deps and strict_deps != "off": + if ctx.attr._strict_proto_deps[BuildSettingInfo].value: if proto_info.direct_sources: # Direct sources can be option imported in addition to `deps`. strict_importable_sources = depset( @@ -226,11 +222,7 @@ def _write_descriptor_set(ctx, proto_info, deps, option_deps, exports, descripto # Set `-option_dependencies_violation_msg=` args.add(ctx.label, format = OPTION_DEPS_FLAG_TEMPLATE) - strict_imports = get_flag_value(ctx, "strict_public_imports") - - # Need to check for off because the starlark flag value doesn't have the sneaky - # mapping from "off" to false. - if strict_imports and strict_imports != "OFF": + if ctx.attr._strict_public_imports[BuildSettingInfo].value: public_import_protos = depset(transitive = [export.check_deps_sources for export in exports]) if not public_import_protos: # This line is necessary to trigger the check. @@ -255,7 +247,7 @@ def _write_descriptor_set(ctx, proto_info, deps, option_deps, exports, descripto mnemonic = "GenProtoDescriptorSet", progress_message = "Generating Descriptor Set proto_library %{label}", proto_compiler = ctx.executable._proto_compiler, - protoc_opts = get_flag_value(ctx, "protocopt"), + protoc_opts = ctx.attr._protocopt[BuildSettingInfo].value, plugin = None, ) @@ -391,25 +383,14 @@ for use with MessageSet. ), # buildifier: disable=attr-license (calling attr.license()) "licenses": attr.license() if hasattr(attr, "license") else attr.string_list(), - "_experimental_proto_descriptor_sets_include_source_info_native": attr.label( - default = "//bazel/private:experimental_proto_descriptor_sets_include_source_info", - ), "_experimental_proto_descriptor_sets_include_source_info": attr.label( - default = "//bazel/flags:experimental_proto_descriptor_sets_include_source_info", - ), - "_strict_proto_deps_native": attr.label( - default = - "//bazel/private:strict_proto_deps", + default = "//bazel/flags:experimental_proto_descriptor_sets_include_source_info_compat", ), "_strict_proto_deps": attr.label( - default = - "//bazel/flags:strict_proto_deps", - ), - "_strict_public_imports_native": attr.label( - default = "//bazel/private:strict_public_imports", + default = "//bazel/flags:strict_proto_deps_compat", ), "_strict_public_imports": attr.label( - default = "//bazel/flags:strict_public_imports", + default = "//bazel/flags:strict_public_imports_compat", ), } | toolchains.if_legacy_toolchain({ "_proto_compiler": attr.label( diff --git a/bazel/private/proto_toolchain_rule.bzl b/bazel/private/proto_toolchain_rule.bzl index 4947d218bbbd4..850fc64a1cc2a 100644 --- a/bazel/private/proto_toolchain_rule.bzl +++ b/bazel/private/proto_toolchain_rule.bzl @@ -7,9 +7,9 @@ # """A Starlark implementation of the proto_toolchain rule.""" +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("//bazel/common:proto_common.bzl", "proto_common") load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo") -load("//bazel/flags:flags.bzl", "get_flag_value") load("//bazel/private:toolchain_helpers.bzl", "toolchains") def _impl(ctx): @@ -25,7 +25,7 @@ def _impl(ctx): plugin = None, runtime = None, proto_compiler = ctx.attr.proto_compiler.files_to_run, - protoc_opts = get_flag_value(ctx, "protocopt"), + protoc_opts = list(ctx.attr._protocopt[BuildSettingInfo].value), progress_message = ctx.attr.progress_message, mnemonic = ctx.attr.mnemonic, **(dict(toolchain_type = toolchains.PROTO_TOOLCHAIN) if proto_common.INCOMPATIBLE_PASS_TOOLCHAIN_TYPE else {}) diff --git a/bazel/private/toolchain_helpers.bzl b/bazel/private/toolchain_helpers.bzl index aa25e51358cb8..c85cb3f2a4cbb 100644 --- a/bazel/private/toolchain_helpers.bzl +++ b/bazel/private/toolchain_helpers.bzl @@ -15,7 +15,6 @@ the migration is finished, the helpers can be removed. """ load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo") -load("//bazel/flags:flags.bzl", "get_flag_value") load("//bazel/private:native.bzl", "native_proto_common") _incompatible_toolchain_resolution = getattr(native_proto_common, "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", False) @@ -27,7 +26,7 @@ def _find_toolchain(ctx, legacy_attr, toolchain_type): fail("No toolchains registered for '%s'." % toolchain_type) return toolchain.proto else: - return get_flag_value(ctx, legacy_attr)[ProtoLangToolchainInfo] + return getattr(ctx.attr, "_" + legacy_attr)[ProtoLangToolchainInfo] def _use_toolchain(toolchain_type): if _incompatible_toolchain_resolution: diff --git a/bazel/tests/bazel_proto_library_tests.bzl b/bazel/tests/bazel_proto_library_tests.bzl index cc2f2de4b9d8f..8e5d58c481bbe 100644 --- a/bazel/tests/bazel_proto_library_tests.bzl +++ b/bazel/tests/bazel_proto_library_tests.bzl @@ -45,6 +45,8 @@ def bazel_proto_library_test_suite(name): _test_proto_library_without_sources, _test_proto_library_with_generated_sources, _test_proto_library_with_mixed_sources, + _test_strict_proto_deps_starlark_override_native, + _test_strict_proto_deps_starlark_off_native_on, ] # Flipping experimental flag in test requires Bazel 8 @@ -649,7 +651,7 @@ def _test_experimental_proto_descriptor_sets_include_source_info(name): name = name, target = name + "_a_proto", impl = _test_experimental_proto_descriptor_sets_include_source_info_impl, - config_settings = {"@@//bazel/flags:experimental_proto_descriptor_sets_include_source_info": True}, + config_settings = {"@@//bazel/flags:experimental_proto_descriptor_sets_include_source_info": "true"}, ) def _test_experimental_proto_descriptor_sets_include_source_info_impl(env, target): @@ -773,3 +775,27 @@ def _test_proto_library_with_mixed_sources_impl(env, target): target.label.package + "/a.proto", target.label.package + "/generated2.proto", ]) + +def _test_strict_proto_deps_starlark_override_native(name): + util.helper_target(proto_library, name = name + "_foo", srcs = ["foo.proto", "bar.proto"]) + + analysis_test( + name = name, + target = name + "_foo", + impl = _test_descriptor_set_output_strict_deps_strict_impl, + config_settings = { + "@@//bazel/flags:strict_proto_deps": "error", + }, + ) + +def _test_strict_proto_deps_starlark_off_native_on(name): + util.helper_target(proto_library, name = name + "_foo", srcs = ["foo.proto"]) + + analysis_test( + name = name, + target = name + "_foo", + impl = _test_descriptor_set_output_strict_deps_disabled_impl, + config_settings = { + "@@//bazel/flags:strict_proto_deps": "off", + }, + )