Turbopack: run loaders with the current source content after an edit (#102288)
### What?
In `next dev` with Turbopack, editing a file processed by a
`turbopack.rules` loader (e.g. a global stylesheet using
`@tailwindcss/turbopack`) or by the PostCSS transform could leave the
served output one edit behind. `touch` had no effect; an unrelated edit
or restart caught it up. After this change, loaders and PostCSS run with
the file's current content after an edit.
### Why?
The first loader run after an edit could receive the **previous**
content while the disk already had the new one. The evaluation task
depends on files the loaders report, which can include the resource
itself (loader-runner reports it, and loaders may read or `this.fs.stat`
it). An edit could therefore re-run evaluation before its source content
argument had settled. Loaders that cache by the resource's mtime, as
`@tailwindcss/turbopack` does, then stored stale output under the new
mtime.
### How?
- `EvaluateArg::SourceContent { source, encoding }` declares source
content as an evaluation argument. Webpack loaders use `TextOrBinary`,
so non-UTF-8 content still reaches loaders as a `Buffer`; PostCSS uses
`Text` and still rejects invalid UTF-8.
- `custom_evaluate` resolves the complete argument list (`Vc`, `Path`
and `SourceContent`) in one rooted operation keyed on the arguments and
working directory, and reads that operation strongly consistently before
running JavaScript. Callers choose argument order explicitly; there is
no hidden prepend.
- A missing source skips evaluation and keeps the existing NotFound
result. The resolved JSON argument snapshot is not persisted, so
absolute paths for additional roots don't enter the persistent cache.
- The source is read through the `Source` API, so earlier source
transforms still apply. Loader-reported file reads, stats, globs,
environment variables and build dependencies remain tracked, including
metadata-only changes.
- Docs on the argument types, encoding, argument operation and loader
evaluation were updated. No user-facing docs describe this behavior.
Finding: https://next-maintainer-agent.vercel.tools/agent-feedback/8451
Fixes #99313
Related: #93052 (same mechanism in the built-in PostCSS transform;
covered by the new PostCSS test, not checked against that issue's own
reproduction)
**Version research:** 16.4.0 (`e273d5b2e869`) vs the preceding stable
16.3.8 (`b0fad0d45eb4`), scoped to `turbopack-node` transforms. Both
already reported and tracked the resource as a loader file dependency,
and the linked issues report the same symptom on 16.3.6 and 16.2.x. This
points to a longstanding race rather than an established 16.4.0
regression. No introducing commit or known-good version was established,
and no bisect was run.
### Verification
- New `test/development/app-dir/loader-stale-resource-input`: an
mtime-caching CSS loader reads and stats its resource through `this.fs`,
logging source, disk content and mtime. It covers three successive
edits, an external reported dependency, metadata-only changes for a
resource-stat loader, and an unrelated edit that must not re-run the CSS
loader.
- New `test/development/app-dir/postcss-stale-resource-input`: an
mtime-caching PostCSS plugin covers the same successive edits (Turbopack
only).
- Both stale-input tests failed on canary without the Rust fix (stale
first invocation; CSS one edit behind). On the final branch: loader 4/4
and PostCSS 1/1 tests passed in each of 3 `pnpm test-dev-turbo` runs;
the loader suite also passed with `pnpm test-dev-webpack`.
- Existing tests on the final branch with `pnpm test-dev-turbo`:
`webpack-loader-fs`, `webpack-loader-binary`,
`development/app-dir/webpack-loader-errors`,
`webpack-loader-resource-query`, `turbopack-postcss-multiple-configs`,
`tailwind-css`, `development/basic/tailwind-jit.test.ts`,
`turbopack-additional-roots`, and `next-font/basepath.test.ts` (mocked
Google font evaluation).
- Additional existing loader suites passed during development before the
final argument-resolution refactor:
`webpack-loader-{misc,resolve,import-module,set-environment-variable,utils,conditions,module-type,ts-transform}`,
`no-double-tailwind-execution`, and `webpack-loader-parse-error`.
- Manual check before the final argument-resolution refactor: a Tailwind
v4 (`@tailwindcss/turbopack`) app with repeated `globals.css` rewrites,
plus a plain-CSS control. Without the fix the CSS stayed one revision
behind and `touch` did nothing; with the fix, served CSS matched every
revision.
- `cargo fmt`, `cargo clippy -p turbopack-node --all-targets -- -D
warnings -A deprecated`, `cargo check -p next-core -p turbopack-tests
--tests`, prettier/eslint on the new test files, and `pnpm
lint-typescript`.
- Not run locally: Windows, browser HMR and persistent-cache variants;
performance benchmarking of the per-evaluation argument operation; the
full Rust execution-test suite (left to CI).
<!-- NEXT_JS_LLM -->
<!-- fleet 60646929-4c5d-4ebe-802f-a3e73cf68d2c -->
---------
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>