Skip to content

Commit 34d9c2e

Browse files
author
Tamas Vajk
committed
Respect --instrumentation_filter for Rust coverage
Add ctx.coverage_instrumented() check so that only targets matching --instrumentation_filter get -Cinstrument-coverage, consistent with Bazel's recommended approach for rules. For rust_test targets with a crate attribute, also check if the underlying crate should be instrumented. Rust compiles the crate sources directly into the test binary, so the test must be built with -Cinstrument-coverage for the crate's code to produce coverage. Add --instrumentation_filter=^// and --instrument_test_targets to CI coverage tasks so all workspace targets (including tests) are instrumented while excluding external dependencies.
1 parent c70eb85 commit 34d9c2e

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

.bazelci/presubmit.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,20 @@ split_coverage_postprocessing_shell_commands: &split_coverage_postprocessing_she
7474
- echo "coverage --experimental_fetch_all_coverage_outputs" >> user.bazelrc
7575
- echo "coverage --experimental_split_coverage_postprocessing" >> user.bazelrc
7676
- echo "build --//rust/settings:experimental_use_coverage_metadata_files" >> user.bazelrc
77+
coverage_flags: &coverage_flags
78+
- "--instrumentation_filter=^//"
79+
- "--instrument_test_targets"
7780
rbe_coverage_flags: &rbe_coverage_flags
7881
# https://github.com/bazelbuild/bazel/issues/20578
7982
- "--strategy=CoverageReport=local"
83+
- "--instrumentation_filter=^//"
84+
- "--instrument_test_targets"
8085
tasks:
8186
ubuntu2204:
8287
build_targets: *default_linux_targets
8388
test_targets: *default_linux_targets
8489
coverage_targets: *default_linux_targets
90+
coverage_flags: *coverage_flags
8591
post_shell_commands: *coverage_validation_post_shell_commands
8692
run_targets:
8793
- //test:query_test_binary
@@ -99,6 +105,7 @@ tasks:
99105
build_targets: *default_macos_targets
100106
test_targets: *default_macos_targets
101107
coverage_targets: *default_macos_targets
108+
coverage_flags: *coverage_flags
102109
post_shell_commands: *coverage_validation_post_shell_commands
103110
windows:
104111
build_targets: *default_windows_targets
@@ -116,12 +123,14 @@ tasks:
116123
platform: ubuntu2204
117124
shell_commands: *split_coverage_postprocessing_shell_commands
118125
coverage_targets: *default_linux_targets
126+
coverage_flags: *coverage_flags
119127
post_shell_commands: *coverage_validation_post_shell_commands
120128
macos_split_coverage_postprocessing:
121129
name: Split Coverage Postprocessing
122130
platform: macos_arm64
123131
shell_commands: *split_coverage_postprocessing_shell_commands
124132
coverage_targets: *default_macos_targets
133+
coverage_flags: *coverage_flags
125134
post_shell_commands: *coverage_validation_post_shell_commands
126135
ubuntu2204_opt:
127136
name: Opt Mode
@@ -157,6 +166,7 @@ tasks:
157166
build_targets: *default_linux_targets
158167
test_targets: *default_linux_targets
159168
coverage_targets: *default_linux_targets
169+
coverage_flags: *coverage_flags
160170
post_shell_commands: *coverage_validation_post_shell_commands
161171
rbe_ubuntu2204_with_aspects:
162172
name: With Aspects
@@ -191,6 +201,7 @@ tasks:
191201
build_targets: *default_macos_targets
192202
test_targets: *default_macos_targets
193203
coverage_targets: *default_macos_targets
204+
coverage_flags: *coverage_flags
194205
post_shell_commands: *coverage_validation_post_shell_commands
195206
macos_rolling_with_aspects:
196207
name: "Macos Rolling Bazel Version With Aspects"
@@ -199,6 +210,7 @@ tasks:
199210
build_targets: *default_macos_targets
200211
test_targets: *default_macos_targets
201212
coverage_targets: *default_macos_targets
213+
coverage_flags: *coverage_flags
202214
post_shell_commands: *coverage_validation_post_shell_commands
203215
soft_fail: yes
204216
bazel: "rolling"
@@ -358,6 +370,7 @@ tasks:
358370
build_targets: *default_linux_targets
359371
test_targets: *default_linux_targets
360372
coverage_targets: *default_linux_targets
373+
coverage_flags: *coverage_flags
361374
post_shell_commands: *coverage_validation_post_shell_commands
362375
linux_docs:
363376
name: Docs

rust/private/rustc.bzl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,8 +1219,17 @@ def construct_arguments(
12191219
rustc_flags.add("--extern")
12201220
rustc_flags.add("proc_macro")
12211221

1222-
if toolchain.coverage_supported and ctx.configuration.coverage_enabled:
1223-
# https://doc.rust-lang.org/rustc/instrument-coverage.html
1222+
# Use Bazel's standard instrumentation filter (--instrumentation_filter)
1223+
# so that only targets matching the filter get instrumented, consistent
1224+
# with how coverage works for other languages (Java, C++).
1225+
# For rust_test targets with a `crate` attribute, also check if the
1226+
# underlying crate should be instrumented. Rust compiles the crate
1227+
# sources directly into the test binary, so the test must be built
1228+
# with -Cinstrument-coverage for the crate's code to produce coverage.
1229+
_coverage_instrumented = ctx.coverage_instrumented()
1230+
if not _coverage_instrumented and crate_info.is_test and hasattr(ctx.attr, "crate") and ctx.attr.crate:
1231+
_coverage_instrumented = ctx.coverage_instrumented(ctx.attr.crate)
1232+
if toolchain.coverage_supported and ctx.configuration.coverage_enabled and _coverage_instrumented:
12241233
rustc_flags.add("--codegen=instrument-coverage")
12251234

12261235
if toolchain._experimental_link_std_dylib:

0 commit comments

Comments
 (0)