Skip to content

cquery: Include toolchains as ruleInputs #22

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

These were already returned in a text output for a deps query - this adds them also as ruleInputs.

Fixes bazelbuild#19058

Summary by CodeRabbit

  • New Features

    • Configured queries now explicitly include implicit toolchain dependencies as rule inputs in their output.
  • Tests

    • Added an integration test to verify that toolchain dependencies are correctly reported as rule inputs in configured queries.

These were already returned in a text output for a deps query - this
adds them also as ruleInputs.
@arvi18
Copy link
Author

arvi18 commented Apr 28, 2025

cc @gregestren

@arvi18
Copy link
Author

arvi18 commented Apr 28, 2025

I'm open to this but want to run this by someone else with an interest in cquery's proto outputs. I'm not sure of their Github handle so I'll send this link to them directly.

@arvi18
Copy link
Author

arvi18 commented Apr 28, 2025

Sounds great - please let me know if there's anything to do here :) It'd be easy to gate this behaviour behind a flag or something if that'd be simpler for folks.

@arvi18
Copy link
Author

arvi18 commented Apr 28, 2025

@gregestren any news here? Thanks!

@arvi18
Copy link
Author

arvi18 commented Apr 28, 2025

No new updates here yet, I'm sorry. I am still trying to get bandwidth on this but haven't followed up yet.

Copy link

coderabbitai bot commented Apr 28, 2025

Walkthrough

The changes introduce explicit handling and reporting of implicit toolchain dependencies in the query output of a build system. The core update modifies how target protocol buffers are generated, ensuring that implicit toolchain dependencies are included as rule inputs. This is achieved by altering method signatures and internal logic in the proto output formatter classes, and updating related method calls to provide the necessary dependency information. Additionally, a new integration test is added to verify that toolchain dependencies are correctly reported in configured queries.

Changes

File(s) Change Summary
src/main/java/com/google/devtools/build/lib/query2/cquery/ProtoOutputFormatterCallback.java Enhanced the processOutput method to collect and pass implicit toolchain dependencies for RuleConfiguredTarget instances, ensuring these are included in the target proto buffer output.
src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java Updated toTargetProtoBuffer to accept and process an additional parameter for extra implicit dependencies, merging them with regular rule inputs. Refactored method signatures and internal logic accordingly. Added import for ImmutableSortedSet.
src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedJSONProtoOutputFormatter.java,
src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedProtoOutputFormatter.java
Modified internal calls to toTargetProtoBuffer to pass an empty ImmutableSortedSet as the new required argument. Added necessary import for ImmutableSortedSet.
src/test/shell/integration/configured_query_test.sh Added a new integration test test_toolchain_rule_inputs to verify that toolchain dependencies are correctly reported as rule inputs in configured query outputs.

Sequence Diagram(s)

sequenceDiagram
    participant QueryEngine
    participant ProtoOutputFormatterCallback
    participant ProtoOutputFormatter
    participant RuleConfiguredTarget

    QueryEngine->>ProtoOutputFormatterCallback: processOutput(ConfiguredTarget)
    alt ConfiguredTarget is RuleConfiguredTarget
        ProtoOutputFormatterCallback->>RuleConfiguredTarget: getImplicitDeps()
        ProtoOutputFormatterCallback->>ProtoOutputFormatter: toTargetProtoBuffer(target, implicitDeps)
    else
        ProtoOutputFormatterCallback->>ProtoOutputFormatter: toTargetProtoBuffer(target, empty set)
    end
    ProtoOutputFormatter->>QueryEngine: Build.Target proto (with all deps)
Loading

Poem

In the warren where toolchains hide,
Now their footprints won’t subside.
With sorted sets and careful code,
Implicit paths are now bestowed.
Tests ensure the truth is told,
Every toolchain’s in the fold.
🛠️🐇—A rabbit’s work, precise and bold!

✨ 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 addresses issue bazelbuild#19058 by including toolchains as ruleInputs in cquery results. Previously, these toolchains were only available in the text output of a deps query. The changes modify the ProtoOutputFormatterCallback and ProtoOutputFormatter classes to include toolchain dependencies as rule inputs, which are identified as implicit dependencies of RuleConfiguredTargets. A new test case is added to verify the functionality.

Highlights

  • Toolchain Inclusion: Toolchains are now included as ruleInputs in cquery output, fixing issue cquery doesn't include artificial toolchain deps in ruleInputs section, but does in configuredRuleInputs bazelbuild/bazel#19058.
  • Implicit Dependency Handling: The code now explicitly handles implicit dependencies (toolchains) that are not captured by attribute traversal.
  • Code Modification: Modified ProtoOutputFormatterCallback to add implicit dependencies from RuleConfiguredTarget as rule inputs.
  • Code Modification: Modified ProtoOutputFormatter to include implicit dependencies when converting a Target to a Build.Target protobuf.
  • Test Addition: Added a new integration test test_toolchain_rule_inputs to verify the correct inclusion of toolchains as rule inputs.

Changelog

Click here to see the changelog
  • src/main/java/com/google/devtools/build/lib/query2/cquery/ProtoOutputFormatterCallback.java
    • Imported ImmutableSortedSet to manage implicit dependencies.
    • Imported RuleConfiguredTarget to identify rule-based configured targets.
    • Added logic to extract implicit dependencies (toolchains) from RuleConfiguredTarget and include them as rule inputs.
    • Modified processOutput to add implicit dependencies as inputs.
  • src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java
    • Imported ImmutableSortedSet.
    • Modified toTargetProtoBuffer methods to accept and handle ImmutableSortedSet<Label> for extra implicit dependencies.
    • Modified toTargetProtoBuffer to include implicit dependencies when converting a Target to a Build.Target protobuf.
    • Modified processOutput to pass an empty ImmutableSortedSet to toTargetProtoBuffer.
  • src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedJSONProtoOutputFormatter.java
    • Imported ImmutableSortedSet.
    • Modified processOutput to pass an empty ImmutableSortedSet to toTargetProtoBuffer.
  • src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedProtoOutputFormatter.java
    • Imported ImmutableSortedSet.
    • Modified createPostFactoStreamCallback to pass an empty ImmutableSortedSet to toTargetProtoBuffer.
  • src/test/shell/integration/configured_query_test.sh
    • Added a new integration test test_toolchain_rule_inputs to verify the correct inclusion of toolchains as rule inputs.
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 toolchain's hidden,
Implicit, now brought to light,
Query's answer clear.

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

The pull request introduces changes to include toolchains as ruleInputs in cquery, addressing issue bazelbuild#19058. The changes involve modifying the ProtoOutputFormatterCallback and ProtoOutputFormatter classes to handle toolchain dependencies correctly. Additionally, a new test case is added to verify the functionality.

Summary of Findings

  • Missing null check: In ProtoOutputFormatterCallback.java, the configuredTargetKey is not checked for null before calling getConfiguration(configurationKey). This could lead to a NullPointerException if configuredTargetKey is null.
  • Inconsistent handling of implicit dependencies: The code in ProtoOutputFormatterCallback.java adds implicit dependencies as rule inputs. It's important to ensure that this logic is consistent with how other dependencies are handled and that it doesn't introduce any unintended side effects.
  • Potential performance impact: The addition of toolchain dependencies as rule inputs could potentially increase the size of the output and impact performance, especially for large projects. It's important to monitor the performance impact of these changes.

Merge Readiness

The pull request introduces important functionality by including toolchains as ruleInputs in cquery. However, there are a few issues that need to be addressed before merging. Specifically, the missing null check in ProtoOutputFormatterCallback.java should be added to prevent potential NullPointerExceptions. Additionally, the potential performance impact of these changes should be monitored. I am unable to approve this pull request, and recommend that it not be merged until the identified issues are addressed and re-reviewed. Other reviewers should also review and approve this code before merging.

Comment on lines +247 to +250
ImmutableSortedSet<Label> ruleInputs = ImmutableSortedSet.<Label>naturalOrder()
.addAll(rule.getSortedLabels(dependencyFilter))
.addAll(extraImplicitDeps)
.build();

Choose a reason for hiding this comment

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

medium

Consider adding a comment explaining why ImmutableSortedSet is being used here, and what the performance implications are.

        // Include explicit elements for all direct inputs and outputs of a rule; this goes beyond
        // what is available from the attributes above, since it may also (depending on options)
        // include implicit outputs, exec-configuration outputs, and default values.
        // Using ImmutableSortedSet to ensure deterministic order of rule inputs.
        ImmutableSortedSet<Label> ruleInputs = ImmutableSortedSet.<Label>naturalOrder()
            .addAll(rule.getSortedLabels(dependencyFilter))
            .addAll(extraImplicitDeps)
            .build();

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: 0

🧹 Nitpick comments (1)
src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java (1)

179-180: Consider adding JavaDoc to clarify the purpose of extraImplicitDeps.

The method signature change is correct, but the extraImplicitDeps parameter would benefit from documentation explaining that it's intended for implicit toolchain dependencies.

  /** Converts a logical {@link Target} object into a {@link Build.Target} protobuffer. */
+ * @param target The target to convert
+ * @param extraImplicitDeps Additional implicit dependencies (like toolchains) to include as rule inputs
+ * @param extraDataForAttrHash Additional data to include in synthetic attribute hash calculation
  */
  private Build.Target toTargetProtoBuffer(Target target, ImmutableSortedSet<Label> extraImplicitDeps, Object extraDataForAttrHash)
      throws InterruptedException {
📜 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 a09cf42.

📒 Files selected for processing (5)
  • src/main/java/com/google/devtools/build/lib/query2/cquery/ProtoOutputFormatterCallback.java (3 hunks)
  • src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java (4 hunks)
  • src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedJSONProtoOutputFormatter.java (2 hunks)
  • src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedProtoOutputFormatter.java (2 hunks)
  • src/test/shell/integration/configured_query_test.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck (0.10.0)
src/test/shell/integration/configured_query_test.sh

[warning] 1525-1525: Expanding an array without an index only gives the first element.

(SC2128)

🔇 Additional comments (11)
src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedProtoOutputFormatter.java (1)

16-16: Method signature update well-implemented

The import of ImmutableSortedSet and the updated call to toTargetProtoBuffer with an empty set are correctly implemented to accommodate the new parameter for implicit dependencies. This change aligns properly with the method signature update in the parent class.

Also applies to: 42-42

src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedJSONProtoOutputFormatter.java (1)

16-16: Method signature update correctly applied

The necessary import has been added and the method call updated properly to accommodate the new parameter for implicit dependencies, maintaining consistency with the parent class changes.

Also applies to: 48-48

src/main/java/com/google/devtools/build/lib/query2/cquery/ProtoOutputFormatterCallback.java (3)

20-20: Required imports properly added

The imports for ImmutableSortedSet and RuleConfiguredTarget are correctly added to support the new functionality for collecting toolchain dependencies.

Also applies to: 31-31


249-260: Well-documented implementation for collecting toolchain dependencies

This implementation correctly identifies and collects implicit toolchain dependencies that wouldn't otherwise be discovered through attribute traversal. The code is clear, well-commented, and follows the appropriate pattern for collecting dependencies into a sorted set.


261-261: Proper integration with target proto buffer generation

The collected implicit dependencies are correctly passed to the toTargetProtoBuffer method, ensuring toolchain dependencies are included in the rule inputs for the proto output.

src/test/shell/integration/configured_query_test.sh (1)

1524-1603: Comprehensive test for toolchain rule inputs

This test thoroughly verifies the new functionality by:

  1. Setting up a custom toolchain type and implementation
  2. Creating a rule that consumes the toolchain
  3. Running a configured query with the extra toolchain
  4. Verifying that the toolchain appears in the rule inputs

The test covers the end-to-end functionality well and serves as good documentation for how toolchain dependencies should now appear in query results.

🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 1525-1525: Expanding an array without an index only gives the first element.

(SC2128)

src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java (5)

29-29: Import addition is appropriate.

Adding ImmutableSortedSet is necessary to support the new parameter in toTargetProtoBuffer method.


174-176: Method signature change looks good with proper delegation.

The method now accepts an additional parameter extraImplicitDeps to include toolchain dependencies. The delegation to the private method with an empty default for extraDataForAttrHash maintains a clean API separation.


247-252: Correctly implements toolchain inclusion as rule inputs.

The implementation properly merges the original rule dependencies with the extra implicit dependencies using a sorted set builder. This ensures deterministic ordering of the inputs in the proto output.

I'd recommend adding a brief comment explaining that extraImplicitDeps represents toolchain dependencies:

  // Include explicit elements for all direct inputs and outputs of a rule; this goes beyond
  // what is available from the attributes above, since it may also (depending on options)
  // include implicit outputs, exec-configuration outputs, and default values.
+ // Also includes toolchain dependencies and other implicit deps not attached to any attribute
  ImmutableSortedSet<Label> ruleInputs = ImmutableSortedSet.<Label>naturalOrder()
      .addAll(rule.getSortedLabels(dependencyFilter))
      .addAll(extraImplicitDeps)
      .build();

573-573: Correctly updates call site with empty set.

This change correctly adapts the existing usage to the new method signature by passing an empty set of extra implicit dependencies, maintaining compatibility with existing code.


1-583:

✅ Verification successful

Verify integration test coverage.

The changes look good overall. To ensure these changes work as expected, make sure there's an integration test that verifies toolchain dependencies are correctly included in the proto output.


🏁 Script executed:

#!/bin/bash
# Description: Check for the presence of an integration test for toolchain dependencies in query output

# Look for test cases related to toolchain rule inputs
rg -A 10 "test.*toolchain.*rule.*input" --glob "*.sh" src/test/

Length of output: 898


Integration test coverage verified

An existing integration test (test_toolchain_rule_inputs) in
src/test/shell/integration/configured_query_test.sh covers toolchain rule inputs in the query output. No further action required.

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.

cquery doesn't include artificial toolchain deps in ruleInputs section, but does in configuredRuleInputs
2 participants