Skip to content

Bazel Query Examples

This guide demonstrates how to use bazel query and bazel aquery to explore the build graph.

Basic Queries

List all targets in the workspace

bazel query //...

Output:

//go_service:go_service_test
//go_service:server
//py_service:py_service_test
//py_service:server
//tests:test_go_service
//tests:test_py_service

List targets in a specific package

bazel query //go_service:all

Find all test targets

bazel query 'kind(test, //...)'

Find all Python targets

bazel query 'kind(py_.*, //...)'

Find all Go targets

bazel query 'kind(go_.*, //...)'

Dependency Queries

Show all dependencies of a target

bazel query 'deps(//go_service:server)'

This shows everything the Go server depends on, including: - Source files - Toolchains - External dependencies

Show only direct dependencies

bazel query 'deps(//go_service:server, 1)'

The 1 limits the depth to immediate dependencies only.

Find what depends on a target (reverse dependencies)

bazel query 'rdeps(//..., //go_service:server)'

This shows all targets that depend on the Go server.

Show path between two targets

bazel query 'somepath(//py_service:server, @pip_deps//:fastapi)'

Visualization

Generate a dependency graph

bazel query 'deps(//go_service:server)' --output=graph > go_deps.dot

Then convert to image (requires Graphviz):

dot -Tpng go_deps.dot -o go_deps.png

Generate graph for entire workspace

bazel query '//...' --output=graph > workspace.dot
dot -Tpng workspace.dot -o workspace.png

Action Queries (Advanced)

aquery shows the actual actions (compile, link, etc.) that Bazel executes.

Show all actions for building a target

bazel aquery //go_service:server

Show only compilation actions

bazel aquery 'mnemonic("GoCompile", //go_service:server)'

Show linking actions

bazel aquery 'mnemonic("GoLink", //go_service:server)'

Show actions with inputs and outputs

bazel aquery //go_service:server --output=text

Practical Examples

Find all binary targets (runnable services)

bazel query 'kind(".*_binary", //...)'

Output:

//go_service:server
//py_service:server

Find targets that changed since last commit

bazel query --universe_scope=//... \
  --order_output=no \
  "set(//...)" --output=label

Check if target exists

bazel query //py_service:server &>/dev/null && echo "Target exists" || echo "Target not found"

Find unused dependencies (requires analysis)

# This is more complex and requires additional tooling
bazel query "rdeps(//..., //some:target, 1)" --output=label_kind

Filtering and Formatting

Output as a list

bazel query '//...' --output=label

Output as JSON

bazel query '//...' --output=jsonproto

Output with kind (target type)

bazel query '//...' --output=label_kind

Example output:

go_binary rule //go_service:server
py_binary rule //py_service:server
go_test rule //go_service:go_service_test
py_test rule //py_service:py_service_test

Query Functions Cheatsheet

Function Description Example
deps(target) All dependencies deps(//go_service:server)
rdeps(universe, target) Reverse dependencies rdeps(//..., //py_service:server)
allpaths(from, to) All paths between targets allpaths(//go_service:server, @rules_go//:go)
somepath(from, to) One path between targets somepath(//go_service:server, @rules_go//:go)
kind(pattern, input) Filter by target type kind(test, //...)
attr(name, pattern, input) Filter by attribute attr(srcs, ".*\.py", //...)
labels(attr, input) Extract label values labels(srcs, //go_service:server)
filter(pattern, input) Filter labels by regex filter("test", //...)

Performance Tips

Use --universe_scope for large queries

bazel query 'allpaths(//service:a, //service:b)' --universe_scope=//service/...

This limits the search space, making queries faster.

Use --nohost_deps to skip host dependencies

bazel query 'deps(//go_service:server)' --nohost_deps

Use --notool_deps to skip tool dependencies

bazel query 'deps(//go_service:server)' --notool_deps

Debugging Build Issues

Find why a target is built

bazel query 'rdeps(//..., //some:target)' --output=graph | dot -Tpng > why_built.png

Check for circular dependencies

bazel query 'somepath(//pkg:target, //pkg:target)'
# If output is found, there's a cycle

List all external dependencies

bazel query 'kind("http_archive", @*//...)'

Further Reading