Skip to content

Commit d21e160

Browse files
author
Tamas Vajk
committed
Always instrument test targets for coverage
ctx.coverage_instrumented() returns False for test targets by design (Bazel considers test sources not worth instrumenting). But Rust test binaries statically link their dependencies, and the coverage runtime only initializes if the binary itself is compiled with -Cinstrument-coverage. Without this, test binaries produce no profraw files and coverage collection fails with "no input files specified". Always add -Cinstrument-coverage for test crates (crate_info.is_test) when coverage is enabled, while still respecting the instrumentation filter for library targets.
1 parent 26d2d7e commit d21e160

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

rust/private/rustc.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,10 @@ def construct_arguments(
12231223
# Use Bazel's standard instrumentation filter (--instrumentation_filter)
12241224
# so that only targets matching the filter get instrumented, consistent
12251225
# with how coverage works for other languages (Java, C++).
1226-
if toolchain.coverage_supported and ctx.configuration.coverage_enabled and ctx.coverage_instrumented():
1226+
# Test targets always need instrumentation since the coverage runtime
1227+
# only initializes if the binary is compiled with -Cinstrument-coverage,
1228+
# even when the statically linked library code was instrumented.
1229+
if toolchain.coverage_supported and ctx.configuration.coverage_enabled and (crate_info.is_test or ctx.coverage_instrumented()):
12271230
rustc_flags.add("--codegen=instrument-coverage")
12281231

12291232
if toolchain._experimental_link_std_dylib:

0 commit comments

Comments
 (0)