Skip to content
Latchkey

How to Find Affected Targets With bazel query rdeps in CI

bazel query rdeps walks the reverse dependency graph from the changed files up to every target that transitively depends on them, the affected set to build and test.

Map changed files to their owning targets, then query rdeps(//..., <those targets>) to get everything that could be impacted. Because it follows the real dependency graph, the skip is dependency-aware and safe.

Steps

  • Diff to get changed files against the base ref.
  • Map files to targets with bazel query --keep_going.
  • Expand with rdeps(//..., set(<targets>)) to include dependents.
  • Pass the result to bazel test.

Terminal

Terminal
# changed files against the PR base
FILES=$(git diff --name-only origin/main...HEAD)

# targets that own those files
DIRECT=$(bazel query --keep_going \
  "set($FILES)" 2>/dev/null)

# everything that depends on them
AFFECTED=$(bazel query --keep_going \
  "rdeps(//..., set($DIRECT))" 2>/dev/null)

bazel test $AFFECTED

Gotchas

  • Files that are not part of any target (BUILD files, .bazelrc) can change behavior globally; treat those as a full build.
  • For large graphs, tools like bazel-diff compute the affected set more precisely by hashing the action graph across two revisions.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →