bazelbuild / bazelbuild/bazel

Add `--query` flag to `bazel build`

Open
#26,938 1 comment 2 reactions 0 assignees View on GitHub
help wanted P3 team-CLI type: feature request
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### Description of the feature request:

`bazel query` is a powerful tool, but the most common way to connect it to `bazel build` is through a shell's command substitution, such as:
```bash
bazel build $(bazel query 'deps(foo) - deps(bar)')
```
This works fine, despite being a bit of a syntactic mess, until the query starts returning too many targets, and then the command line grows too long and things start breaking. At which point you might do something like:
```bash
temp_file=$(mktemp)
bazel query --output_file=${temp_file} 'deps(foo) - deps(bar)'
bazel build --target_pattern_file=${temp_file}
```
Which is okay, but this is difficult enough that it usually isn't implemented until _after_ something has broken, instead of just working from the beginning.
It is also burdensome when invoking bazel programmatically, instead of from a command line, especially in environments that don't have a builtin way to produce a tmp file (such as node.js).

This pain could be resolved by allowing the query syntax to be passed to `bazel build` (or `test`, or `coverage`) invocations in a similar way to `--target_pattern_file`, but instead of reading a file to discover additional targets to add to the invocation, it would execute a `bazel query`.

The invocation would then look like this:
```bash
bazel build --query='deps(foo) - deps(bar)'
```

This would be more efficient than any of the current alternatives, and wouldn't run into command line size limits for large results.

Additionally, the existing `--query_file` flag that already exists on `bazel query` could be added to `build` too.

### Which category does this issue belong to?

Core

Contributor guide

Open the contributing guide

Research direction

Start by tracing Bazel's existing handling of --target_pattern_file and query's --query_file across the build and query command entry points. Define how a build invocation should combine query results with target patterns, then verify the behavior for build and the mentioned test or coverage variants without command-line-size limits.

Written by the indexing model from the issue text.

Assessment

Domain
build-system, cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.