Restore Turborepo build files with outputs
Configure Turborepo to restore package build files on a cache hit, and check whether a missing directory points to outputs or inputs.
You run the build on a clean checkout and Turborepo reports a cache hit. The log even says it built dist/index.js. The next job fails when it tries to read that file. Start by checking what the cache hit restored.
Tell a hit from a restore
Use this check in a workspace where packages/ui has a build script that writes dist/index.js. Start with an empty local cache. The commands use local caching only. The first command builds the package and warms the cache. Between runs, move the generated directory out of the checkout so you can tell if the second run puts it back.
pnpm exec turbo run build --filter=@demo/ui --cache=local:rw
saved_build=$(mktemp -d)
mv packages/ui/dist "$saved_build/"
pnpm exec turbo run build --filter=@demo/ui --cache=local:rw
ls packages/ui/distWith no matching output glob, you can see cache hit, replaying logs followed by No such file or directory from ls.
Turborepo looks up a cache entry by hash. With caching enabled, the entry can contain the build log even when it holds no generated files. Seeing the build script's message again tells you what the script printed on the original run. Check the directory to find out what the cache returned this time.
diagram
What a cache hit returns
Name the files to keep
Add an outputs glob to the build task in the root turbo.json. In a workspace with other task settings, merge this field into the existing build task.
{
"tasks": {
"build": {
"outputs": ["dist/**"]
}
}
}Repeat the check. Changing the task definition gives the first run a new cache key. The second run should restore index.js under packages/ui/dist.
Turborepo resolves dist/** from the package that ran the task. Use that glob when all your packages write to their own dist directories. If just the UI package uses that layout, put the glob on its @demo/ui#build task. A path such as packages/ui/dist/** would look for that whole path inside the UI package.
Check the hash on a miss
If the second run says cache miss, compare the hashes printed next to the task. A different hash means you have a change in the inputs to the cache lookup. Start with files the build itself touched. A generated directory can change the next hash if Turborepo counts those files as inputs. With the default inputs, ignore dist/ in Git to keep those files out of the hash.
Keep generated files out of the input set and keep the files you need in outputs. If the hash stays the same and the second run still leaves the directory empty, compare the output glob with the files on disk after a fresh build.