Bazel has a lot of commands. Most of them you’ll never touch. Here are the ones I reach for constantly when I’m trying to figure out what’s going on in a BUILD file.
“What targets exist here?”
bazel query '//...'
Flat list. Not that useful on its own. Add --output=label_kind to see what each thing actually is:
bazel query 'kind(".*", //...)' --output=label_kind
pkg_tar_impl rule //image:app_layer
oci_image rule //image:hello_image
oci_load rule //image:hello_tarball
_write_file rule //image:hello_tarball_write_tags # internal, ignore
Now you can tell the real targets from the auto-generated ones.
“What does this target produce?”
bazel cquery --output=files //image:app_layer
# bazel-out/darwin_arm64-fastbuild/bin/image/app_layer.tar
This gives you the actual file path on disk. You can tar -tf it, cat it, whatever. This is the command I use most.
“What does this target depend on?”
bazel query 'deps(//image:app_layer, 1)'
The 1 means direct deps only. Without it you get the entire transitive tree — toolchains, Starlark files, everything. You almost never want that.
“If I change this file, what rebuilds?”
bazel query 'rdeps(//..., //app:main.py)'
This is the Bazel superpower. Reverse deps. Change main.py and Bazel tells you exactly: app_layer -> hello_image -> hello_tarball. Nothing else.
“Show me the full rule definition”
bazel query '//image:app_layer' --output=build
This expands all defaults and shows you every attribute. Super useful when you’re trying to figure out what a macro is actually generating under the hood.
pkg_tar_impl(
name = "app_layer",
package_dir = "/app",
srcs = ["//app:main.py"],
out = "//image:app_layer.tar", # <-- there's the output
)
“What can I actually run?”
bazel query 'kind(".*_binary|.*_test|oci_load|sh_binary|py_binary", //...)'
Not everything is runnable. bazel build works on any target, but bazel run only works on targets that produce an executable.
The Reference Card
| What | Command |
|---|---|
| List all targets | bazel query '//...' |
| List with types | query 'kind(".*", //...)' --output=label_kind |
| Show output files | cquery --output=files //target |
| Direct deps | query 'deps(//target, 1)' |
| Reverse deps | query 'rdeps(//..., //target)' |
| Full rule definition | query '//target' --output=build |
| Dep graph (graphviz) | query 'deps(//target)' --output=graph |
| Build | bazel build //target |
| Build + execute | bazel run //target |
| Build everything | bazel build //... |