Aquery (action graph query)
aquery command allows you to query for actions in your build graph.
It operates on the post-analysis Configured Target Graph and exposes
information about Actions, Artifacts and their relationships.
aquery is useful when we are interested in the properties of the Actions/Artifacts
generated from the Configured Target Graph. For example, the actual commands run
and their inputs/outputs/mnemonics.
The tool accepts several command-line options. Notably, the aquery command runs on top of a regular Bazel build and inherits the set of options available during a build.
It supports the same set of functions that is also available to traditional
aquery output (without specific details):
$ bazel aquery 'deps(//some:label)' action 'Writing file some_file_name' Mnemonic: ... Target: ... Configuration: ... ActionKey: ... Inputs: [...] Outputs: [...]
A simple example of the syntax for
aquery is as follows:
bazel aquery "aquery_function(function(//target))"
The query expression (in quotes) consists of the following:
aquery_function(...): functions specific to
aquery. More details below.
function(...): the standard functions as traditional
//targetis the label to the interested target.
# aquery examples: # Get the action graph generated while building //src/target_a $ bazel aquery '//src/target_a' # Get the action graph generated while building all dependencies of //src/target_a $ bazel aquery 'deps(//src/target_a)' # Get the action graph generated while building all dependencies of //src/target_a # whose inputs filenames match the regex ".*cpp". $ bazel aquery 'inputs(".*cpp", deps(//src/target_a))'
There are currently 3
inputs: filter actions by inputs.
outputs: filter actions by outputs
mnemonic: filter actions by mnemonic
expr ::= inputs(word, expr)
inputs operator returns the actions generated from building
whose input filenames match the regex provided by
$ bazel aquery 'inputs(".*cpp", deps(//src/target_a))'
mnemonic functions share a similar syntax.
You can also combine functions to achieve the AND operation. For example:
$ bazel aquery 'mnemonic("Cpp.*", (inputs(".*cpp", inputs("foo.*", //src/target_a))))'
The above command would find all actions involved in building
whose mnemonics match
"Cpp.*" and inputs match the patterns
Important: aquery functions can't be nested inside non-aquery functions
- Conceptually this makes sense since the output of aquery functions is Actions, not Configured Targets.
An example of the syntax error produced:
$ bazel aquery 'deps(inputs(".*cpp", //src/target_a))' ERROR: aquery filter functions (inputs, outputs, mnemonic) produce actions, and therefore can't be the input of other function types: deps deps(inputs(".*cpp", //src/target_a))
aquery runs on top of a regular Bazel build and thus inherits the set of
available during a build.
The default output format (
text) is human-readable,
jsonproto for machine-readable format.
Includes the content of the action command lines in the output (potentially large).
Includes names of the action inputs and outputs in the output (potentially large).
Whether to include Aspect-generated actions in the output.
Include the content of the param files used in the command (potentially large).
Warning: Enabling this flag will automatically enable the
Without performing extra analysis, dump the current Action Graph from Skyframe.
Note: Specifying a target with
--skyframe_state is currently not supported.
This flag is only available with
Other tools and features
Querying against the state of Skyframe
In some cases, it is useful to query the Action Graph currently on Skyframe. An example use case would be:
bazel build //target_a
bazel build //target_b
As a Bazel user, I want to determine if
foo.out was generated from building
One could run
bazel aquery 'outputs("foo.out", //target_a)' and
bazel aquery 'outputs("foo.out", //target_b)' to figure out the action responsible
foo.out, and in turn the target. However, the number of different
targets previously built can be larger than 2, which makes running multiple
commands a hassle.
As an alternative, the
--skyframe_state flag can be used:
# List all actions on Skyframe's action graph $ bazel aquery --output=proto --skyframe_state # or # List all actions on Skyframe's action graph, whose output matches "foo.out" $ bazel aquery --output=proto --skyframe_state 'outputs("foo.out")'
aquery takes the content of the Action Graph
that Skyframe keeps on the current instance of Bazel, (optionally) performs filtering on it and
outputs the content, without re-running the analysis phase.
--skyframe_state is currently only available for
Non-inclusion of target labels in the query expression
--skyframe_state queries the whole action graph that exists on Skyframe,
regardless of the targets. Having the target label specified in the query together with
--skyframe_state is considered a syntax error:
# WRONG: Target Included $ bazel aquery --output=proto --skyframe_state //target_a ERROR: Error while parsing '//target_a)': Specifying build target(s) [//target_a] with --skyframe_state is currently not supported. # WRONG: Target Included $ bazel aquery --output=proto --skyframe_state 'inputs(".*.java", //target_a)' ERROR: Error while parsing '//target_a)': Specifying build target(s) [//target_a] with --skyframe_state is currently not supported. # CORRECT: Without Target $ bazel aquery --output=proto --skyframe_state $ bazel aquery --output=proto --skyframe_state 'inputs(".*.java")'
Comparing aquery outputs
You can compare the outputs of two different aquery invocations using the
For instance: when you make some changes to your rule definition and want to verify that the
command lines being run did not change.
aquery_differ is the tool for that.
The tool is available in the bazelbuild/bazel repository. To use it, clone the repository to your local machine. An example usage:
$ bazel run //tools/aquery_differ -- \ --before=/path/to/before.proto \ --after=/path/to/after.proto \ --input_type=proto \ --attrs=cmdline \ --attrs=inputs
The above command returns the difference between the
after aquery outputs:
which actions were present in one but not the other, which actions have different
command line/inputs in each aquery output, ...). The result of running the above command would be:
Aquery output 'after' change contains an action that generates the following outputs that aquery output 'before' change doesn't: ... /list of output files/ ... [cmdline] Difference in the action that generates the following output(s): /path/to/abc.out --- /path/to/before.proto +++ /path/to/after.proto @@ -1,3 +1,3 @@ ... /cmdline diff, in unified diff format/ ...
--before, --after: The aquery output files to be compared
--input_type=(proto|text_proto), default=proto: the format of the input
files. We currently support
textproto aquery output.
--attrs=(cmdline|inputs), default=cmdline: the attributes of actions
to be compared.
It is possible for Aspects to be applied on top of each other. The aquery output of the action generated by these Aspects would then include the Aspect path, which is the sequence of Aspects applied to the target which generated the action.
An example of Aspect-on-Aspect:
t0 ^ | <- a1 t1 ^ | <- a2 t2
Let ti be a target of rule ri, which applies an Aspect ai to its dependencies.
Assume that a2 generates an action X when applied to target t0. The text output of
bazel aquery --include_aspects 'deps(//t2)' for action X would be:
action ... Mnemonic: ... Target: //my_pkg:t0 Configuration: ... AspectDescriptors: [//my_pkg:rule.bzl%a2(foo=...) -> //my_pkg:rule.bzl%a1(bar=...)] ...
This means that action
X was generated by Aspect
a2 applied onto
a1(t0) is the result of Aspect
AspectDescriptor has the following format:
AspectClass could be the name of the Aspect class (for native Aspects) or
bzl_file%aspect_name (for Starlark Aspects).
sorted in topological order of the
Handling Shared Actions
Sometimes actions are
between configured targets.
In the execution phase, those shared actions are
simply considered as one and only executed once.
However, aquery operates on the pre-execution, post-analysis action graph, and hence treats these
like separate actions whose output Artifacts have the exact same
execPath. As a result,
we'll have equivalent Artifacts appearing duplicated.
The list of aquery issues/planned features can be found on GitHub.
The ActionKey remains the same even though the content of an input file changed.
In the context of aquery, the
ActionKey refers to the
String gotten from
Returns a string encoding all of the significant behaviour of this Action that might affect the output. The general contract of
getKeyis this: if the work to be performed by the execution of this action changes, the key must change. ... Examples of changes that should affect the key are: - Changes to the BUILD file that materially affect the rule which gave rise to this Action. - Changes to the command-line options, environment, or other global configuration resources which affect the behaviour of this kind of Action (other than changes to the names of the input/output files, which are handled externally). - An upgrade to the build tools which changes the program logic of this kind of Action (typically this is achieved by incorporating a UUID into the key, which is changed each time the program logic of this action changes). Note the following exception: for actions that discover inputs, the key must change if any input names change or else action validation may falsely validate.
This excludes the changes to the content of the input files, and is not to be confused with
Please contact email@example.com and firstname.lastname@example.org for any issue/feature request.