From 1f306787ba29e02e60e0cac0bb0c4c6d940bf388 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Tue, 28 Apr 2026 13:59:25 +0000 Subject: [PATCH 1/4] Fix rustfmt silently skipping symlinked sources When transform_sources() symlinks hand-written .rs files into bazel-out (which happens when any compile_data entry is generated), the symlinked files are no longer is_source=True. This causes _find_rustfmtable_srcs to filter them all out, silently skipping formatting for the entire crate. Fix by recovering original source files from the aspect context's rule attributes when available. Also use crate_info.srcs directly in rustfmt_test to include all sources. --- rust/private/rustfmt.bzl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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. From c6b46258ced7e676bc0e23c51c141124f17c1f32 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Tue, 28 Apr 2026 15:36:22 +0000 Subject: [PATCH 2/4] Add test for rustfmt with generated compile_data Add a test case with an intentionally unformatted source file and generated compile_data. This triggers transform_sources to symlink sources into bazel-out, exercising the fix in _find_rustfmtable_srcs. Without the fix, rustfmt silently passes (sources are skipped). With the fix, rustfmt correctly detects the formatting issue. --- test/rustfmt/rustfmt_failure_tester.sh | 2 ++ .../rustfmt_integration_test_suite.bzl | 25 +++++++++++++++++++ .../srcs/compile_data_generated/lib.rs | 6 +++++ 3 files changed, 33 insertions(+) create mode 100644 test/rustfmt/srcs/compile_data_generated/lib.rs diff --git a/test/rustfmt/rustfmt_failure_tester.sh b/test/rustfmt/rustfmt_failure_tester.sh index e9102bccbb..8c74d7abf3 100755 --- a/test/rustfmt/rustfmt_failure_tester.sh +++ b/test/rustfmt/rustfmt_failure_tester.sh @@ -86,6 +86,7 @@ EOF check_build_result $TEST_OK ${variant}_formatted_2015_test check_build_result $TEST_OK ${variant}_formatted_2018_test check_build_result $TEST_OK ${variant}_generated_test + check_build_result $TEST_FAILED ${variant}_compile_data_generated_test done # Format a specific target @@ -99,6 +100,7 @@ EOF check_build_result $TEST_OK ${variant}_formatted_2015_test check_build_result $TEST_OK ${variant}_formatted_2018_test check_build_result $TEST_OK ${variant}_generated_test + check_build_result $TEST_OK ${variant}_compile_data_generated_test done # Format all targets diff --git a/test/rustfmt/rustfmt_integration_test_suite.bzl b/test/rustfmt/rustfmt_integration_test_suite.bzl index 183c4cd3cf..83703f1225 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,36 @@ 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), + tags = ["manual"], + 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..84851fa667 --- /dev/null +++ b/test/rustfmt/srcs/compile_data_generated/lib.rs @@ -0,0 +1,6 @@ +pub fn main() +{ + println!( + "compile_data_generated" + ); +} From 8dd4c70113be562f6ffdf029232c3c63e0e43018 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Tue, 28 Apr 2026 16:26:24 +0000 Subject: [PATCH 3/4] Fix CI: exclude compile_data_generated_test from test suite The test uses an intentionally unformatted source and is expected to fail. Including it in the test_suite causes CI to run it via //... and report failures. The test is already exercised by the rustfmt_failure_tester.sh script which expects exit code 3. --- test/rustfmt/rustfmt_integration_test_suite.bzl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/rustfmt/rustfmt_integration_test_suite.bzl b/test/rustfmt/rustfmt_integration_test_suite.bzl index 83703f1225..0f66e4aef8 100644 --- a/test/rustfmt/rustfmt_integration_test_suite.bzl +++ b/test/rustfmt/rustfmt_integration_test_suite.bzl @@ -131,7 +131,6 @@ def rustfmt_integration_test_suite(name, **kwargs): "{}_unformatted_2015_test".format(variant), "{}_unformatted_2018_test".format(variant), "{}_generated_test".format(variant), - "{}_compile_data_generated_test".format(variant), ]) native.test_suite( From 34b71b40ffa0c16f7b832c0472168301dedbc778 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Tue, 28 Apr 2026 16:47:44 +0000 Subject: [PATCH 4/4] Use formatted source for compile_data_generated test The unformatted source caused CI failures: - --config=rustfmt aspect caught the formatting issue during build - rustfmt_failure_tester.sh couldn't format it due to norustfmt tag Use a properly formatted source instead. The test still exercises the code path where compile_data has a generated file (triggering transform_sources), verifying that rustfmt correctly finds and checks the sources rather than silently skipping them. --- test/rustfmt/rustfmt_failure_tester.sh | 2 -- test/rustfmt/rustfmt_integration_test_suite.bzl | 2 +- test/rustfmt/srcs/compile_data_generated/lib.rs | 7 ++----- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/test/rustfmt/rustfmt_failure_tester.sh b/test/rustfmt/rustfmt_failure_tester.sh index 8c74d7abf3..e9102bccbb 100755 --- a/test/rustfmt/rustfmt_failure_tester.sh +++ b/test/rustfmt/rustfmt_failure_tester.sh @@ -86,7 +86,6 @@ EOF check_build_result $TEST_OK ${variant}_formatted_2015_test check_build_result $TEST_OK ${variant}_formatted_2018_test check_build_result $TEST_OK ${variant}_generated_test - check_build_result $TEST_FAILED ${variant}_compile_data_generated_test done # Format a specific target @@ -100,7 +99,6 @@ EOF check_build_result $TEST_OK ${variant}_formatted_2015_test check_build_result $TEST_OK ${variant}_formatted_2018_test check_build_result $TEST_OK ${variant}_generated_test - check_build_result $TEST_OK ${variant}_compile_data_generated_test done # Format all targets diff --git a/test/rustfmt/rustfmt_integration_test_suite.bzl b/test/rustfmt/rustfmt_integration_test_suite.bzl index 0f66e4aef8..e4464047fb 100644 --- a/test/rustfmt/rustfmt_integration_test_suite.bzl +++ b/test/rustfmt/rustfmt_integration_test_suite.bzl @@ -121,7 +121,6 @@ def rustfmt_integration_test_suite(name, **kwargs): rustfmt_test( name = "{}_compile_data_generated_test".format(variant), - tags = ["manual"], targets = [":{}_compile_data_generated".format(variant)], ) @@ -131,6 +130,7 @@ def rustfmt_integration_test_suite(name, **kwargs): "{}_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 index 84851fa667..7b78b16814 100644 --- a/test/rustfmt/srcs/compile_data_generated/lib.rs +++ b/test/rustfmt/srcs/compile_data_generated/lib.rs @@ -1,6 +1,3 @@ -pub fn main() -{ - println!( - "compile_data_generated" - ); +pub fn main() { + println!("compile_data_generated"); }