Turbopack: evict cached route entries on server HMR full re-evaluation (#102649)
### What?
In Turbopack dev, when server HMR falls back to a full re-evaluation, it
now evicts every cached server output file from `require.cache`, not
just the server HMR chunks. This PR also adds a test-only hook that
forces that fallback, plus a regression test.
### Why?
When an update can't be applied in place, `setupServerHmr` falls back to
`reEvaluateAllModulesExpensive()`. Until now that fallback only evicted
`<distDir>/server/chunks/**`. Route entry files such as
`server/app/<page>.js` stayed cached.
Suppose a page was loaded before a fallback that ran on another route's
update, and then one of its Client Components was edited. That page
could keep server-rendering the old Client Component markup while its
server component values were current. Another request didn't fix it; a
later edit or a restart did.
That is one way to get the "dev server keeps serving old server output
until restart" behavior in reports like #86306 and #93293. This PR
doesn't claim to fix every report there. Natural edit-and-request stress
hit the fallback, but these tests only reproduced the stale output end
to end when the fallback was forced.
### How?
- **Eviction:** the fallback now evicts every cached file under
`<distDir>/server/` through the existing `deleteCache` helper. That
still clears manifest caches and notifies cache-invalidation listeners.
The other fallback steps (runtime chunk cache, handler registry, edge
contexts, fetch reset, dropping the validation worker) are unchanged.
Normal, non-fallback HMR invalidation is untouched.
- **Test hook:** `NEXT_TEST_SERVER_HMR_FORCE_REEVALUATION` takes an
entry path, e.g. `api/force-reeval/route.js`. The first partial server
HMR update pulled for a route with that entry path takes the existing
fallback path instead of being applied, and prints one marker line so
tests can confirm it fired. When the variable is unset, nothing changes.
No natural deterministic trigger was found, so the test needs this hook.
- **Regression test:** `test/development/app-dir/server-hmr-fallback` is
Turbopack-only. It loads a page and a route handler, edits a shared
module and a Client Component, and lets the handler's update take the
forced fallback. It then renders the page once, edits the Client
Component again, and checks the exact text of the rendered element in
the raw server HTML (scripts excluded). Finally it checks that regular
server HMR still works.
### Verification
Run locally on Linux. Native bindings were built from source with `pnpm
build-native` because `16.5.0-canary.76` isn't published.
- New test with only the eviction change reverted (hook and test kept):
fails. It server-renders `CLIENT_V2`, expected `CLIENT_V3`.
- New test with the fix: passed 3/3 runs.
- `pnpm test-dev-turbo
test/development/app-dir/server-hmr/server-hmr.test.ts`: 21/21 passed
(existing test, behavior unchanged).
- Prettier, ESLint on changed files, and `pnpm --filter=next types` all
pass.
- Not run: macOS/Windows, and Webpack dev (the change is in the
Turbopack hot reloader only).
<!-- NEXT_JS_LLM -->
<!-- fleet 67e38c94-3777-4c00-959f-e515f4c3b034 -->
Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>