Turbopack: ignore fully dynamic alternatives when tracing file paths (#100651)
## Summary
With Turbopack, a file read whose path is _either_ an unknown value _or_
a known file traced the **entire project** into `.nft.json`, including
`public/`. It also printed "Dynamic filesystem access causes tracing of
the whole project". Two examples:
- `readFileSync(options.path ?? path.join(process.cwd(),
'manifest.json'))`
- a default parameter `p = path.join(process.cwd(), 'manifest.json')`
that is then read
The default-parameter form regressed in 16.4: #97867 models default
params as "caller's argument or the default". The `??` form behaved like
this before.
A bare unknown path (`readFileSync(p)`) is already skipped when
`ignore_dynamic_requests` is on, as it is for Next.js server and client
code. The mixed case slipped through because `has_constant_parts()` is
true as soon as _any_ alternative is constant. The unbounded alternative
then resolved to every file in the project.
### Approach
A new `Pattern::filter_non_dynamic()` in `turbopack-core` keeps only the
top-level alternatives that have constant parts. When dynamic requests
are ignored, the file-tracing handlers trace that filtered pattern. The
handlers are: fs reads, `fs.readdir`, `path.join`, `path.resolve`,
`child_process.spawn` (its file reference) and `child_process.fork`. As
a result:
- the known file is traced, and the unknown alternative is ignored, just
as a bare unknown path is;
- fully dynamic patterns behave as before: a "very dynamic" lint, then
skip;
- partially bounded paths such as `path.join(process.cwd(), name)` still
trace the project and still warn;
- module requests (`require`, `import()`, `new URL`, workers), externals
tracing, standalone nft (`ignore_dynamic_requests` off) and webpack are
unchanged.
### Behavior change
A path that only exists in the dropped dynamic alternative is no longer
traced. It now ends up exactly where a bare `readFileSync(foo)` already
does. The only thing it got from the old behavior was a whole-project
trace, which was a failure in its own right, not a working fallback. For
the same reason the whole-project warning goes away for these cases.
`child_process.fork(x ?? './worker.js')` creates a module reference, so
in that case this also narrows what gets bundled to `./worker.js`.
### Tests
- `Pattern::filter_non_dynamic` has unit tests.
- `test/production/standalone-mode/tracing-static-files` covers the
`??`, default-parameter and bare-dynamic forms by reading the page's
`.nft.json`:
- **Turbopack:** the known data files are traced.
- **Both bundlers:** the unreferenced `data/unrelated.txt` and
`next.config.js` aren't traced.
Without the fix, Turbopack lists the whole project.
- The same suite has an external package (`serverExternalPackages`) with
the same patterns, covering the externals tracing context where
`ignore_dynamic_requests` is off. There, the dynamic alternatives are
kept and traced from the reading module's directory, but they don't
escape into the project. Project-root files referenced via
`process.cwd()` from external packages aren't traced; that predates this
PR and is asserted as current behavior.
## Verification
- Manual check: in an app whose instrumentation hook reads a manifest
through a default parameter, the trace went from the whole project (260
MB from a large `public/` asset) to just the manifest and the generated
chunks.
<!-- NEXT_JS_LLM -->
<!-- fleet ad3254c1-cab0-4e7e-89a5-84a673c8e16a -->
---------
Co-authored-by:
vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Showing
Please
register
or
sign in
to comment