Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions rust/private/rustfmt.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions test/rustfmt/rustfmt_integration_test_suite.bzl
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions test/rustfmt/srcs/compile_data_generated/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn main() {
println!("compile_data_generated");
}
Loading