Skip to content

Conversation

keith
Copy link
Member

@keith keith commented Sep 22, 2025

This allows combining the common pattern of build and then query into a
single build command that executes the passed query (or query_file)

Fixes #26938

RELNOTES[NEW]: Add 'build --target_query' flag for building the result of a
bazel query in a single command

@keith keith force-pushed the ks/add-support-for-build-query branch from decad34 to 4307f8c Compare September 22, 2025 22:31
} catch (QueryException | InterruptedException | IOException e) {
throw new TargetPatternsHelperException(
"Error executing query: " + e.getMessage(),
TargetPatterns.Code.TARGET_PATTERNS_UNKNOWN);
Copy link
Member Author

Choose a reason for hiding this comment

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

idk what exit codes i should really use here, i didn't want to force all callers to handle this same logic, so I kept only this 1 exception type that they're already handling

This allows combining the common pattern of build and then query into a
single build command that executes the passed query (or query_file)

Fixes #26938

RELNOTES[inc]: Add 'build --query' flag for building the result of a
bazel query in a single command
@keith keith force-pushed the ks/add-support-for-build-query branch from 4307f8c to fbb7c00 Compare September 22, 2025 22:38
@keith keith force-pushed the ks/add-support-for-build-query branch from 3d6f761 to 2d3e949 Compare September 22, 2025 23:06
@keith keith changed the title Add support for 'build --query' Add support for building queried targets in 1 command Sep 22, 2025
@fmeum
Copy link
Collaborator

fmeum commented Sep 23, 2025

RELNOTES[NEW] seems more appropriate for a new flag, it's not an incompatible change.

help =
"If set, build will evaluate the query expression and build the resulting targets. "
+ "Example: --query='deps(//foo) - deps(//bar)'. It is an error to specify this "
+ "along with command-line patterns or --target_pattern_file.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Or --target_query_file?

if (!targets.isEmpty() && !buildRequestOptions.targetPatternFile.isEmpty()) {

int optionCount = 0;
if (!targets.isEmpty()) optionCount++;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Google style dislikes single line ifs.

TargetPatterns.Code.TARGET_PATTERNS_UNKNOWN);
}

return new ArrayList<>(targetPatterns);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Prefer ImmutableList.copyOf here and make the return type more specific.

if (!targets.isEmpty() && !buildRequestOptions.targetPatternFile.isEmpty()) {

int optionCount = 0;
if (!targets.isEmpty()) optionCount++;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a design reason that residue targets can't be mixed with a query?
I am specifically interested in how it interacts with --remote_download_outputs=toplevel, as that is one place where we use a query like this to download only certain files, while still building everything:

bazel test --remote_download_outputs=top-level //... $(bazel query 'attr(tags, ci-download-outputs, //...)')

I haven't figured out if there is an existing technical limitation that would make supporting that difficult.

Copy link
Member Author

Choose a reason for hiding this comment

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

nothing inherent, i inherited this logic from targetPatternFile and I haven't traced back why it was done for that case

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.

Add --query flag to bazel build
3 participants