Skip to content

Respect --instrumentation_filter for Rust coverage#4013

Open
tamasvajk wants to merge 2 commits intobazelbuild:mainfrom
tamasvajk:coverage-instrumentation-filter-v2
Open

Respect --instrumentation_filter for Rust coverage#4013
tamasvajk wants to merge 2 commits intobazelbuild:mainfrom
tamasvajk:coverage-instrumentation-filter-v2

Conversation

@tamasvajk
Copy link
Copy Markdown
Contributor

@tamasvajk tamasvajk commented May 4, 2026

Summary

Respect --instrumentation_filter when instrumenting Rust targets for coverage, consistent with Bazel's recommended approach for rules.

Problem

Previously, all Rust targets were instrumented with -Cinstrument-coverage when running bazel coverage, regardless of --instrumentation_filter. This causes:

  • Third-party and vendored crate dependencies to be unnecessarily recompiled with coverage instrumentation, slowing down builds
  • Coverage reports to include noise from code the user doesn't care about

Changes

  1. rustc.bzl: Add ctx.coverage_instrumented() check, the standard Bazel API for respecting --instrumentation_filter. Only targets matching the filter get -Cinstrument-coverage. For rust_test targets with a crate attribute, also check whether 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 data.

  2. coverage.md: Add documentation for coverage usage, explaining --instrumentation_filter, the rust_test with crate attribute behavior, and --instrument_test_targets.

  3. .bazelci/presubmit.yml: Add --instrumentation_filter=^// and --instrument_test_targets to CI coverage tasks so that all workspace targets (including tests) are instrumented while excluding external dependencies.

Note: This PR was largely AI-generated using Claude Code, with human review and guidance throughout.

@tamasvajk tamasvajk force-pushed the coverage-instrumentation-filter-v2 branch 4 times, most recently from 34d9c2e to 5d59fe5 Compare May 4, 2026 14:04
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.
@tamasvajk tamasvajk force-pushed the coverage-instrumentation-filter-v2 branch from 5d59fe5 to f3693b6 Compare May 5, 2026 06:38
Remove --instrument_test_targets from recommended settings since it is
not needed for the common case of rust_test with a crate attribute.
Document the inconsistency where #[cfg(test)] code gets instrumented
even without the flag.
@tamasvajk tamasvajk force-pushed the coverage-instrumentation-filter-v2 branch from 80de598 to 76c0787 Compare May 5, 2026 07:29
@tamasvajk tamasvajk changed the title [WIP] Respect --instrumentation_filter for Rust coverage Respect --instrumentation_filter for Rust coverage May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant