Skip to content

Provide --experimental_instrumentation_filter_fragment cli option #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arvi18
Copy link

@arvi18 arvi18 commented Apr 28, 2025

This option is similar to --instrumentation_filter option. The difference is this option can be used multiple times to accumulate its values. If this option is provided --instrumentation_filter has no effect.

Multiple uses of --instrumentation_filter are not accumulated. This makes it difficult to have short hand configuration flags for a combination of coverage flags in a bazelrc file. e.g.,

build:covA  --instrumentation_filter=afoo/abar,-afoo/abaz
build:covB  --instrumentation_filter=bfoo/bbar,-bfoo/bbaz
build:covC  --instrumentation_filter=cfoo/cbar,-cfoo/cbaz
\# To combine the flags concatenate the respective filter strings
build:covAB  --instrumentation_filter=afoo/abar,-afoo/abaz,bfoo/bbar,-bfoo/bbaz
build:covAC  --instrumentation_filter=afoo/abar,-afoo/abaz,cfoo/cbar,-cfoo/cbaz
build:covABC --instrumentation_filter=afoo/abar,-afoo/abaz,bfoo/bbar,-bfoo/bbaz,cfoo/cbar,-cfoo/cbaz

With a large set of flags and their respective large filter strings it is uneasy to combine the short hand configuration flags because the respective filter strings need to be concatenated into a single string. This is uneasy to maintain because filter strings are repeated in the combinations.

--experimental_instrumentation_filter_fragment simplifies combining multiple shorthand configuration flags because its multiple uses are accumulated. e.g.,

build:covA  --experimental_instrumentation_filter_fragment=afoo/abar,-afoo/abaz
build:covB  --experimental_instrumentation_filter_fragment=bfoo/bbar,-bfoo/bbaz
build:covC  --experimental_instrumentation_filter_fragment=cfoo/cbar,-cfoo/cbaz
build:covAB --config covA --config covB
build:covAC --config covA --config covC
build:covABC --config covA --config covB --config covC

Add tests for --experimental_instrumentation_filter_fragment cli to verify that its multiple uses are accumulated and --instrumentation_filter has no effect when --experimental_instrumentation_filter_fragment is used.

bazelbuild#22959 is the upstream issue.

Testing Done:

$ bazelisk  test src/test/java/com/google/devtools/build/lib/starlark:all
INFO: Analyzed 4 targets (0 packages loaded, 0 targets configured).
INFO: Found 1 target and 3 test targets...
INFO: Elapsed time: 242.197s, Critical Path: 216.72s
INFO: 27 processes: 1 internal, 25 darwin-sandbox, 1 worker.
INFO: Build completed successfully, 27 total actions
//src/test/java/com/google/devtools/build/lib/starlark:BindTest (cached) PASSED in 7.5s
//src/test/java/com/google/devtools/build/lib/starlark:StarlarkRuleClassFunctionsTest (cached) PASSED in 78.0s
  Stats over 5 runs: max = 78.0s, min = 59.8s, avg = 73.9s, dev = 7.1s
//src/test/java/com/google/devtools/build/lib/starlark:StarlarkTests     PASSED in 121.1s
  Stats over 25 runs: max = 121.1s, min = 24.8s, avg = 88.6s, dev = 32.1s

Executed 1 out of 3 tests: 3 tests pass.
There were tests whose specified size is too big. Use the --test_verbose_timeout_warnings command line option to see which ones these are.

Summary by CodeRabbit

  • New Features
    • Introduced a new command-line option to allow advanced filtering for coverage instrumentation using regular expressions.
  • Bug Fixes
    • Ensured that the new filter option takes precedence over the legacy filter when both are specified.
  • Tests
    • Added comprehensive tests to verify correct behavior of coverage instrumentation with the new filtering option enabled.

This option is similar to `--instrumentation_filter` option. The difference
is this option can be used multiple times to accumulate its values. If this
option is provided `--instrumentation_filter` has no effect.

Multiple uses of `--instrumentation_filter` are not accumulated. This makes
it difficult to have short hand configuration flags for a combination of
coverage flags in a bazelrc file. e.g.,
```
build:covA  --instrumentation_filter=afoo/abar,-afoo/abaz
build:covB  --instrumentation_filter=bfoo/bbar,-bfoo/bbaz
build:covC  --instrumentation_filter=cfoo/cbar,-cfoo/cbaz
\# To combine the flags concatenate the respective filter strings
build:covAB  --instrumentation_filter=afoo/abar,-afoo/abaz,bfoo/bbar,-bfoo/bbaz
build:covAC  --instrumentation_filter=afoo/abar,-afoo/abaz,cfoo/cbar,-cfoo/cbaz
build:covABC --instrumentation_filter=afoo/abar,-afoo/abaz,bfoo/bbar,-bfoo/bbaz,cfoo/cbar,-cfoo/cbaz
```
With a large set of flags and their respective large filter strings it is uneasy
to combine the short hand configuration flags because the respective filter strings
need to be concatenated into a single string. This is uneasy to maintain because
filter strings are repeated in the combinations.

`--experimental_instrumentation_filter_fragment` simplifies combining multiple shorthand
configuration flags because its multiple uses are accumulated. e.g.,
```
build:covA  --experimental_instrumentation_filter_fragment=afoo/abar,-afoo/abaz
build:covB  --experimental_instrumentation_filter_fragment=bfoo/bbar,-bfoo/bbaz
build:covC  --experimental_instrumentation_filter_fragment=cfoo/cbar,-cfoo/cbaz
build:covAB --config covA --config covB
build:covAC --config covA --config covC
build:covABC --config covA --config covB --config covC
```

Add tests for --experimental_instrumentation_filter_fragment cli to verify that its
multiple uses are accumulated and --instrumentation_filter has no effect
when --experimental_instrumentation_filter_fragment is used.

bazelbuild#22959 is the upstream issue.

Testing Done:
```
$ bazelisk  test src/test/java/com/google/devtools/build/lib/starlark:all
INFO: Analyzed 4 targets (0 packages loaded, 0 targets configured).
INFO: Found 1 target and 3 test targets...
INFO: Elapsed time: 242.197s, Critical Path: 216.72s
INFO: 27 processes: 1 internal, 25 darwin-sandbox, 1 worker.
INFO: Build completed successfully, 27 total actions
//src/test/java/com/google/devtools/build/lib/starlark:BindTest (cached) PASSED in 7.5s
//src/test/java/com/google/devtools/build/lib/starlark:StarlarkRuleClassFunctionsTest (cached) PASSED in 78.0s
  Stats over 5 runs: max = 78.0s, min = 59.8s, avg = 73.9s, dev = 7.1s
//src/test/java/com/google/devtools/build/lib/starlark:StarlarkTests     PASSED in 121.1s
  Stats over 25 runs: max = 121.1s, min = 24.8s, avg = 88.6s, dev = 32.1s

Executed 1 out of 3 tests: 3 tests pass.
There were tests whose specified size is too big. Use the --test_verbose_timeout_warnings command line option to see which ones these are.
```
@arvi18
Copy link
Author

arvi18 commented Apr 28, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@arvi18
Copy link
Author

arvi18 commented Apr 28, 2025

As I commented earlier, I still think --instrumentation-filter should override this rather than the other way around (that is, if the regular filter is set, this should be ignore), since that lets me easily set a custom filter on a command line without having to worry about what's configured in various bazelrcs.

But otherwise, I don't mind accepting this as an experimental feature.

Copy link

coderabbitai bot commented Apr 28, 2025

Walkthrough

This change introduces a new experimental command-line option, --experimental_instrumentation_filter_fragment, to control coverage instrumentation filtering in the build configuration. The option is added to the core options, and logic is updated in the build configuration to prioritize this new filter over the legacy instrumentation_filter when specified. The filter is constructed by combining multiple flag values and converting them into a RegexFilter. Comprehensive tests are added to verify the correct behavior of coverage instrumentation in various scenarios involving the new option, including precedence handling and matching logic.

Changes

File(s) Change Summary
src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java Added a private final field instrumentationFilter of type RegexFilter. Modified the constructor to initialize this field using the new instrumentationFilterFragment option if present, or fallback to the legacy instrumentationFilter. Updated the getter to return this new field. Added necessary imports for handling string joining and exceptions.
src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java Introduced a new public option instrumentationFilterFragment as a list of strings, annotated for command-line usage as --experimental_instrumentation_filter_fragment. Updated help text to clarify its precedence and usage over the legacy filter option. No other logic changes.
src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleContextTest.java Added multiple new test methods to verify the behavior of coverage instrumentation when the experimental filter fragment flag is used. Tests cover matching, non-matching, precedence over the legacy flag, and behavior with source file labels and non-default labels. Existing logic and tests remain unchanged.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CoreOptions
    participant BuildConfigurationValue
    participant RegexFilter

    User->>CoreOptions: Specify --experimental_instrumentation_filter_fragment (possibly multiple times)
    CoreOptions->>BuildConfigurationValue: Pass options to constructor
    BuildConfigurationValue->>BuildConfigurationValue: Check if instrumentationFilterFragment is non-empty
    alt instrumentationFilterFragment is non-empty
        BuildConfigurationValue->>RegexFilter: Convert joined fragments to RegexFilter
        RegexFilter-->>BuildConfigurationValue: Return RegexFilter instance
        BuildConfigurationValue->>BuildConfigurationValue: Set instrumentationFilter to new RegexFilter
    else instrumentationFilterFragment is empty
        BuildConfigurationValue->>BuildConfigurationValue: Set instrumentationFilter to legacy option
    end
    BuildConfigurationValue->>User: getInstrumentationFilter() returns the correct filter
Loading

Poem

In fields of flags, a filter new,
Experimental, regexes grew!
Old filters now must step aside,
For fragments join and rules divide.
With tests that hop from case to case,
Coverage finds its proper place.
Hooray for change—let’s celebrate,
With carrot cake upon our plate! 🥕

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @arvi18, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request introduces a new command-line option, --experimental_instrumentation_filter_fragment, to accumulate instrumentation filters for code coverage. This simplifies combining multiple shorthand configuration flags in bazelrc files. The existing --instrumentation_filter option is overridden when the new flag is present. The changes include modifications to BuildConfigurationValue.java and CoreOptions.java to implement the new option and its behavior, and the addition of new tests in StarlarkRuleContextTest.java to verify the functionality.

Highlights

  • New CLI Option: Introduces --experimental_instrumentation_filter_fragment to accumulate instrumentation filters, addressing issue Accumulate multiple uses of --instrumentation_filter cli flag bazelbuild/bazel#22959.
  • Configuration Changes: Modifies BuildConfigurationValue.java to handle the new option and prioritize it over --instrumentation_filter.
  • Option Definition: Adds the definition of the new option in CoreOptions.java.
  • Testing: Adds new tests to StarlarkRuleContextTest.java to validate the behavior of the new option.

Changelog

Click here to see the changelog
  • src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java
    • Adds import for com.google.common.base.Joiner at line 18.
    • Adds import for com.google.devtools.common.options.OptionsParsingException at line 48.
    • Adds a private instrumentationFilter field of type RegexFilter at line 148.
    • Initializes instrumentationFilter based on --experimental_instrumentation_filter_fragment or --instrumentation_filter at lines 306-317.
    • Returns the value of the instrumentationFilter field in getInstrumentationFilter() at line 586.
  • src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java
    • Adds import for com.google.common.base.Joiner at line 20.
    • Adds the --experimental_instrumentation_filter_fragment option, which allows multiple values, at lines 279-292.
  • src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleContextTest.java
    • Adds tests for --experimental_instrumentation_filter_fragment to verify its behavior with coverage instrumentation at lines 3024-3094.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A new flag appears,
Filters fragment, quell our fears,
Coverage now refined.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new command-line option to accumulate instrumentation filters, which simplifies combining multiple shorthand configuration flags. The code is well-structured and includes tests to verify the new functionality. However, there are a few areas where the code can be improved, such as exception handling and test coverage.

Summary of Findings

  • Instrumentation Filter Logic: The pull request introduces a new flag, --experimental_instrumentation_filter_fragment, to accumulate instrumentation filters. The logic correctly prioritizes this new flag over the existing --instrumentation_filter. However, the exception handling in BuildConfigurationValue.java could be improved to provide more context to the user.
  • Test Coverage: The pull request includes new tests to verify the behavior of the new flag. The tests cover cases where the flag is used multiple times, and when it's used in conjunction with the old flag. However, consider adding tests for edge cases, such as empty filter fragments or invalid regex patterns.

Merge Readiness

The pull request is almost ready for merging. Addressing the medium severity issue related to exception handling in BuildConfigurationValue.java would improve the user experience. While the tests cover the main functionality, adding tests for edge cases would increase the robustness of the code. Once these issues are addressed, the pull request should be ready for merging. I am unable to approve this pull request, and recommend that others review and approve this code before merging.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c0c98a2 and d0c75ff.

📒 Files selected for processing (3)
  • src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java (5 hunks)
  • src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java (2 hunks)
  • src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleContextTest.java (1 hunks)
🔇 Additional comments (12)
src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java (1)

278-292: Good implementation of the new experimental option

This new option effectively adds the requested functionality to specify multiple instrumentation filter fragments that accumulate. The option is properly documented, with the accumulation behavior and precedence over the legacy option clearly explained in the help text.

A few observations:

  • The option correctly uses allowMultiple = true to enable accumulation
  • The precedence is set so that this option overrides the legacy --instrumentation_filter when specified
  • The help text is comprehensive and includes all necessary details
src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java (5)

148-148: LGTM: Clean field declaration

The field declaration for the new RegexFilter instance is appropriate and correctly marked as final.


586-586: LGTM: Getter returns the correct field

The getter now returns the newly computed field rather than directly accessing options.instrumentationFilter, which correctly implements the precedence described in the option's help text.


17-19: LGTM: Required import for Joiner

The import for Joiner is needed to support joining the filter fragments with commas.


48-48: LGTM: Required import for exception handling

The import for OptionsParsingException is needed to handle potential parsing errors when converting the joined fragments to a RegexFilter.


306-317: Verify compatibility with the PR requirements

The current implementation makes the experimental option override the legacy option when both are specified. However, in the PR objectives, the author mentioned a preference for the legacy option to override the experimental one.

The help text and implementation both make the experimental option take precedence, but the PR description mentioned a preference for the opposite behavior. Please confirm that the current precedence is the intended behavior.

src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleContextTest.java (6)

3023-3033: Well-written test for basic experimental flag behavior with coverage disabled.

The test correctly verifies that when code coverage collection is disabled (--nocollect_code_coverage), the coverage_instrumented() method should return false, regardless of the experimental filter fragment setting. This establishes a good baseline for the experimental feature.


3036-3045: Good test for source file label behavior with experimental filter fragment.

This test correctly verifies that source files are not instrumented, even when the experimental filter fragment is set. This follows the same behavior as the existing instrumentation filter and maintains consistency in the coverage system.


3048-3057: Proper test for filter non-matching behavior.

This test correctly validates that a rule is not instrumented when it doesn't match the filter pattern specified by the experimental instrumentation filter fragment. The test demonstrates how negation patterns work with the new flag (--experimental_instrumentation_filter_fragment=-:bar).


3060-3069: Good test for filter matching behavior.

This test properly verifies that rules matching the filter pattern are correctly instrumented. It also demonstrates that multiple experimental filter fragments can be combined, which is the core functionality of this new feature.


3072-3083: Important test for precedence behavior.

This test correctly validates that when both --experimental_instrumentation_filter_fragment and --instrumentation_filter are specified, the experimental filter takes precedence. This is a critical part of the feature as mentioned in the PR objectives.


3086-3095: Thorough test for non-default label matching.

This test properly verifies that dependencies (non-default labels) are correctly instrumented when they match the experimental filter fragment. It completes the test suite with comprehensive coverage of the feature's behavior.

Comment on lines +306 to +317
if (!options.instrumentationFilterFragment.isEmpty()) {
// A regex-based instrumentation filter instance is formed by concatenating
// string values of --experimental_instrumentation_filter_fragment.
try {
this.instrumentationFilter = new RegexFilter.RegexFilterConverter().convert(
Joiner.on(",").join(options.instrumentationFilterFragment));
} catch (OptionsParsingException e) {
throw new RuntimeException(e);
}
} else {
this.instrumentationFilter = options.instrumentationFilter;
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Implementation handles the new option properly but could improve error handling

The implementation correctly joins multiple filter fragments and creates a RegexFilter when the experimental option is specified, falling back to the legacy filter otherwise.

Consider improving the error handling by including a more descriptive error message when wrapping the OptionsParsingException:

try {
  this.instrumentationFilter = new RegexFilter.RegexFilterConverter().convert(
    Joiner.on(",").join(options.instrumentationFilterFragment));
} catch (OptionsParsingException e) {
-  throw new RuntimeException(e);
+  throw new RuntimeException("Failed to parse experimental_instrumentation_filter_fragment: " + e.getMessage(), e);
}

This would make debugging easier by providing context about which option caused the error.

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