Reuse last-good source when a dev static-info read is torn
The `edge-runtime-module-errors` development-mode tests intermittently fail with a 404 on an edge route that should exist. Each case rewrites `pages/api/route.js` and `afterEach` restores it, and under load the route occasionally answers 404 for the rest of the run. The cause is a race between reading a route file for its static info and a writer rewriting it. In webpack dev the pages and middleware manifests are regenerated from scratch on every compilation from that compilation's entrypoints, and both the on-demand entry handler and `config.entry` read each route file out-of-band to derive its static info, most importantly the runtime that decides which compiler owns the entry. Editors, git checkouts, formatters, codemods, and the test harness all rewrite files with a non-atomic truncate-then-write, which briefly leaves the file empty. A read that lands in that truncate window comes back empty and parses the runtime as `undefined`. Misread as a plain Node route, an edge entry takes the `onServer` branch, which no-ops in the edge compilation, and the edge-keyed entry is skipped by the Node compilation too, so it is emitted by neither and drops out of the regenerated manifest. Because its on-demand entry is still marked built, `ensure` short-circuits without recompiling it, and the write settling fires no watcher event, so nothing repairs it and the route answers 404 until the file changes again. This change reuses the last good source instead of acting on such a read. A new `readStaticInfoSource` wrapper remembers the last non-empty source read for each file and, in the dev server, returns that source when a subsequent read comes back empty or momentarily absent. That keeps the route in its compiler's entrypoints and therefore in the manifest, consistent with its still-built entry. An empty read is never legitimate route source, so preferring the last good source is always the safe choice: a genuine runtime switch writes non-empty content and is read and classified from its own source, and a file that is genuinely emptied is invalid regardless of how it is classified and simply rebuilds and errors once real content is read again. The reuse is transient, since the cache is overwritten by the next non-empty read that any compilation or request performs, and it is limited to the dev server, since a production build reads a static source tree with nothing racing it. It is worth being precise about what this guarantees. The fix eliminates the diagnosed failure, where the static-info read alone being torn drops the route and produces a stuck 404. It does so by keeping the route in the entrypoints, which is also what lets webpack build the module at all: in the old behavior the dropped entry means webpack never builds the route in that compilation, so it is simply absent. Webpack's own build read happens later in the compilation than `config.entry`, so in practice it observes the settled content and builds fresh. The remaining theoretical window is narrower and softer: only if webpack's later build read also lands in a truncate window would the module build empty, producing a 500 (a present-but-empty route) rather than a 404, recoverable on the next write the same way. That read is webpack's own and is not touched here. Across 2500 rapid rewrite iterations the served status stayed 200 throughout, where the same churn without the fix stuck at iteration 112, so the change strictly narrows the failure window (from one torn read to two, at different points of the compilation) and softens the worst-case symptom. The fix is scoped to empty and absent reads, which are the torn states a single writer produces: a truncate window, or the brief gap of an atomic rename. Non-empty but corrupt reads require two writers racing the same file, which normal editing, git, and branch switches do not do, and covering them would mean reusing last-good when a non-empty read fails to parse, which could not be distinguished from a genuine syntax error the user just typed.
Showing
Please
register
or
sign in
to comment