next.js
aba8ca3f - Turbopack: opt-in atomic file writes, used for client reference manifests (#101786)

Commit
10 hours ago
Turbopack: opt-in atomic file writes, used for client reference manifests (#101786) ### What? Adds `File::with_atomic_write()` to `turbo-tasks-fs`, and uses it for `_client-reference-manifest.js`. An atomic write puts the new content in a temporary file in the same directory, then renames it over the target. Executables were already written this way. A reader that opens the file during the write sees the old content or the new content, never an empty or partial file. ### Why? The dev server reads client reference manifests with `evalManifest` while Turbopack may be rewriting them. Turbopack truncates and rewrites files in place, so a read can catch an empty or half-written manifest. The retry loops in `load-components.ts` exist to work around exactly this (#45244, #56518). Webpack dev has written manifests atomically since #100112. This change does the same for Turbopack's client reference manifest. ### How? - `FileMeta` gains an `atomic_write` flag. `File::with_atomic_write()` sets it, and `DiskFileSystem::write` takes the temp-file + rename path when either the flag is set or the file is executable. - **Before-write comparison:** `streaming_compare` used to compare the new file's whole `FileMeta` with the metadata read from disk. Disk only stores permissions, so it now compares permissions only. Otherwise every file with the flag would look changed and be rewritten on every write. Files with a `content_type` (e.g. images) had the same problem and are no longer rewritten when unchanged. - `atomic_write` is left out of `DeterministicHash`: it changes how a file is written, not what is written. - **Test:** the new unix test checks that a plain write keeps the inode, and that an atomic write replaces it while an already-open reader still reads the old content. It also checks that writing the same content again does not rewrite the file. Only the client reference manifest opts in for now. Other manifests that the server reads while they may change can opt in the same way.
Author
Parents
Loading