Learning center

Turborepo replayed a build made with another API URL

On a cache hit, Turborepo restores the task's outputs and replays its logs from the run that created the entry. An environment variable you did not list in the task's env is not part of the hash, so a production build can be served the files of a staging build. The replayed log even repeats the staging value. Before you ship a cached build, check that every variable the build reads is in the hash, with turbo run build --dry=json.

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:

  1. The variables the build reads: process.env.*, import.meta.env.* and the build tool's own configuration.
  2. The environmentVariables block 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 seeIt coversIt cannot exclude
cache miss, executingThe build ran here with this environmentNothing about earlier entries
cache hit, replaying logsHashed inputs matched an earlier runUnhashed variables that differed
A hit, and every read variable in envThe hash covered those valuesInputs 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.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading