diff --git a/rust/private/rustfmt.bzl b/rust/private/rustfmt.bzl index 3669ecdf0e..d3a1d0ffba 100644 --- a/rust/private/rustfmt.bzl +++ b/rust/private/rustfmt.bzl @@ -47,7 +47,19 @@ def _find_rustfmtable_srcs(crate_info, aspect_ctx = None): if tag.replace("-", "_").lower() in ignore_tags: return [] - # Filter out any generated files + # Filter out generated files, but recover hand-written sources that were + # symlinked into bazel-out by transform_sources() (which happens when any + # compile_data entry is generated). Return the original source File objects + # so that exec_path and short_path remain correct for manifests and runfiles. + if aspect_ctx and hasattr(aspect_ctx.rule.attr, "srcs"): + original_srcs = [] + for target in aspect_ctx.rule.attr.srcs: + for f in target.files.to_list(): + if f.is_source: + original_srcs.append(f) + if original_srcs: + return original_srcs + srcs = [src for src in crate_info.srcs.to_list() if src.is_source] return srcs @@ -219,7 +231,7 @@ def _rustfmt_test_impl(ctx): ) crate_infos = [_get_rustfmt_ready_crate_info(target) for target in ctx.attr.targets] - srcs = [depset(_find_rustfmtable_srcs(crate_info)) for crate_info in crate_infos if crate_info] + srcs = [crate_info.srcs for crate_info in crate_infos if crate_info] # Some targets may be included in tests but tagged as "no-format". In this # case, there will be no manifest. diff --git a/test/rustfmt/rustfmt_integration_test_suite.bzl b/test/rustfmt/rustfmt_integration_test_suite.bzl index 183c4cd3cf..e4464047fb 100644 --- a/test/rustfmt/rustfmt_integration_test_suite.bzl +++ b/test/rustfmt/rustfmt_integration_test_suite.bzl @@ -1,5 +1,6 @@ """Test definitions for rustfmt test rules""" +load("@bazel_skylib//rules:write_file.bzl", "write_file") load( "@rules_rust//rust:defs.bzl", "rust_binary", @@ -101,12 +102,35 @@ def rustfmt_integration_test_suite(name, **kwargs): targets = [":{}_generated".format(variant)], ) + # + # Test targets with generated compile_data (triggers transform_sources + # to symlink hand-written sources into bazel-out) + # + write_file( + name = "{}_compile_data_gen".format(variant), + out = "{}_generated_data.txt".format(variant), + content = ["generated"], + ) + + rust_rule( + name = "{}_compile_data_generated".format(variant), + srcs = ["srcs/compile_data_generated/lib.rs"], + compile_data = [":{}_compile_data_gen".format(variant)], + edition = "2021", + ) + + rustfmt_test( + name = "{}_compile_data_generated_test".format(variant), + targets = [":{}_compile_data_generated".format(variant)], + ) + tests.extend([ "{}_formatted_2015_test".format(variant), "{}_formatted_2018_test".format(variant), "{}_unformatted_2015_test".format(variant), "{}_unformatted_2018_test".format(variant), "{}_generated_test".format(variant), + "{}_compile_data_generated_test".format(variant), ]) native.test_suite( diff --git a/test/rustfmt/srcs/compile_data_generated/lib.rs b/test/rustfmt/srcs/compile_data_generated/lib.rs new file mode 100644 index 0000000000..7b78b16814 --- /dev/null +++ b/test/rustfmt/srcs/compile_data_generated/lib.rs @@ -0,0 +1,3 @@ +pub fn main() { + println!("compile_data_generated"); +}