操作图查询 (aquery)

报告问题 查看源代码

您可以使用 aquery 命令查询构建图中的操作。该 API 会针对后分析配置目标图执行操作,并公开有关操作、工件及其关系的信息。

如果您对通过配置的目标图表生成的操作/工件的属性感兴趣,aquery 非常有用。例如,实际命令运行及其输入/输出/助记符。

该工具接受多个命令行选项。 值得注意的是,query 命令在常规 Bazel 构建之上运行,并继承构建期间可用的一组选项。

它支持同样适用于传统 query 的一组函数,但 siblingsbuildfilestests 除外。

aquery 输出示例(不含具体详细信息):

$ bazel aquery 'deps(//some:label)'
action 'Writing file some_file_name'
  Mnemonic: ...
  Target: ...
  Configuration: ...
  ActionKey: ...
  Inputs: [...]
  Outputs: [...]

基本语法

以下是 aquery 语法的一个简单示例:

bazel aquery "aquery_function(function(//target))"

查询表达式(用引号括起来)包括以下内容:

  • aquery_function(...):特定于 aquery 的函数。如需了解详情,请参阅下文
  • function(...):与传统 query 一样的标准函数
  • //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))'

使用查询函数

有三个 aquery 函数:

  • inputs:按输入过滤操作。
  • outputs:按输出过滤操作
  • mnemonic:按助记符过滤操作

expr ::= inputs(word, expr)

inputs 运算符会返回构建 expr 时生成的操作,其输入文件名与 word 提供的正则表达式匹配。

$ bazel aquery 'inputs(".*cpp", deps(//src/target_a))'

outputsmnemonic 函数具有相似的语法。

您还可以组合函数来实现 AND 运算。例如:

  $ bazel aquery 'mnemonic("Cpp.*", (inputs(".*cpp", inputs("foo.*", //src/target_a))))'

以上命令将查找构建 //src/target_a 时涉及的所有操作,这些助记符与 "Cpp.*" 匹配,而输入与 ".*cpp""foo.*" 格式匹配。

生成的语法错误示例:

        $ 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))

选项

build 选项

aquery 在常规 Bazel 构建上运行,因此会继承构建期间可用的一组选项

查询选项

--output=(text|summary|proto|jsonproto|textproto), default=text

默认输出格式 (text) 是人类可读的格式,使用 prototextprotojsonproto 表示机器可读的格式。proto 消息为 analysis.ActionGraphContainer

--include_commandline, default=true

在输出中包含操作命令行的内容(可能会很大)。

--include_artifacts, default=true

包括操作的输出和输出的输出名称(可能会很大)。

--include_aspects, default=true

是否在输出中包含宽高比生成的操作。

--include_param_files, default=false

添加命令中使用的参数文件的内容(可能会比较大)。

--include_file_write_contents, default=false

添加 actions.write() 操作的文件内容和 SourceSymlinkManifest 操作清单文件的内容。文件内容在 file_contents 字段中返回,格式为 --output=xxxproto。使用 --output=text 时,输出有 FileWriteContents: [<base64-encoded file contents>]

--skyframe_state, default=false

在不执行额外分析的情况下,从 Skyframe 中转储操作图。

其他工具和功能

查询 Skyframe 状态

Skyframe 是 Bazel 的评估和增量模型。在 Bazel 服务器的每个实例中,Skyframe 会存储根据先前的分析阶段运行所构造的依赖关系图。

在某些情况下,查询 Skyframe 上的操作图会很有用。用例示例:

  1. 运行 bazel build //target_a
  2. 运行 bazel build //target_b
  3. 文件 foo.out 已生成。

作为 Bazel 用户,我想确定 foo.out 是通过构建 //target_a 还是 //target_b 生成的。

您可以运行 bazel aquery 'outputs("foo.out", //target_a)'bazel aquery 'outputs("foo.out", //target_b)' 来找出负责创建 foo.out 的操作,进而确定目标。但是,之前构建的不同目标的数量可能大于 2,这使得运行多个 aquery 命令变得非常容易。

或者,您也可以使用 --skyframe_state 标志:

  # 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")'

使用 --skyframe_state 模式时,aquery 会接受 Skyframe 在 Bazel 实例上保留的操作图的内容,可以选择对其执行过滤并输出内容,而无需重新运行分析阶段。

