AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Turbopack Persistent Cache Causes Slower Builds And OOM After Pipeline Changes

When a compile pipeline change (e.g. turbopack.rules loader, reactCompiler) invalidates most of the module graph, `next build` over an existing `.next/cache` is significantly slower and uses more memory than a cold build, often causing OOM on constrained CI. The persistent cache retains stale entries during rebuild and lacks mass-invalidation fallback.

criticalConfidence 85%Next.jsAffected V16.3.6Affected V16.4.0-Canary.57

Origin Analysis

Turbopack's filesystem cache does not detect mass invalidation; invalid entries remain in memory/on disk while new artifacts are produced, leading to duplicate graph state and memory bloat. `turbopackGc` only cleans after the build, not during, so peak memory is unaffected. There is no mechanism to clear or bypass the cache when a large fraction of modules change.
1. Create app with many routes/components importing from a macro expanded via `turbopack.rules` and `condition.content`; enable `reactCompiler` and `experimental.turbopackRustReactCompiler`. 2. Run `LOADER=off next build` from empty `.next` (cold). 3. Run `LOADER=on next build` keeping `.next/cache` from step 2 (warm over old pipeline). 4. Run `LOADER=on next build` from empty `.next` (cold new pipeline). Compare time and peak memory: warm is slower and higher.

Fixing Code Block

Edge Case Audit

Clearing the cache on any config change discards all incremental build benefits after pipeline changes, which may increase build times for small changes. In a monorepo or with frequent config tweaks this may negate caching. Do not run concurrently across multiple processes targeting the same `.next` directory—use a lock or ensure CI jobs are serialized. To roll back, remove the script and restore the previous `.next/cache` from backup; the cache will naturally rebuild. Test on Vercel where cache is restored automatically: add this step before `next build` in the build command.

Ecosystem Topology