Respect --instrumentation_filter for Rust coverage#3998
Open
tamasvajk wants to merge 6 commits intobazelbuild:mainfrom
Open
Respect --instrumentation_filter for Rust coverage#3998tamasvajk wants to merge 6 commits intobazelbuild:mainfrom
--instrumentation_filter for Rust coverage#3998tamasvajk wants to merge 6 commits intobazelbuild:mainfrom
Conversation
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.
882a206 to
a9530b0
Compare
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
a9530b0 to
26d2d7e
Compare
added 4 commits
April 28, 2026 18:19
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.
8065153 to
077019a
Compare
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.collect_coverage.rs: Handle zero.profrawfiles gracefully. When a test target is not instrumented (because it doesn't match the filter), no profraw files are produced. Previously this causedllvm-profdata mergeto fail with "no input files specified". Now it writes an empty coverage file and exits successfully..bazelrc: Addcoverage --instrumentation_filter=^//so that all workspace targets are instrumented by default..bazelci/presubmit.yml: Add--instrumentation_filter=^//to all CI coverage tasks.Usage
The default
--instrumentation_filter=^//instruments all workspace targets. Projects with vendored dependencies should narrow this: