[WIP] Respect --instrumentation_filter for Rust coverage#3998
Closed
tamasvajk wants to merge 10 commits intobazelbuild:mainfrom
Closed
[WIP] Respect --instrumentation_filter for Rust coverage#3998tamasvajk wants to merge 10 commits intobazelbuild:mainfrom
--instrumentation_filter for Rust coverage#3998tamasvajk wants to merge 10 commits intobazelbuild:mainfrom
Conversation
8065153 to
077019a
Compare
UebelAndre
requested changes
May 3, 2026
Collaborator
UebelAndre
left a comment
There was a problem hiding this comment.
Thanks! Just had one question.
| - echo "coverage --experimental_split_coverage_postprocessing" >> user.bazelrc | ||
| - echo "build --//rust/settings:experimental_use_coverage_metadata_files" >> user.bazelrc | ||
| coverage_flags: &coverage_flags | ||
| - "--instrumentation_filter=^//" |
Collaborator
There was a problem hiding this comment.
Isn't this the default? If so is it necessary?
Contributor
Author
There was a problem hiding this comment.
https://bazel.build/reference/command-line-reference#flag--instrumentation_filter shows the default is -/javatests[/:],-/test/java[/:]
added 6 commits
May 4, 2026 08:57
Previously, all Rust targets were instrumented with -Cinstrument-coverage when running `bazel coverage`, regardless of --instrumentation_filter. This differs from how coverage works for Java and C++ targets, where only targets matching the filter are instrumented. Add ctx.coverage_instrumented() check so that Rust coverage respects the same filter, reducing unnecessary recompilation and keeping coverage reports focused on the code under test.
Without an explicit --instrumentation_filter, Bazel's default filter may not match workspace targets, causing ctx.coverage_instrumented() to return False and producing empty Rust coverage reports. Add coverage --instrumentation_filter=^// to both .bazelrc and all CI coverage tasks. Projects with vendored dependencies should narrow this, e.g.: coverage --instrumentation_filter=^//,-^//third_party
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.
This reverts commit d21e160.
When a test target is not instrumented (e.g. due to --instrumentation_filter), no .profraw files are produced. Previously llvm-profdata merge would fail with "no input files specified". Write an empty coverage file and exit successfully instead, so that uninstrumented test targets don't break coverage collection.
The .bazelrc is inherited by sub-workspace CI tasks (cc_common_link, bzlmod_repo_mapping, sys, pyo3) causing build/test failures. The flag is already passed via coverage_flags in presubmit.yml for coverage tasks.
077019a to
1229613
Compare
The --instrumentation_filter=^// flag doesn't work correctly with Bazel 7.4.1 for Rust coverage collection. Remove it from the ubuntu1804 tasks so they use the default filter, which works.
5211230 to
bf827f9
Compare
added 3 commits
May 4, 2026 10:08
The empty profraw handling causes coverage to silently produce empty data on Bazel 7.4.1 instead of going through the normal coverage pipeline. This isn't needed since bazel coverage already handles uninstrumented test targets gracefully.
This reverts commit 6306782.
The coverage collection wrapper does not reliably find profraw files on Bazel 7.x, causing empty coverage reports. Remove coverage_targets and post_shell_commands from ubuntu1804 tasks.
--instrumentation_filter for Rust coverage--instrumentation_filter for Rust coverage
Contributor
Author
|
Closing this in favor of #4013. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Respect
--instrumentation_filterwhen instrumenting Rust targets for coverage, consistent with Bazel's recommended approach for rules.Problem
Previously, all Rust targets were instrumented with
-Cinstrument-coveragewhen runningbazel coverage, regardless of--instrumentation_filter. This causes:Changes
rustc.bzl: Addctx.coverage_instrumented()check, the standard Bazel API for respecting--instrumentation_filter. Only targets matching the filter get-Cinstrument-coverage..bazelci/presubmit.yml: Add--instrumentation_filter=^//to CI coverage tasks so that all workspace targets are instrumented (excluding external dependencies).Usage
By default, Bazel's
--instrumentation_filteris-/javatests[/:],-/test/java[/:], which instruments most targets including external dependencies. To restrict coverage to only workspace targets (recommended for Rust projects with vendored deps):To further exclude vendored code: