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 $AFFECTEDGotchas
- 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
How to Test Only Changed Targets With Pants --changed-since in CIUse the Pants build system --changed-since flag in CI to test only targets affected by a diff, with --changed…
How to Cache Build Outputs for Incremental CIPersist build outputs (compiler caches, build directories) across CI runs with actions/cache so even affected…
How to Add a Force Full Build Option to Incremental CIGive incremental CI an escape hatch to run the full build on demand via workflow_dispatch input or a commit m…