From 5f4da167d9a7278491a618f80b471a34aca66bf8 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Thu, 12 Mar 2026 12:50:00 -0700 Subject: [PATCH 1/2] Fix data on cc_import Previously this attribute was accepted by did nothing. Fixes https://github.com/bazelbuild/bazel/issues/18844 --- cc/private/rules_impl/cc_import.bzl | 37 ++++++++++++++++++- tests/cc/common/BUILD | 3 ++ .../cc_import_configured_target_tests.bzl | 33 +++++++++++++++++ tests/cc/common/data_file.txt | 1 + tests/cc/common/header.h | 1 + 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/cc/common/cc_import_configured_target_tests.bzl create mode 100644 tests/cc/common/data_file.txt create mode 100644 tests/cc/common/header.h diff --git a/cc/private/rules_impl/cc_import.bzl b/cc/private/rules_impl/cc_import.bzl index bf0431f62..b27403c6e 100644 --- a/cc/private/rules_impl/cc_import.bzl +++ b/cc/private/rules_impl/cc_import.bzl @@ -187,7 +187,37 @@ def _cc_import_impl(ctx): cc_infos.append(dep[CcInfo]) merged_cc_info = cc_common.merge_cc_infos(direct_cc_infos = [this_cc_info], cc_infos = cc_infos) - return [merged_cc_info] + runfiles_list = [] + for data_dep in ctx.attr.data: + if data_dep[DefaultInfo].data_runfiles.files: + runfiles_list.append(data_dep[DefaultInfo].data_runfiles) + else: + # This branch ensures interop with custom Starlark rules following + # https://bazel.build/extending/rules#runfiles_features_to_avoid + runfiles_list.append(ctx.runfiles(transitive_files = data_dep[DefaultInfo].files)) + runfiles_list.append(data_dep[DefaultInfo].default_runfiles) + + for dep in ctx.attr.deps: + runfiles_list.append(dep[DefaultInfo].default_runfiles) + + runfiles = ctx.runfiles().merge_all(runfiles_list) + + if linking_context: + default_runfiles = ctx.runfiles(files = cc_helper.get_dynamic_libraries_for_runtime(linking_context, True)) + default_runfiles = runfiles.merge(default_runfiles) + data_runfiles = ctx.runfiles(files = cc_helper.get_dynamic_libraries_for_runtime(linking_context, False)) + data_runfiles = runfiles.merge(data_runfiles) + else: + default_runfiles = runfiles + data_runfiles = runfiles + + return [ + merged_cc_info, + DefaultInfo( + default_runfiles = default_runfiles, + data_runfiles = data_runfiles, + ), + ] cc_import = rule( implementation = _cc_import_impl, @@ -443,6 +473,11 @@ most build rules."""), "data": attr.label_list( allow_files = True, flags = ["SKIP_CONSTRAINTS_OVERRIDE"], + doc = """ +The list of files needed by this library at runtime. +See general comments about data +at Typical attributes defined by +most build rules.""", ), "defines": attr.string_list(doc = """ List of defines to add to the compile line of this and all dependent targets. diff --git a/tests/cc/common/BUILD b/tests/cc/common/BUILD index 91acc88d6..c72740943 100644 --- a/tests/cc/common/BUILD +++ b/tests/cc/common/BUILD @@ -1,3 +1,6 @@ load(":cc_binary_configured_target_tests.bzl", "cc_binary_configured_target_tests") +load(":cc_import_configured_target_tests.bzl", "cc_import_configured_target_tests") cc_binary_configured_target_tests(name = "cc_binary_configured_target_tests") + +cc_import_configured_target_tests(name = "cc_import_configured_target_tests") diff --git a/tests/cc/common/cc_import_configured_target_tests.bzl b/tests/cc/common/cc_import_configured_target_tests.bzl new file mode 100644 index 000000000..60807458d --- /dev/null +++ b/tests/cc/common/cc_import_configured_target_tests.bzl @@ -0,0 +1,33 @@ +"""Tests for cc_import.""" + +load("@rules_testing//lib:truth.bzl", "matching") +load("@rules_testing//lib:util.bzl", "util") +load("//cc:cc_import.bzl", "cc_import") +load("//tests/cc/testutil:cc_analysis_test.bzl", "cc_analysis_test", "cc_test_suite") + +def _test_data_in_runfiles(name, **kwargs): + util.helper_target( + cc_import, + name = name + "/import_with_data", + hdrs = ["header.h"], + data = ["data_file.txt"], + ) + cc_analysis_test( + name = name, + impl = _test_data_in_runfiles_impl, + target = name + "/import_with_data", + **kwargs + ) + +def _test_data_in_runfiles_impl(env, target): + target = env.expect.that_target(target) + target.runfiles().contains_predicate(matching.str_endswith("/data_file.txt")) + target.data_runfiles().contains_predicate(matching.str_endswith("/data_file.txt")) + +def cc_import_configured_target_tests(name): + cc_test_suite( + name = name, + tests = [ + _test_data_in_runfiles, + ], + ) diff --git a/tests/cc/common/data_file.txt b/tests/cc/common/data_file.txt new file mode 100644 index 000000000..082b3465b --- /dev/null +++ b/tests/cc/common/data_file.txt @@ -0,0 +1 @@ +test data diff --git a/tests/cc/common/header.h b/tests/cc/common/header.h new file mode 100644 index 000000000..d55bc773b --- /dev/null +++ b/tests/cc/common/header.h @@ -0,0 +1 @@ +// Empty header for testing. From 11b3fe99756b04d6be092ed22d3d883a9e03151e Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Thu, 12 Mar 2026 13:06:07 -0700 Subject: [PATCH 2/2] gate on bazel version --- tests/cc/common/cc_import_configured_target_tests.bzl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/cc/common/cc_import_configured_target_tests.bzl b/tests/cc/common/cc_import_configured_target_tests.bzl index 60807458d..34747f75a 100644 --- a/tests/cc/common/cc_import_configured_target_tests.bzl +++ b/tests/cc/common/cc_import_configured_target_tests.bzl @@ -1,10 +1,13 @@ """Tests for cc_import.""" +load("@bazel_features//private:util.bzl", _bazel_version_ge = "ge") load("@rules_testing//lib:truth.bzl", "matching") load("@rules_testing//lib:util.bzl", "util") load("//cc:cc_import.bzl", "cc_import") load("//tests/cc/testutil:cc_analysis_test.bzl", "cc_analysis_test", "cc_test_suite") +_use_rules_cc_impls = _bazel_version_ge("9.0.0-pre.20250911") + def _test_data_in_runfiles(name, **kwargs): util.helper_target( cc_import, @@ -29,5 +32,5 @@ def cc_import_configured_target_tests(name): name = name, tests = [ _test_data_in_runfiles, - ], + ] if _use_rules_cc_impls else [], )