What a hit restores#
Turborepo restores "the results of your task from cache using a fingerprint from the first time the task ran", and replays that run's logs. Caching The fingerprint covers the inputs Turborepo knows about. A variable reaches it only through the env or globalEnv keys, or through framework inference for prefixes such as NEXT_PUBLIC_. Using environment variables
The same page describes two environment modes. Strict Mode, the default, passes a task only the variables listed in those keys. Loose Mode passes everything, "even though it isn't accounted for in the task hash".
Fixture: one hash, two API URLs#
Turborepo 2.11.13 and npm 11.19.1. The web package's build writes process.env.API_URL into dist/config.js. turbo.json lists no env for build. With "envMode": "loose":
$ API_URL=https://staging.example.test turbo run build --output-logs=full
web:build: cache miss, executing 1e0b3087f2fb1460
web:build: built with API_URL=https://staging.example.test
Cached: 0 cached, 1 total
[…]
$ API_URL=https://api.example.test turbo run build --output-logs=full
web:build: cache hit, replaying logs 1e0b3087f2fb1460
web:build: built with API_URL=https://staging.example.test
Cached: 1 cached, 1 total
$ cat apps/web/dist/config.js
export const API_URL = "https://staging.example.test";
The second build asked for production and got the staging file, because both runs computed the same hash. With the default Strict Mode, the variable never reached the build, so the first run wrote "(unset)", and the production run replayed that. The documentation warns about exactly this: if a build handles a missing variable gracefully, you can "still successfully complete tasks and get unintended cache hits."
Declaring the variable fixed both modes:
$ API_URL=https://api.example.test turbo run build --dry=json # printing tasks[0].hash and .environmentVariables.specified
1e0b3087f2fb1460 {"env":[],"passThroughEnv":null}
$ declare API_URL: "tasks": { "build": { "env": ["API_URL"], "outputs": ["dist/**"] } }
$ API_URL=https://api.example.test turbo run build --dry=json # printing tasks[0].hash and .environmentVariables.specified
b53bf19c1eb7ce6a {"env":["API_URL"],"passThroughEnv":null}
[…]
web:build: cache miss, executing b53bf19c1eb7ce6a
web:build: built with API_URL=https://api.example.test
Check a cached build before you ship it#
--dry=json displays the tasks that would run without running them, and in the fixture each task's entry held its hash, its hashed inputs files and an environmentVariables block. turbo run Compare two things for every deployable task:
- The variables the build reads:
process.env.*,import.meta.env.*and the build tool's own configuration. - The
environmentVariablesblock in the dry run, which lists the variables that entered the hash.
A variable in the first list and not in the second is a cache key that is missing. When the build log says cache hit, replaying logs, the values printed below it come from the run that created the entry, which may have been another branch or machine.
| You see | It covers | It cannot exclude |
|---|---|---|
cache miss, executing | The build ran here with this environment | Nothing about earlier entries |
cache hit, replaying logs | Hashed inputs matched an earlier run | Unhashed variables that differed |
A hit, and every read variable in env | The hash covered those values | Inputs read from outside the package, such as a remote config fetched at build time |
The same observation, a hit with the wrong value, also comes from a file outside the task's inputs. The dry run's inputs field lists the hashed files, so check those as well.
Go's test cache has the same limit: see go test's cached result. For build outputs restored by a GitHub Actions cache, see a restore key that brought back old output.