Skip to content

Add RUST_COVERAGE_IGNORE_REGEX for configurable coverage filtering#3999

Open
tamasvajk wants to merge 1 commit intobazelbuild:mainfrom
tamasvajk:coverage-configurable-ignore
Open

Add RUST_COVERAGE_IGNORE_REGEX for configurable coverage filtering#3999
tamasvajk wants to merge 1 commit intobazelbuild:mainfrom
tamasvajk:coverage-configurable-ignore

Conversation

@tamasvajk
Copy link
Copy Markdown
Contributor

Summary

Add RUST_COVERAGE_IGNORE_REGEX environment variable for configurable llvm-cov export filtering.

Problem

The collect_coverage.rs script hardcodes two -ignore-filename-regex patterns (.*external/.+ and /tmp/.+) passed to llvm-cov export. Projects often need additional filters — for example, to exclude vendored C/C++ code or generated files — but there's no way to add them without patching the script.

Fix

Read an optional RUST_COVERAGE_IGNORE_REGEX environment variable: a comma-separated list of additional regex patterns passed as -ignore-filename-regex arguments to llvm-cov. The existing hardcoded patterns are preserved as defaults.

Users can set this via --test_env in .bazelrc:

coverage --test_env=RUST_COVERAGE_IGNORE_REGEX=.*vendor/.*,^/rustc/

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

The llvm-cov export step hardcodes two ignore patterns (external/,
/tmp/). Projects often need additional filters for third-party C
code, generated files, or stdlib sources, but editing the coverage
script is not practical.

Add support for the RUST_COVERAGE_IGNORE_REGEX environment variable:
a comma-separated list of additional -ignore-filename-regex patterns
passed to llvm-cov. Users can set this via --test_env in .bazelrc:

  coverage --test_env=RUST_COVERAGE_IGNORE_REGEX=.*third_party/.*,^bazel-out/
@tamasvajk tamasvajk force-pushed the coverage-configurable-ignore branch from be1b0e8 to 979f34d Compare April 28, 2026 14:23
Copy link
Copy Markdown
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Is there any prior-art for this in other rules? I'm wondering if a custom environment variable is really needed

@tamasvajk
Copy link
Copy Markdown
Contributor Author

Thanks! Is there any prior-art for this in other rules? I'm wondering if a custom environment variable is really needed

I couldn't find direct prior art in other Bazel rule sets — C++ coverage in Bazel uses collect_coverage.sh with its own hardcoded logic, and rules_go doesn't use llvm-cov.

The env var approach was chosen because the filtering happens at runtime (in the collect_coverage binary, not during analysis), so --test_env is the standard Bazel mechanism for passing runtime configuration to tests without invalidating the analysis cache.

An alternative would be to add a build setting like --@rules_rust//rust/settings:coverage_ignore_regex and plumb it through to the test action's environment. That would be more discoverable and consistent with how rules_rust exposes other configuration, though it would require more plumbing and invalidate the analysis cache when changed. Happy to rework it that way if you'd prefer.

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.

2 participants