How-To Guide

How to Implement an Affected Task Graph

Build an affected-only task graph for a monorepo so CI runs only the tasks a change requires, using git diff and package dependencies.

On this page

  1. Introduction
  2. Why Affected-Only Matters
  3. Finding the Changed Packages
  4. Walking the Dependency Graph
  5. Mapping Affected Packages to Tasks
  6. Correctness Edge Cases
  7. Combining with Caching
  8. Common Pitfalls and Conclusion

Introduction

Running every task in a monorepo on every pull request wastes minutes and, at scale, hours. Most changes affect a few packages, so the goal is to run only the tasks whose inputs changed and the tasks that depend on them. That is an affected task graph: start from the changed files, map them to the packages that own them, walk the dependency graph to include dependents, and run only those tasks. This tutorial builds that graph from a git diff and the package dependency structure.

Why Affected-Only Matters

A monorepo with a hundred packages running the full pipeline on every change pays for work unrelated to the change: minutes of CI, cache storage, and developer waiting time. A change to one package’s source usually affects only that package and its dependents, so running the rest is pure waste.

Affected-only execution is also feedback quality: a developer who changed one package sees only that package’s results, so a failure is clearly theirs and the run is fast enough to run on every push. The alternative, a long full run, is run less often and read less.

It also makes the cache more effective, because a task that is not affected is not even scheduled, so there is no question of whether to restore it. The affected graph is a filter before the cache, and the two compose to make a change cost roughly what the change is worth.

# affected-only flow
git diff main...HEAD  ->  changed files
changed files          ->  owning packages (by path)
owning packages        ->  + their dependents (walk the dep graph)
that set               ->  run only their tasks
# everything else is unchanged => skip (or restore from cache)

Finding the Changed Packages

The first step is the diff: files changed between the merge base of the target branch and the current commit, so the comparison is against where the branch diverged, not against the moving tip. Then map each changed file to the package whose directory contains it, using the workspace globs.

Map by the longest matching package directory, because a file might sit under several globs. Files outside any package, such as shared config or CI files, should be treated as affecting everything, so a change to a shared tsconfig or a CI workflow does not skip tests that depend on it.

# files changed since the branch diverged from main
BASE=$(git merge-base HEAD origin/main)
git diff --name-only "$BASE"...HEAD
# then map each path to its owning package by the package directory,
# and treat shared/root files (tsconfig, CI config) as affecting all

Walking the Dependency Graph

Once you know the changed packages, include their dependents: any package that depends, directly or transitively, on a changed package must be rebuilt or retested, because the changed package’s output is an input to it. This is a reverse dependency walk from the changed set.

Read the graph from each package’s dependencies, matching workspace names, and build a reverse adjacency list. Then do a breadth-first search from the changed packages to collect all dependents. The result is the affected set; the complement is skipped.

type Graph = Map<string, Set<string>>; // pkg -> its direct dependents

export function affected(changed: string[], dependentsOf: Graph): Set<string> {
  const result = new Set<string>();
  const queue = [...changed];
  while (queue.length) {
    const pkg = queue.shift()!;
    if (result.has(pkg)) continue;
    result.add(pkg);
    for (const dep of dependentsOf.get(pkg) ?? []) queue.push(dep);
  }
  return result;                            // changed packages + all dependents
}

Mapping Affected Packages to Tasks

Each affected package has a set of tasks (build, test, lint). Run those tasks in dependency order, which the task runner computes from the same graph. The union of the affected packages’ tasks, ordered by the package graph, is the affected task graph.

Tasks also have internal dependencies: a package’s test depends on its build, so order matters within a package too. The task runner’s own dependency declarations (such as dependsOn in Turborepo) handle this, so the affected set feeds the runner and the runner orders the tasks correctly.

# feed the affected set to the task runner (Turborepo example):
turbo run build test lint --filter="...[origin/main]"
# "...[origin/main]" expands to changed packages + their dependents
# the runner then orders the tasks by the package graph

Correctness Edge Cases

Changes outside any package must not be silently ignored. A change to the root tsconfig, a shared CI workflow, a lockfile or a global tool affects every package, so it should widen the affected set to all. Detect these root-level changes and treat them as affecting everything.

A change to a workspace dependency version or the lockfile similarly affects many packages, because the resolved dependency tree changed. Treat lockfile and workspace-manifest changes as global, or compute which packages’ resolved trees changed. Getting this wrong makes CI pass while a real breakage rides in with a shared change.

# treat as GLOBAL (affect all packages)
[ ] root tsconfig / build config
[ ] lockfile (pnpm-lock.yaml) changes
[ ] CI workflow files
[ ] shared tooling versions
[ ] workspace manifest (packageManager, catalogs)
# otherwise a shared break ships while CI passes

Combining with Caching

An affected set reduces the work; a cache makes the remaining work fast when it repeats. Together, a pull request rebuilds only the affected packages, and a package that was built earlier in the same branch reuses its cached output. The affected graph decides what to consider; the cache decides what actually reruns.

For the release or main branch, run the full graph but rely on the cache to make unchanged tasks instant. The two regimes cover different needs: affected-only for fast PR feedback, full with cache for release correctness.

# two regimes
PR:    affected-only (fast feedback) + cache for repeats within the branch
main:  full graph + remote cache (unchanged tasks are instant)
# affected decides what to CONSIDER; cache decides what actually RE-RUNS

Common Pitfalls and Conclusion

Do not compare against the moving tip of the target branch; use the merge base, or you will include unrelated changes. Do not ignore root and lockfile changes, or a shared break slips through. Do not map files to packages by a shallow prefix; use the longest matching package directory. And do not run affected-only on the release branch, where correctness needs the full graph.

An affected task graph runs only what a change requires: map changed files to packages, walk the dependency graph to include dependents, order the tasks by the package graph, treat root and lockfile changes as global, and combine with a cache. Then CI time tracks the change’s size, not the repository’s, while the release still validates everything.

Sources: Turborepo: Filtering, Nx: Affected tasks, git merge-base.

FAQ

What git comparison should I use for the diff?
The merge base of the target branch and the current commit, so the comparison is against where the branch diverged, not the moving tip. Comparing against the tip includes unrelated changes and can falsely widen the affected set.
What changes should be treated as affecting everything?
Files outside any package: the root tsconfig or build config, CI workflows, the lockfile, shared tooling versions and workspace manifests. A change to any of these can affect every package, so the affected set becomes all packages.

Related Examples