アクション グラフ クエリ(aquery)

問題を報告 ソースを表示

aquery コマンドを使用すると、ビルドグラフ内のアクションをクエリできます。分析後の構成済みターゲット グラフで動作し、アクション、アーティファクト、それらの関係に関する情報を公開します。

aquery は、構成されたターゲット グラフから生成されたアクション/アーティファクトのプロパティが必要な場合に役立ちます。たとえば、実際のコマンドの実行とその入力/出力/ニーモニックなどです。

このツールでは、いくつかのコマンドライン オプションを使用できます。特に、aquery コマンドは通常の 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 関数の使用

aquery 関数には、次の 3 つがあります。

  • inputs: 入力でアクションをフィルタします。
  • outputs: 出力でアクションをフィルタする
  • mnemonic: ニーモニックでアクションをフィルタする

expr ::= inputs(word, expr)

inputs 演算子は、入力ファイル名が word によって提供される正規表現と一致する expr のビルドによって生成されたアクションを返します。

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

outputs 関数と mnemonic 関数の構文は似ています。

複数の関数を組み合わせて AND 演算を行うこともできます。次に例を示します。

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

上記のコマンドは、ニーモニックが "Cpp.*" に一致し、入力がパターン ".*cpp""foo.*" に一致する //src/target_a のビルドに関連するすべてのアクションを検索します。

生成される構文エラーの例:

        $ 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 は、通常の Bazel ビルド上で実行されるため、ビルド中に使用できるオプションのセットを継承します。

Aquery オプション

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

デフォルトの出力形式(text)は人が読める形式です。機械が読み取れる形式には prototextproto、または jsonproto を使用します。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 出力の比較

aquery_differ ツールを使用すると、2 つの異なる aquery 呼び出しの出力を比較できます。たとえば、ルール定義に変更を加えて、実行中のコマンドラインが変更されていないことを確認する場合などです。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 出力の違いを返します。つまり、どのアクションがどちらかに存在し、他方には存在しないか、どのアクションが各 aquery 出力で異なるコマンドライン/入力を持つかなどを返します。上記のコマンドを実行すると、次のような結果が得られます。

  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: 比較する aquery 出力ファイル

--input_type=(proto|text_proto), default=proto: 入力ファイルの形式。prototextproto の aquery 出力がサポートされています。

--attrs=(cmdline|inputs), default=cmdline: 比較するアクションの属性。

アスペクト オン アスペクト

Aspect は相互に重なって適用できます。これらの Aspect によって生成されたアクションの aquery 出力には、Aspect パスが含まれます。これは、アクションを生成したターゲットに適用されるアスペクトのシーケンスです。

アスペクト オン アスペクトの例:

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

ti をルール ri のターゲットとします。ルール 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 Aspects の場合)です。AspectDescriptor は、依存関係グラフのトポロジ順に並べ替えられます。

JSON プロファイルとのリンク

aquery はビルドで実行されているアクションに関する情報(実行される理由、その入出力)を提供しますが、JSON プロファイルは実行のタイミングと所要時間を示します。共通要素であるアクションの主要な出力を介して、この 2 つのセットの情報を組み合わせることができます。

アクションの出力を JSON プロファイルに含めるには、--experimental_include_primary_output --noslim_profile を使用してプロファイルを生成します。スリム プロファイルは、一次出力を含めることには対応していません。デフォルトでは、アクションのプライマリ出力はクエリによって含まれます。

現在、これら 2 つのデータソースを組み合わせる正規のツールは提供していませんが、上記の情報を使用して独自のスクリプトを作成することは可能です。

既知の問題

共有アクションの処理

アクションは、構成されたターゲット間で共有されることがあります。

実行フェーズでは、これらの共有アクションは 1 つのアクションと見なされ、1 回だけ実行されます。ただし、aquery は実行前、分析後のアクション グラフで動作するため、出力アーティファクトの execPath がまったく同じである個別のアクションのように扱います。その結果、同等のアーティファクトが重複しているように見えます。

aQuery の問題と計画された機能の一覧については、GitHub をご覧ください。

よくある質問

入力ファイルの内容が変更されても、ActionKey は変わりません。

aquery のコンテキストでは、ActionKeyActionAnalysisMetadata#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 と混同しないでください。

更新

問題や機能リクエストについては、こちらから問題を報告してください。