Skip to content
Latchkey

bazel query Command Reference

Inspect the Bazel dependency graph to scope what a change affects.

bazel query answers questions about the target graph: dependencies, reverse dependencies, and which targets a change touches. CI uses it to build and test only what changed.

What it does

bazel query evaluates a query expression against the loaded build graph and prints matching targets. Functions like deps(), rdeps(), and kind() let you find what depends on a changed file, the basis of affected-target CI.

Common flags and usage

  • 'deps(//path:target)': targets a target depends on
  • 'rdeps(//..., //path:lib)': targets that depend on a target
  • --output=label|package|graph: output format
  • kind('.*_test', //...): filter by rule kind

Example

shell
# Find and test only what depends on a changed library
TARGETS=$(bazel query 'rdeps(//..., //lib:core)')
bazel test --config=ci ${TARGETS}

In CI

Use rdeps to compute the affected test targets for a PR and run only those, cutting CI time on large monorepos. query reads the graph without building, so it is fast and side-effect free.

Key takeaways

  • query inspects the target graph without building anything.
  • rdeps finds reverse dependencies for affected-target CI.
  • Selective build-and-test from query results cuts monorepo CI time.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →