Turbopack: recover from an unreadable persistent cache at startup (#101954)
### What?
Turbopack's persistent cache now recovers from two kinds of external
damage at startup:
- **A referenced `.sst` file is missing** (deleted or lost outside
Turbopack). Before, `next dev` printed `Ready` and then the first route
that needed the file panicked (`Failed to restore … Unable to open
static sorted file referenced from 000000NN.meta … No such file or
directory`), and the server exited. Now the database fails to open, that
version's cache directory is deleted, and Turbopack starts with an empty
cache. It warns: *"Turbopack's filesystem cache has been deleted because
it could not be read…"*.
- **Dot-prefixed files in the cache directory**, such as the `._<name>`
AppleDouble sidecars macOS writes on exFAT/FAT32/network volumes.
Before, every start failed with `Failed to open database: invalid digit
found in string`, and the error didn't name the file. Now these files
are ignored and the cache is kept.
### Why?
The persistent cache is only an optimization, so damage that happened
outside Turbopack should cost a cold start, not a crash. The missing-SST
case reproduces on 16.3.0, 16.4.1, 16.4.2 and `16.5.0-canary.53`. It is
longstanding, not a recent regression.
- The existing invalidate-on-panic hook did clean up after the crash,
but only for the *next* start.
- The AppleDouble case never recovered on its own.
### How?
- **`turbo-persistence` `load_directory`**:
- skips dot-prefixed entries before parsing the sequence number;
- names the file when a name can't be parsed;
- after the `SstFilter` pass, checks that every SST file still
referenced by a live meta file exists under its canonical name. The
check reuses the directory listing it already reads, so there are no
extra `stat` calls. Retired and obsolete SSTs are not checked, so
healthy compacted DBs are unaffected.
- **`turbo-tasks-backend` `open_versioned_on_disk`**: if opening fails,
it removes **only** the current version's directory and retries the open
**once**.
- The removal is guarded so it can only touch a direct child of the
cache base directory. The directory is first atomically renamed to a
`__stale_`-prefixed sibling, so an interrupted deletion can't leave a
partially deleted database at the active path; the renamed directory is
then deleted (leftovers are swept by `handle_db_versioning` on the next
start).
- The result is reported as `StartupCacheState::Invalidated` with a new
`invalidation_reasons::UNREADABLE`.
- If removal or the retry fails, the error includes the original open
failure.
- The fallback uses the same gate as invalidate-on-panic. With
`TURBO_ENGINE_SKIP_INVALIDATE_ON_PANIC` / `__NEXT_TEST_MODE` set, the
(now clearer) open error is returned as before, so these problems stay
visible there.
- **`next-napi-bindings`**: warning text for the new reason code.
Not covered: files lost or corrupted *during* a session (and missing
blob files). Treating those as per-task cache misses would leave an
inconsistent task graph, so the existing panic plus
invalidate-on-next-start behavior is unchanged. Corrupt `.meta` parsing
is handled separately in #96869.
Updated docs/comments:
- `turbopack/crates/turbo-persistence/README.md` (on-disk format:
ignored dot files; Opening: missing-SST check)
- `load_directory` doc comment
- `open_versioned_on_disk` and `turbo_backing_storage` docs
- `invalidation_reasons::UNREADABLE`
No Next.js docs describe this behavior.
### Verification
- New regression tests: `turbo-persistence/src/tests.rs` (missing,
renamed and retired SSTs; dot files in writable and read-only opens;
unparseable names), plus tests in `turbo-tasks-backend`
`kv_backing_storage.rs` and `db_versioning.rs`:
- replace and reuse the recovered cache;
- recovery disabled;
- removal failure (no retry);
- retry failure (one retry, both errors kept);
- the removal path guard.
- The persistence tests failed before the fix, except the retired-SST
guard. The backend replacement test fails with the fallback disabled.
- `cargo test -p turbo-persistence --lib` (195 passed, including the
existing compaction/merge stress tests, as a check against false
"missing SST" errors on healthy DBs). `cargo test -p turbo-tasks-backend
--lib` (253 passed).
- `cargo clippy -p turbo-persistence -p turbo-tasks-backend -p
next-napi-bindings --tests -- -D warnings`, `cargo fmt --check`, strict
rustdoc (`RUSTDOCFLAGS="-D warnings" cargo doc --no-deps
--document-private-items`) on the touched crates.
- Manual: a locally built `next-napi-bindings`, loaded into
`next@16.5.0-canary.53` via `__INTERNAL_CUSTOM_TURBOPACK_BINDINGS` (with
`TURBO_ENGINE_IGNORE_DIRTY=1`), on a two-route app.
- Warm the cache, stop, delete one live task-data `.sst`, restart,
request the uncompiled `/cold`: **200**, the server stays up, one
cache-deleted warning, and the next start reuses the rebuilt cache.
- Same with `._*` sidecars next to every cache file: **200** on repeated
restarts, cache kept.
- With `TURBO_ENGINE_SKIP_INVALIDATE_ON_PANIC=1`: startup fails with
`000000NN.sst referenced from 000000NN.meta is missing in …` instead of
the later panic.
- Unmodified canary.53 on the same steps: no response for `/cold`, exit
code 1.
- Not run: Windows, WASI and Miri runs, `next build` end to end (the
change is in the shared backing-storage open path, covered by the Rust
tests).
<!-- NEXT_JS_LLM -->
<!-- fleet 79dad4fe-738c-4dec-84c9-a9f7eca2ffec -->
---------
Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>