Turbopack: add build cache portability e2e test and fix location-dependent tasks (#100780)
### What?
Adds an e2e test that checks whether the Turbopack filesystem build
cache survives moving the project to another directory. It also fixes
the first portability problems the test found.
It relies on the `executions` counter in turbo-tasks task statistics,
added in #101025.
### Why?
A restored build cache should still be valid when the project sits at a
different absolute path. That happens with CI checkouts, Docker build
contexts, and caches restored into another workspace. Today, moving a
project makes many tasks re-execute even though nothing changed. This PR
adds a way to measure that and removes some of the causes.
### How?
**Test** (`test/e2e/app-dir/turbopack-portable-build-cache`, Turbopack
production builds only, not deploy):
- Each case runs a cold `next build`, a warm build in the same
directory, then moves the whole project root (including `.next`) and
runs another warm build.
- Every build writes turbo-tasks task statistics
(`NEXT_TURBOPACK_TASK_STATISTICS`) to a temporary file outside the
project. The test collects the functions with `executions > 0`.
- `cache_miss` isn't enough: it only counts newly created tasks, while a
restored task that re-executes after an invalidation counts as a cache
hit. #101025 adds the `executions` counter.
- Inline snapshots record the sorted executed function names for the
same-dir and moved builds. All `NextConfig` methods are collapsed into a
single `NextConfig::*` entry: they depend on the same config cell, and
which ones are called depends on the build mode. The snapshots document
the current state, and the moved diff should shrink as portability
improves. A cold-build execution count check makes sure an empty
snapshot means "cached", not "nothing counted".
- Two fixtures:
- a minimal app;
- a monorepo whose workspace root is the project root. It covers app and
pages routes, a route handler, SSG, a server action, client components
from a workspace package, proxy, a `turbopack.rules` loader,
`resolveAlias`, jsconfig `paths` outside the app, and relative symlinks.
The test asserts the route table, the server-actions manifest, and
rendered markers on both builds, so a silently dropped feature can't
make a snapshot look clean.
- The build env pins `NEXT_SERVER_ACTIONS_ENCRYPTION_KEY`. Inside
Docker, `getStorageDirectory()` doesn't persist the generated key in
`.next/cache`, so each build would produce a new one. The key is a
Turbopack project input, so this would invalidate
server-action-dependent work and make snapshots environment-dependent
and nondeterministic.
- To keep the snapshots environment-independent, the test also:
- always installs with pnpm's hoisted linker. The default layout depends
on the Node.js version (realpath workaround), which changes which
symlinks are resolved;
- disables telemetry, which reads extra config values.
**Turbopack fixes**, so tasks don't depend on the absolute project
location:
- **Source map rewriting** (`turbopack-core` `source_map/utils.rs`):
- It used to await the entire `DiskFileSystem`, whose cell includes the
absolute root, just to build the `turbopack:///[name]/` prefix.
- It now reads only the filesystem name, through its own task. It reads
it only when a source actually uses that prefix, and needs the root only
for absolute-path conversion.
- This removed all relocation-only codegen re-execution
(`module_factory_with_code_generation_issue` /
`EcmascriptChunkItemContent::new`) in the minimal app.
- **`WriteLinkContent::hash`** reads only the filesystem name instead of
the whole filesystem, and no longer requires a `DiskFileSystem`. Hash
values are unchanged.
- **`resolve_options`** checks for the configured tsconfig with
`get_type()` instead of `metadata()`. A failed `metadata` call's error
includes the absolute path, and errors are part of the task output, so
the check used to change after a move:
- files and symlinks count as present;
- directories, missing entries, and read errors fall back to tsconfig
discovery.
- The `trace_task_dirty` feature compiles again (`task_id` was undefined
in `make_task_dirty_without_scheduling`).
**Remaining after a move** (visible in the moved snapshots):
- `NextConfig::*` re-runs (`from_string` and its getters), because the
serialized config contains absolute paths (`turbopack.root`,
`outputFileTracingRoot`, …).
- `DiskFileSystem::new_internal` and writes to the new output location
re-run.
- The webpack-loader evaluation pool re-runs; it needs absolute paths to
launch Node.js.
### Verification
- Both portability cases pass twice in a row with `CI=1`, and once more
with `__NEXT_CACHE_COMPONENTS=true`. The task-statistics snapshots list
the same functions as the earlier trace-based ones.
- Existing `server-source-maps`, `tsconfig-path`,
`typescript-custom-tsconfig` and `tsconfig-module-preserve` e2e tests
pass.
- Rust tests and Turbopack snapshot tests for the touched crates pass.
- Clippy, `pnpm typescript`, prettier and eslint are clean.
- Only measured on Linux x64.
<!-- NEXT_JS_LLM -->
<!-- fleet b5790f48-9831-405d-9305-a49127b7dd9e -->
---------
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>