Skip to content

Lesson 4 of 9 · 27m

Affected-Graph Builds and Correct Cache Keys

Use dependency graphs and content hashing to build only what changed, while understanding exactly what must invalidate a task.

Affected builds are the monorepo superpower and the monorepo scandal. Done correctly, a pull request touching one button tests the design system and the two applications that consume it. Done incorrectly, CI silently skips the package that reads an environment variable nobody modeled. The goal is not merely speed. The goal is trustworthy minimality.

Start with a graph you can inspect

Turborepo and Nx infer task dependencies from workspace dependencies and task configuration. Bazel requires more explicit target definitions. Either way, you need commands that answer "why?"

turbo run build --filter='...[HEAD^]' --dry=json
nx affected:graph --base=origin/main --head=HEAD
bazel query 'rdeps(//..., //packages/ui:ui)'

Do not treat the graph as an implementation detail. Engineers should be able to inspect why a target ran and why another target did not. If nobody can explain the graph, they will respond by running everything forever.

A cache key is a claim about reality

A task cache entry says: "these inputs always produce these outputs." Inputs include more than source files:

source files
dependency versions
lockfile
compiler version
environment variables
generated inputs
task configuration
operating system / architecture, when relevant

A simplified Turborepo configuration makes some of this explicit:

{
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "inputs": ["src/**", "package.json", "tsconfig.json"],
      "outputs": ["dist/**"],
      "env": ["NODE_ENV", "API_SCHEMA_VERSION"]
    }
  }
}

If DATABASE_URL only affects tests, it belongs in test configuration, not global build invalidation. If STRIPE_SECRET_KEY does not affect output, it must never become a cache input. Secrets do not belong in cache keys.

Affected detection needs a trustworthy base

Pull request CI normally compares against the target branch. The dangerous mistakes are shallow clones, incorrect merge-base selection, and using the last successful build as a base without understanding what it means:

git fetch origin main --depth=200
git merge-base origin/main HEAD

If the merge base is wrong, affected detection is wrong. This is why CI should log the base commit, head commit, affected projects, and task hashes. Debugging skipped work without those facts is archaeology.

Local correctness before distributed cleverness

Before enabling remote caching, prove local cache correctness. Run the same task twice, mutate inputs, and verify expected invalidation:

turbo run build --filter=@acme/web
turbo run build --filter=@acme/web

The second run should restore or hit cache. Then change a source file, a dependency version, and an environment variable one at a time. The first should invalidate consumers; the second should invalidate appropriately; the third depends on whether that variable truly affects output. Do not skip this because the vendor dashboard looks easy.

Know when affected builds are unsafe

Affected builds become dangerous when the graph cannot see behavior. Hidden reads of shared mutable infrastructure, dynamic runtime configuration, unmodeled generated files, and tests dependent on external state all create false negatives. The mitigation is not fear; it is explicit modeling or conservative invalidation around the unknown boundary.

Field notes

  • Fast but wrong CI is worse than slow CI because it trains teams to merge broken changes confidently.
  • Cache poisoning is real. Treat remote cache writes as privileged and never cache artifacts produced with secret material baked in.
  • Hidden inputs are usually discovered after a production incident. Add them to task configuration the same day, not in a quarterly cleanup.
  • A nightly full build is a useful backstop, but it is not a license to accept incorrect pull request validation.

Was this lesson helpful?

Sign in to rate