cquery: The Configured Graph
bazel query answers questions about what could be built. bazel cquery ("configurable query") answers questions about what is built with the flags you pass: it runs the loading and analysis phases, resolves every select(), applies configuration transitions, and then evaluates the same query language on the configured target graph.
select() makes the difference
select() chooses attribute values by configuration (the platforms lesson covers it in depth). Here the binary links a fast or a debug allocator depending on --compilation_mode:
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir -p app alloc
cat > alloc/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_library")
config_setting(name = "opt", values = {"compilation_mode": "opt"})
cc_library(name = "fast", hdrs = ["fast.h"], visibility = ["//visibility:public"])
cc_library(name = "debug", hdrs = ["debug.h"], visibility = ["//visibility:public"])
EOF
cat > app/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "app",
srcs = ["main.cc"],
deps = select({
"//alloc:opt": ["//alloc:fast"],
"//conditions:default": ["//alloc:debug"],
}),
)
EOF
touch alloc/fast.h alloc/debug.h
echo 'int main() { return 0; }' > app/main.cc
echo "== query: every branch"
bazel query --noimplicit_deps 'filter("//alloc:(fast|debug)quot;, deps(//app))' 2>/dev/null
echo "== cquery: the default configuration"
bazel cquery --noimplicit_deps 'filter("//alloc:(fast|debug)quot;, deps(//app))' 2>/dev/null
echo "== cquery -c opt"
bazel cquery -c opt --noimplicit_deps 'filter("//alloc:(fast|debug)quot;, deps(//app))' 2>/dev/null== query: every branch
//alloc:debug
//alloc:fast
== cquery: the default configuration
//alloc:debug (0a96350)
== cquery -c opt
//alloc:fast (2e9f273)
query reports both allocators, because it can't know which branch you'll build. cquery reports exactly one, followed by the configuration id in parentheses: a short hash of all the build options. -c opt gives a different id, because it's a different configuration with its own output directory.
Configurations you can see
One build often contains several configurations: the target configuration for the code you ship and an exec configuration for tools that run during the build (code generators, compilers built from source). The same target can appear in both:
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir gen
cat > gen/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(name = "util", hdrs = ["util.h"])
cc_binary(name = "codegen", srcs = ["codegen.cc"], deps = [":util"])
genrule(name = "table", outs = ["table.h"], tools = [":codegen"], cmd = "$(execpath :codegen) > $@")
cc_binary(name = "app", srcs = ["app.cc", ":table"], deps = [":util"])
EOF
touch gen/util.h
printf '#include <cstdio>\nint main() { std::puts("#define TABLE_SIZE 64"); }\n' > gen/codegen.cc
printf '#include "gen/table.h"\nint main() { return TABLE_SIZE == 64 ? 0 : 1; }\n' > gen/app.cc
bazel cquery --noimplicit_deps 'filter("^//gen", deps(//gen:app))' 2>/dev/null
echo "== the configurations"
bazel config 2>/dev/null | sort -k2//gen:app (0a96350)
//gen:table (0a96350)
//gen:app.cc (null)
//gen:util (0a96350)
//gen:codegen (a0bd5d0)
//gen:util.h (null)
//gen:util (a0bd5d0)
//gen:codegen.cc (null)
== the configurations
Available configurations:
3030ed4c810cf3435d066294fa495e650fe75173e4b4ef410ea56cb683458569 fastbuild-noconfig (test-trimmed)
89e16a55bf3dd865e7bbd094bbefc4c013c9ab2ec5f5eb39fb82a391ade48bae k8-fastbuild
0a9635087e2e2876e64d4f89a8a25cbc1b4d9e2771d41913cb9abe91bb6dec73 k8-fastbuild (test-trimmed)
a0bd5d08bfe64fbbacb6cb4a475aa418d3f62ea195e30f52e91a50eb5b27d235 k8-opt-exec (exec)
//gen:util appears twice: once in the target configuration (linked into app) and once in the exec configuration (linked into codegen, which runs on the build machine). On a cross-compiling build those really are two different compiles, for two CPUs. bazel config lists the configurations of the last build: k8-opt-exec (exec) is the tool configuration (tools are built optimized by default), and the (test-trimmed) variants are copies with test-only options removed, which Bazel uses for non-test targets so that changing a test flag doesn't invalidate them; bazel config <id> prints every option of one, and bazel config <id1> <id2> diffs two, which is how you find out why a target is built twice.
Starlark output: asking the analysis results
--output=starlark evaluates a small Starlark expression on each configured target, with access to its providers (the data rules pass to each other, see Providers & depsets):
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir lib
cat > lib/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(name = "base", srcs = ["base.cc"], hdrs = ["base.h"], defines = ["BASE_V2"])
cc_library(name = "net", srcs = ["net.cc"], hdrs = ["net.h"], deps = [":base"])
EOF
touch lib/base.cc lib/base.h lib/net.cc lib/net.h
echo "== output files"
bazel cquery //lib:net --output=files 2>/dev/null
echo "== headers and defines seen by dependents of //lib:net"
bazel cquery //lib:net --output=starlark \
--starlark:expr='"headers=%s defines=%s" % (sorted([f.path for f in providers(target)["@@rules_cc+//cc/private:cc_info.bzl%CcInfo"].compilation_context.headers.to_list()]), providers(target)["@@rules_cc+//cc/private:cc_info.bzl%CcInfo"].compilation_context.defines.to_list())' 2>/dev/null== output files
bazel-out/k8-fastbuild/bin/lib/libnet.a
bazel-out/k8-fastbuild/bin/lib/libnet.so
== headers and defines seen by dependents of //lib:net
headers=["lib/base.h", "lib/net.h"] defines=["BASE_V2"]
--output=files lists the files a target produces in this configuration (what bazel build would print). The Starlark expression dug into CcInfo, the C++ provider, and shows that net's dependents see both headers and base's define. This is the quickest way to debug "why is this flag / header / file on my command line?" without writing a rule.
Note: The key
@@rules_cc+//cc/private:cc_info.bzl%CcInfois the provider's canonical identity, as printed by--starlark:expr='providers(target)'. It's long, but it's exact.
When to use which
- query for repository-wide structure and impact analysis (fast, configuration-free, works on targets that don't even build for your platform).
- cquery when
select(), platforms or transitions matter, or when you need analysis results (output files, providers). - aquery when you need the actual commands: the next lesson.
Test yourself
A binary's deps use select(). Which command shows only the branch chosen for -c opt?
- bazel query -c opt
- bazel cquery -c opt
- bazel build --query
- bazel mod graph
Why can the same library appear twice in cquery output?
- A bug in the BUILD file
- It is built in two configurations, e.g. target and exec (for a tool used during the build)
- It has two names
- The cache is stale
Which cquery option lists the files a target produces?
- --output=build
- --output=files
- --output=label_kind
- --output=graph