Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Configure and optimize Turborepo monorepo build pipelines with correct task structure, caching, and CI setup.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/caching/RULE.md
1# How Turborepo Caching Works23Turborepo's core principle: **never do the same work twice**.45## The Cache Equation67```8fingerprint(inputs) → stored outputs9```1011If inputs haven't changed, restore outputs from cache instead of re-running the task.1213## What Determines the Cache Key1415### Global Hash Inputs1617These affect ALL tasks in the repo:1819- `package-lock.json` / `yarn.lock` / `pnpm-lock.yaml`20- Files listed in `globalDependencies` (or `global.env` when using `globalConfiguration`)21- Environment variables in `globalEnv` (or `global.env`)22- `turbo.json` configuration2324```json25{26"globalDependencies": [".env", "tsconfig.base.json"],27"globalEnv": ["CI", "NODE_ENV"]28}29```3031### Task Hash Inputs3233These affect specific tasks:3435- All files in the package (unless filtered by `inputs`)36- `package.json` contents37- Environment variables in task's `env` key38- Task configuration (command, outputs, dependencies)39- Hashes of dependent tasks (`dependsOn`)40- Files from `global.inputs` (when using `futureFlags.globalConfiguration` — see below)4142```json43{44"tasks": {45"build": {46"dependsOn": ["^build"],47"inputs": ["src/**", "package.json", "tsconfig.json"],48"env": ["API_URL"]49}50}51}52```5354### How `global.inputs` Changes the Hash Equation5556When `futureFlags.globalConfiguration` is enabled, `global.inputs` files are **not** part of the global hash. Instead, they are prepended to every task's `inputs` and folded into the **task hash**. This is a fundamental change from `globalDependencies`.5758**With `globalDependencies` (default):**5960```61task cache key = hash(global hash, task hash)62↑ includes globalDependencies file hashes63```6465Changing a `globalDependencies` file invalidates **every** task, regardless of task-level `inputs`. There is no way for a task to opt out.6667**With `global.inputs` (`futureFlags.globalConfiguration`):**6869```70task cache key = hash(global hash, task hash)71↑ includes global.inputs file hashes (merged with task inputs)72```7374`global.inputs` files are merged into each task's input globs. This means:7576- Tasks can **exclude** specific global files with negation globs: `"inputs": ["$TURBO_DEFAULT$", "!$TURBO_ROOT$/tsconfig.json"]`77- The global hash is smaller (it still includes lockfile, engines, `global.env`, etc. — but not file hashes from `global.inputs`)78- The task hash correctly includes the global input file hashes alongside the task's own inputs7980```json81{82"futureFlags": { "globalConfiguration": true },83"global": {84"inputs": ["tsconfig.json", ".env"]85},86"tasks": {87"build": {88"outputs": ["dist/**"]89},90"lint": {91"inputs": ["$TURBO_DEFAULT$", "!$TURBO_ROOT$/tsconfig.json"]92}93}94}95```9697In this example, changing `tsconfig.json` invalidates `build` (it's in the task's inputs) but **not** `lint` (which explicitly excludes it). With `globalDependencies`, both would have been invalidated.9899## What Gets Cached1001011. **File outputs** - files/directories specified in `outputs`1022. **Task logs** - stdout/stderr for replay on cache hit103104```json105{106"tasks": {107"build": {108"outputs": ["dist/**", ".next/**"]109}110}111}112```113114## Local Cache Location115116```117.turbo/cache/118├── <hash1>.tar.zst # compressed outputs119├── <hash2>.tar.zst120└── ...121```122123Add `.turbo` to `.gitignore`.124125## Cache Restoration126127On cache hit, Turborepo:1281291. Extracts archived outputs to their original locations1302. Replays the logged stdout/stderr1313. Reports the task as cached (shows `FULL TURBO` in output)132133## Example Flow134135```bash136# First run - executes build, caches result137turbo build138# → packages/ui: cache miss, executing...139# → packages/web: cache miss, executing...140141# Second run - same inputs, restores from cache142turbo build143# → packages/ui: cache hit, replaying output144# → packages/web: cache hit, replaying output145# → FULL TURBO146```147148## Key Points149150- Cache is content-addressed (based on input hash, not timestamps)151- Empty `outputs` array means task runs but nothing is cached152- Tasks without `outputs` key cache nothing (use `"outputs": []` to be explicit)153- Cache is invalidated when ANY input changes154