特殊注意事项

输出格式

--skyframe_state 目前仅适用于 --output=proto--output=textproto

未在查询表达式中添加目标标签

目前,--skyframe_state 会查询 Skyframe 中存在的整个操作图,无论目标是什么。在查询中指定目标标签和 --skyframe_state 会被视为语法错误:

  # 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")'

比较查询输出

您可以使用 aquery_differ 工具比较两个不同的查询调用的输出。例如:当您对规则定义进行一些更改,并希望验证正在运行的命令行是否发生更改时。aquery_differ 就是实现此目的的工具。

该工具位于 bazelbuild/bazel 代码库中。如需使用该工具,请将代码库克隆到本地机器。用法示例:

  $ bazel run //tools/aquery_differ -- \
  --before=/path/to/before.proto \
  --after=/path/to/after.proto \
  --input_type=proto \
  --attrs=cmdline \
  --attrs=inputs

上面的命令返回查询输出的 beforeafter 之间的差异:哪些操作存在于其中一个中,而哪些操作在每个查询输出中具有不同的命令行/输入...,运行以上命令的结果是:

  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:要比较的查询输出文件

--input_type=(proto|text_proto), default=proto:输入文件的格式。为 prototextproto 查询输出提供支持。

--attrs=(cmdline|inputs), default=cmdline:要比较的操作的属性。

宽高比

各个主题可以彼此重叠。然后,这些方面生成的操作的查询输出将包含宽高比路径,这是应用于生成相应目标的目标的一系列顺序。

宽高比示例:

  t0
  ^
  | <- a1
  t1
  ^
  | <- a2
  t2

让我们 ei 成为规则 ri 的一个目标,这会将构面 ai 应用于其依赖项。

假设 a2 在应用于目标 t0 时生成了操作 X,操作 X 的 bazel aquery --include_aspects 'deps(//t2)' 文本输出将如下所示:

  action ...
  Mnemonic: ...
  Target: //my_pkg:t0
  Configuration: ...
  AspectDescriptors: [//my_pkg:rule.bzl%**a2**(foo=...)
    -> //my_pkg:rule.bzl%**a1**(bar=...)]
  ...

这意味着,操作 X 由应用于 a1(t0) 的宽高比 a2 生成,其中 a1(t0) 是应用于目标 t0 的宽高比 a1 的结果。

每个 AspectDescriptor 都具有以下格式:

  AspectClass([param=value,...])

AspectClass 可以是 Aspect 类的名称(针对本机 Aspect)或 bzl_file%aspect_name(针对 Starlark Aspect)。AspectDescriptor 按照依赖关系图的拓扑顺序排序。

与 JSON 配置文件关联

虽然查询提供了有关在 build 中运行的操作(为什么它们正在运行、其输入/输出的原因)的信息,但 JSON 配置文件可以告诉我们它们的执行时间和时长。可以通过一项共同标准将两组信息相结合:操作的主要输出。

如需在 JSON 配置文件中包含操作的输出,请使用 --experimental_include_primary_output --noexperimental_slim_json_profile 生成该配置文件。精简配置文件无法包含主要输出。操作的主要内容默认包含在查询中。

我们目前尚未提供用于整合这两种数据源的规范工具,但您应该能够使用上述信息构建自己的脚本。

已知问题

处理共享操作

有时,操作会在配置的目标之间共享。

在执行阶段,这些共享操作会被视为一次,并且仅执行一次。但是,查询对执行前、分析后的操作图执行操作,因此将它们视为单独的操作,其输出工件具有完全相同的 execPath。因此,等效的工件看起来是重复的。

您可以在 GitHub 上找到查询问题/计划推出的功能的列表。

常见问题解答

即使输入文件的内容发生更改,ActionKey 也会保持不变。

在查询的上下文中,ActionKey 引用了从 ActionAnalysisMetadata#getKey 获取的 String

  Returns a string encoding all of the significant behaviour of this Action that might affect the
  output. The general contract of `getKey` is 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.

这会排除对输入文件内容所做的更改,并且不会与 RemoteCacheClient#ActionKey 混淆。

更新

如有任何问题/功能请求,请在此处提交问题。