diff --git a/crate_universe/BUILD.bazel b/crate_universe/BUILD.bazel index 782b0005be..b6da14961a 100644 --- a/crate_universe/BUILD.bazel +++ b/crate_universe/BUILD.bazel @@ -93,6 +93,7 @@ alias( rust_test( name = "unit_test", aliases = aliases(), + compile_data = glob(["test_data/**"]), crate = ":cargo_bazel", data = glob(["test_data/**"]) + [ "//crate_universe/test_data/serialized_configs", diff --git a/examples/cargo_manifest_dir/external_crate/BUILD.bazel b/examples/cargo_manifest_dir/external_crate/BUILD.bazel index ca5ab63a1a..db59f9b443 100644 --- a/examples/cargo_manifest_dir/external_crate/BUILD.bazel +++ b/examples/cargo_manifest_dir/external_crate/BUILD.bazel @@ -3,6 +3,6 @@ load("@rules_rust//rust:defs.bzl", "rust_library") rust_library( name = "external_crate", srcs = ["src/lib.rs"], - data = ["include/included_file.rs.inc"], + compile_data = ["include/included_file.rs.inc"], visibility = ["//visibility:public"], ) diff --git a/examples/crate_universe/WORKSPACE.bazel b/examples/crate_universe/WORKSPACE.bazel index fecc4bb075..ef3d017e27 100644 --- a/examples/crate_universe/WORKSPACE.bazel +++ b/examples/crate_universe/WORKSPACE.bazel @@ -539,7 +539,7 @@ rust_binary( name = "cxxbridge-cmd", srcs = glob(["src/**/*.rs"]), aliases = aliases(), - data = [ + compile_data = [ "src/gen/include/cxx.h", ], edition = "2021", diff --git a/examples/flag_locations/BUILD.bazel b/examples/flag_locations/BUILD.bazel index fdc92ed42b..a7abdc55eb 100644 --- a/examples/flag_locations/BUILD.bazel +++ b/examples/flag_locations/BUILD.bazel @@ -12,7 +12,7 @@ rust_test( srcs = [ "main.rs", ], - data = [":flag_generator"], + compile_data = [":flag_generator"], edition = "2018", rustc_flags = [ "@$(location :flag_generator)", diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index d2a810f099..d23da7dce5 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -715,8 +715,14 @@ def collect_inputs( if build_info.flags: build_info_inputs.append(build_info.flags) + # The old default behavior was to include data files at compile time. + # This flag controls whether to include data files in compile_data. + data_included_in_inputs = [] + if not toolchain._incompatible_do_not_include_data_in_compile_data: + data_included_in_inputs = getattr(files, "data", []) + nolinkstamp_compile_inputs = depset( - getattr(files, "data", []) + + data_included_in_inputs + build_info_inputs + ([toolchain.target_json] if toolchain.target_json else []) + ([] if linker_script == None else [linker_script]), diff --git a/rust/settings/BUILD.bazel b/rust/settings/BUILD.bazel index f063fb2508..c127e0f2ab 100644 --- a/rust/settings/BUILD.bazel +++ b/rust/settings/BUILD.bazel @@ -253,3 +253,11 @@ per_crate_rustc_flag( build_setting_default = [], visibility = ["//visibility:public"], ) + +# A flag to control whether to include data files in compile_data. +incompatible_flag( + name = "incompatible_do_not_include_data_in_compile_data", + build_setting_default = True, + issue = "https://github.com/bazelbuild/rules_rust/issues/2977", + visibility = ["//visibility:public"], +) diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index f252e22042..ce0bb5d457 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -699,6 +699,7 @@ def _rust_toolchain_impl(ctx): _experimental_use_coverage_metadata_files = ctx.attr._experimental_use_coverage_metadata_files[BuildSettingInfo].value, _incompatible_change_rust_test_compilation_output_directory = ctx.attr._incompatible_change_rust_test_compilation_output_directory[IncompatibleFlagInfo].enabled, _toolchain_generated_sysroot = ctx.attr._toolchain_generated_sysroot[BuildSettingInfo].value, + _incompatible_do_not_include_data_in_compile_data = ctx.attr._incompatible_do_not_include_data_in_compile_data[IncompatibleFlagInfo].enabled, _no_std = no_std, ) return [ @@ -886,6 +887,10 @@ rust_toolchain = rule( "_incompatible_change_rust_test_compilation_output_directory": attr.label( default = Label("//rust/settings:incompatible_change_rust_test_compilation_output_directory"), ), + "_incompatible_do_not_include_data_in_compile_data": attr.label( + default = Label("//rust/settings:incompatible_do_not_include_data_in_compile_data"), + doc = "Label to a boolean build setting that controls whether to include data files in compile_data.", + ), "_no_std": attr.label( default = Label("//:no_std"), ), diff --git a/test/build_env/BUILD.bazel b/test/build_env/BUILD.bazel index 12fd5fa805..ffd49b1234 100644 --- a/test/build_env/BUILD.bazel +++ b/test/build_env/BUILD.bazel @@ -9,7 +9,7 @@ package(default_visibility = ["//visibility:public"]) rust_test( name = "conflicting_deps_test", srcs = ["tests/manifest_dir.rs"], - data = ["src/manifest_dir_file.txt"], + compile_data = ["src/manifest_dir_file.txt"], edition = "2018", )