Buck Query Language
Buck2's query language provides a powerful way to inspect and analyze the build
graph. The query language is shared across different query commands
(buck2 uquery, buck2 cquery, and buck2 aquery), though each command
operates on different graph representations and supports different sets of
operators.
Query Parameters
The most common parameter for a Buck query operator is an expression that evaluates to a build target or collection of build targets. Such an expression could be:
- An explicit build target
- A build target pattern
- A .buckconfig alias
- The set of targets returned by another Buck query operator
Tip: You can pass an alias directly to the buck2 query command line to see
what it resolves to. For example:
buck2 uquery app
Non-target Parameters
In addition to target parameters, some Buck query operators take string
parameters such as filenames (owner()) or regular expressions (filter()).
Note: Hover over parameters in query operator syntax to see their descriptions in the command documentation.
Quoting Arguments
It is not necessary to quote arguments if they comprise sequences of characters
drawn from the alphabet, numerals, forward slash (/), colon (:), period
(.), hyphen (-), underscore (_), or asterisk (*)—and they do not start
with a hyphen or period. For example, quoting java_test is unnecessary.
However, we do recommend that you quote arguments as a best practice even when Buck2 doesn't require it.
You should always use quotes when writing scripts that construct buck2 query
expressions from user-supplied values.
Note that argument quoting for buck2 query is in addition to any quoting that
your shell requires. In the following example, double-quotes are used for the
shell and single-quotes are used for the build target expression:
buck2 uquery "'//foo:bar=wiz'"
Algebraic Set Operations
Buck2's query language supports algebraic set operations for combining query results.
Set Operations: intersection, union, set difference
| Nominal | Symbolic |
|---|---|
intersect | ^ |
union | + |
except | - |
These three operators compute the corresponding set operations over their
arguments. Each operator has two forms: a nominal form (e.g., intersect) and a
symbolic form (e.g., ^). The two forms are equivalent; the symbolic forms are
just faster to type.
For example:
buck2 uquery "deps('//foo:bar') intersect deps('//baz:lib')"
and
buck2 uquery "deps('//foo:bar') ^ deps('//baz:lib')"
both return the targets that appear in the transitive closure of //foo:bar and
//baz:lib.
Properties:
- The
intersect(^) andunion(+) operators are commutative - The
except(-) operator is not commutative - The parser treats all three operators as left-associative and of equal precedence
We recommend that you use parentheses if you need to ensure a specific order of evaluation. A parenthesized expression resolves to the value of the expression it encloses. For example, the first two expressions are equivalent, but the third is not:
x intersect y union z
(x intersect y) union z
x intersect (y union z)