Turborepo Caching
Improve monorepo build performance through reliable Turborepo caching.
When to Use
- You need to speed up repeated builds, tests, or lint jobs in a monorepo.
- You are setting up or validating remote cache behavior in CI.
- You need to debug low cache hit rates or stale artifact issues.
Workflow
Identify cacheable tasks. Ensure tasks are deterministic: same inputs must produce identical outputs. Exclude tasks that use timestamps, random IDs, or external APIs. Audit each task's inputs and outputs in turbo.json.
Configure inputs and outputs. Define explicit inputs (glob patterns for files that affect the task) and outputs (directories or files the task produces). Omit outputs only for tasks with no artifacts (e.g., lint). Include environment variables that affect output via globalEnv or per-task env.
Set up remote cache. Use TURBO_TOKEN and TURBO_TEAM (Vercel Remote Cache) or self-hosted options. In CI, set these as secrets. Run turbo run build --summarize to verify cache hits in the summary.
Handle environment variables. Add any env var that changes task output to globalEnv in turbo.json or to a task's env array. Common culprits: NODE_ENV, CI, VERCEL_ENV, custom feature flags. Missing env vars cause incorrect cache hits.
Add cache observability. Use turbo run <task> --dry-run=json to inspect the hash inputs. Run turbo run build --summarize and check "Cache hit, replaying output" vs "Cache miss, executing". Add a CI step that fails or warns if cache hit rate drops below a threshold.
Document invalidation rules. Document when cache is invalidated: input file changes, env changes, turbo.json changes. Ensure .turbo/cache is in .gitignore and that CI does not persist it across unrelated runs.
turbo.json Configuration Patterns
{
"$schema": "https://turbo.build/schema.json",
"globalEnv": ["NODE_ENV", "CI", "CUSTOM_ENV"],
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**", ".next/**"],
"inputs": ["src/**", "package.json", "tsconfig.json"],
"env": ["PUBLIC_API_URL"]
},
"test": {
"dependsOn": ["build"],
"outputs": ["coverage/**"],
"inputs": ["src/**", "test/**"]
},
"lint": {
"outputs": []
}
}
}
- Use
^build for internal dependency builds; use build for same-package deps.
- Keep
outputs minimal; avoid ** if a subset suffices to reduce cache size.
- Add
inputs when default (all files) is too broad and causes unnecessary misses.
Cache Debugging Commands
turbo run build --summarize - human-readable summary with cache hit/miss.
turbo run build --dry-run=json - JSON with hash inputs and task graph.
turbo run build --force - bypass cache to verify fresh output.
turbo daemon stop - stop local daemon if cache behavior seems stuck.
- Inspect
node_modules/.cache/turbo or .turbo/cache for local cache location.
Output Format
## Cache Strategy
- Tasks: build, test, lint
- Remote cache: enabled (TURBO_TEAM=team-name)
- Key inputs: src/**, package.json, tsconfig.json
- Key env vars: NODE_ENV, CI, PUBLIC_API_URL
## Config Changes
- turbo.json: added outputs for build, inputs for test
- CI: set TURBO_TOKEN, TURBO_TEAM; add --summarize to turbo run
## Validation Checklist
- [ ] Cache hits observed on repeat runs (no input changes)
- [ ] Cache miss on input change (e.g., edit src file)
- [ ] No stale artifacts in dependent tasks
- [ ] CI and local behavior consistent
- [ ] .turbo/cache in .gitignore
Common Pitfalls
Caching non-deterministic outputs. Builds that embed timestamps, process.env.GIT_SHA, or random IDs produce different outputs for same inputs. Cache replays wrong artifacts. Make outputs deterministic or exclude the task from caching.
Missing env vars in hash inputs. If NODE_ENV=production changes build output but is not in globalEnv, production and development builds can incorrectly share cache. Add all env vars that affect output.
Oversized cache artifacts. Including node_modules, .next/cache, or large generated assets bloats cache. Restrict outputs to what downstream tasks need. Use .turboignore or narrow globs.
Ignoring .turbo/cache locality. Local cache lives in .turbo/cache or node_modules/.cache/turbo. CI runners typically start fresh; remote cache must be configured for CI hits. Ensure TURBO_TOKEN and TURBO_TEAM are set in CI.
Wrong dependency graph. dependsOn errors (e.g., missing ^build for internal deps) cause tasks to run before dependencies complete, producing invalid cache entries.
Constraints
- Do not recommend caching nondeterministic task outputs.
- Keep guidance compatible with standard Turborepo workflows.
- Avoid provider-specific assumptions unless explicitly requested.
- Always include env vars that affect task output in hash inputs.
- Prefer explicit
inputs and outputs over relying on defaults.
- Document cache invalidation behavior for any new or modified tasks.
1---2name: turborepo-caching3description: Use this skill when configuring or debugging Turborepo local and remote caching, including task outputs, cache keys, CI reproducibility, cache misses, and developer build performance.4---56# Turborepo Caching78Improve monorepo build performance through reliable Turborepo caching.910## When to Use1112- You need to speed up repeated builds, tests, or lint jobs in a monorepo.13- You are setting up or validating remote cache behavior in CI.14- You need to debug low cache hit rates or stale artifact issues.1516## Workflow17181. **Identify cacheable tasks.** Ensure tasks are deterministic: same inputs must produce identical outputs. Exclude tasks that use timestamps, random IDs, or external APIs. Audit each task's `inputs` and `outputs` in `turbo.json`.19202. **Configure inputs and outputs.** Define explicit `inputs` (glob patterns for files that affect the task) and `outputs` (directories or files the task produces). Omit `outputs` only for tasks with no artifacts (e.g., lint). Include environment variables that affect output via `globalEnv` or per-task `env`.21223. **Set up remote cache.** Use `TURBO_TOKEN` and `TURBO_TEAM` (Vercel Remote Cache) or self-hosted options. In CI, set these as secrets. Run `turbo run build --summarize` to verify cache hits in the summary.23244. **Handle environment variables.** Add any env var that changes task output to `globalEnv` in `turbo.json` or to a task's `env` array. Common culprits: `NODE_ENV`, `CI`, `VERCEL_ENV`, custom feature flags. Missing env vars cause incorrect cache hits.25265. **Add cache observability.** Use `turbo run <task> --dry-run=json` to inspect the hash inputs. Run `turbo run build --summarize` and check "Cache hit, replaying output" vs "Cache miss, executing". Add a CI step that fails or warns if cache hit rate drops below a threshold.27286. **Document invalidation rules.** Document when cache is invalidated: input file changes, env changes, `turbo.json` changes. Ensure `.turbo/cache` is in `.gitignore` and that CI does not persist it across unrelated runs.2930## turbo.json Configuration Patterns3132```json33{34 "$schema": "https://turbo.build/schema.json",35 "globalEnv": ["NODE_ENV", "CI", "CUSTOM_ENV"],36 "pipeline": {37 "build": {38 "dependsOn": ["^build"],39 "outputs": ["dist/**", ".next/**"],40 "inputs": ["src/**", "package.json", "tsconfig.json"],41 "env": ["PUBLIC_API_URL"]42 },43 "test": {44 "dependsOn": ["build"],45 "outputs": ["coverage/**"],46 "inputs": ["src/**", "test/**"]47 },48 "lint": {49 "outputs": []50 }51 }52}53```5455- Use `^build` for internal dependency builds; use `build` for same-package deps.56- Keep `outputs` minimal; avoid `**` if a subset suffices to reduce cache size.57- Add `inputs` when default (all files) is too broad and causes unnecessary misses.5859## Cache Debugging Commands6061- `turbo run build --summarize` - human-readable summary with cache hit/miss.62- `turbo run build --dry-run=json` - JSON with hash inputs and task graph.63- `turbo run build --force` - bypass cache to verify fresh output.64- `turbo daemon stop` - stop local daemon if cache behavior seems stuck.65- Inspect `node_modules/.cache/turbo` or `.turbo/cache` for local cache location.6667## Output Format6869```markdown70## Cache Strategy71- Tasks: build, test, lint72- Remote cache: enabled (TURBO_TEAM=team-name)73- Key inputs: src/**, package.json, tsconfig.json74- Key env vars: NODE_ENV, CI, PUBLIC_API_URL7576## Config Changes77- turbo.json: added outputs for build, inputs for test78- CI: set TURBO_TOKEN, TURBO_TEAM; add --summarize to turbo run7980## Validation Checklist81- [ ] Cache hits observed on repeat runs (no input changes)82- [ ] Cache miss on input change (e.g., edit src file)83- [ ] No stale artifacts in dependent tasks84- [ ] CI and local behavior consistent85- [ ] .turbo/cache in .gitignore86```8788## Common Pitfalls8990- **Caching non-deterministic outputs.** Builds that embed timestamps, `process.env.GIT_SHA`, or random IDs produce different outputs for same inputs. Cache replays wrong artifacts. Make outputs deterministic or exclude the task from caching.9192- **Missing env vars in hash inputs.** If `NODE_ENV=production` changes build output but is not in `globalEnv`, production and development builds can incorrectly share cache. Add all env vars that affect output.9394- **Oversized cache artifacts.** Including `node_modules`, `.next/cache`, or large generated assets bloats cache. Restrict `outputs` to what downstream tasks need. Use `.turboignore` or narrow globs.9596- **Ignoring .turbo/cache locality.** Local cache lives in `.turbo/cache` or `node_modules/.cache/turbo`. CI runners typically start fresh; remote cache must be configured for CI hits. Ensure TURBO_TOKEN and TURBO_TEAM are set in CI.9798- **Wrong dependency graph.** `dependsOn` errors (e.g., missing `^build` for internal deps) cause tasks to run before dependencies complete, producing invalid cache entries.99100## Constraints101102- Do not recommend caching nondeterministic task outputs.103- Keep guidance compatible with standard Turborepo workflows.104- Avoid provider-specific assumptions unless explicitly requested.105- Always include env vars that affect task output in hash inputs.106- Prefer explicit `inputs` and `outputs` over relying on defaults.107- Document cache invalidation behavior for any new or modified tasks.