TExcelTAD
Home › Monorepo Builds: Bazel (+ Buck2 & Pants) › aquery: Actions & Command Lines

aquery: Actions & Command Lines

Targets are what you write; actions are what Bazel runs. A cc_binary with two sources becomes two compile actions, a link action, a strip action and some file-writing bookkeeping. bazel aquery ("action graph query") shows them, with their inputs, outputs, environment and exact command lines, without running anything. It's the tool for questions like "which flags does this file really get compiled with?" and "why did this action's cache key change?".

The actions of a binary

bash · try it yourself
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir app
cat > app/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(name = "greet", srcs = ["greet.cc"], hdrs = ["greet.h"], local_defines = ["GREETING=\\\"hi\\\""])
cc_binary(name = "app", srcs = ["main.cc"], deps = [":greet"], copts = ["-Wextra"])
EOF
touch app/greet.cc app/greet.h app/main.cc
echo "== every action of //app:app and its dependencies, by mnemonic"
bazel aquery 'deps(//app:app)' --output=summary 2>/dev/null | sed -n '/^Mnemonics:/,/^$/p' | grep '^  ' | sort
echo "== the compile action of main.cc"
bazel aquery 'mnemonic("CppCompile", //app:app)' 2>/dev/null | grep -E "^action|Mnemonic|Inputs|Outputs"
== every action of //app:app and its dependencies, by mnemonic
  CcStrip: 1
  CppArchive: 1
  CppCompile: 2
  CppLink: 2
  FileWrite: 1
  RepoMappingManifest: 1
  RunfilesTree: 1
  SolibSymlink: 1
  SourceSymlinkManifest: 1
  Symlink: 1
  SymlinkTree: 1
  TranslateBuildInfo: 2
== the compile action of main.cc
action 'Compiling app/main.cc'
  Mnemonic: CppCompile
  Inputs: [app/greet.h, app/main.cc, external/rules_cc++cc_configure_extension+local_config_cc/builtin_include_directory_paths, external/rules_cc++cc_configure_extension+local_config_cc/cc_wrapper.sh, external/rules_cc++cc_configure_extension+local_config_cc/deps_scanner_wrapper.sh, external/rules_cc++cc_configure_extension+local_config_cc/validate_static_library.sh]
  Outputs: [bazel-out/k8-fastbuild/bin/app/_objs/app/main.pic.d, bazel-out/k8-fastbuild/bin/app/_objs/app/main.pic.o]

--output=summary counts actions by mnemonic, the action type: CppCompile, CppLink, CcStrip, FileWrite, SymlinkTree… Every rule set defines its own (GoCompilePkg, Javac, PyPackageTool…). The mnemonic() function filters actions; inputs() and outputs() filter by file regex, so aquery 'inputs(".*greet\.h", deps(//app:app))' finds every action that reads a header.

The inputs list is the action's declared world: exactly the files the sandbox will contain. main.cc is compiled with greet.h available, because greet is a dependency.

Command lines, trimmed

The full command line is long. A little sed keeps the interesting part:

bash · try it yourself
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir app
cat > app/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(name = "greet", srcs = ["greet.cc"], hdrs = ["greet.h"], local_defines = ["GREETING_V2"])
cc_binary(name = "app", srcs = ["main.cc"], deps = [":greet"], copts = ["-Wextra"])
EOF
touch app/greet.cc app/greet.h app/main.cc
cmdline() { awk '/Command Line/{f=1} f{print} f && /\)$/{exit}' | tr -d "\\\\'()" | tr -s ' \n' ' '; echo; }
echo "== compile greet.cc (fastbuild)"
bazel aquery 'mnemonic("CppCompile", //app:greet)' 2>/dev/null | cmdline | grep -oE '(-std=\S+|-D\S+|-W\S+|-O\S*|-g\S*|-c \S+)' | tr '\n' ' '; echo
echo "== compile main.cc (-c opt)"
bazel aquery -c opt 'mnemonic("CppCompile", //app:app)' 2>/dev/null | cmdline | grep -oE '(-std=\S+|-D\S+|-W\S+|-O\S*|-g\S*|-c \S+)' | tr '\n' ' '; echo
echo "== link"
bazel aquery 'mnemonic("CppLink", //app:app)' 2>/dev/null | cmdline | tr ' ' '\n' | grep -E '^(-o|bazel-out|-l|-fuse)' | head -6
== compile greet.cc (fastbuild)
-Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -std=c++17 -DGREETING_V2 -c app/greet.cc -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted"
== compile main.cc (-c opt)
-Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -g0 -O2 -D_FORTIFY_SOURCE=1 -DNDEBUG -std=c++17 -Wextra -c app/main.cc -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted"
== link
-o
bazel-out/k8-fastbuild/bin/app/app
-fuse-ld=gold
bazel-out/k8-fastbuild/bin/app/_objs/app/main.pic.o
bazel-out/k8-fastbuild/bin/app/_objs/greet/greet.pic.o
-lstdc++

Things you can only see here:

  • local_defines reach only the library's compile, and the binary's copts (-Wextra) only its own.
  • Under -c opt the auto-configured toolchain adds -O2 -DNDEBUG (plus some hardening flags), and the __DATE__/__TIME__ macros are redefined to "redacted", a small part of Bazel's effort to make outputs reproducible.
  • The link line uses the gold linker (-fuse-ld=gold, chosen by the auto-configured toolchain when it is available) and lists object files from bazel-out/k8-fastbuild/bin/…: every path is relative to the execution root, so the same command runs on any machine, which is what makes remote execution possible.

Other output formats

Flag Gives
--output=text (default) human-readable, as above
--output=summary counts per mnemonic and configuration
--output=jsonproto / proto machine-readable, for diffing two builds in a script
--include_commandline=false omit command lines (much shorter)
--include_artifacts=false omit inputs/outputs

A useful trick when a cache hit unexpectedly turns into a miss: run aquery --output=jsonproto for the same target at two commits (or with two sets of flags) and diff the actionKey, arguments and inputs. Bazel's --execution_log_json_file and the newer --execution_log_compact_file record what was executed in a real build for the same purpose.

Test yourself

What does bazel aquery show that cquery doesn't?

  1. Visibility
  2. The actions with their exact command lines, inputs and outputs
  3. Remote cache statistics
  4. Test results

What is a mnemonic?

  1. A target's short name
  2. The type of an action, such as CppCompile or Javac
  3. A configuration hash
  4. A cache key

Why are paths in action command lines relative (bazel-out/...)?

  1. Shorter logs
  2. So the same command and cache key work on any machine, including remote executors
  3. Absolute paths are forbidden by gcc
  4. No reason