11#!/usr/bin/env bun
22/**
3- * Caps the size of Turbopack's dev filesystem cache .
3+ * Caps Turbopack's dev and CI build filesystem caches .
44 *
55 * `experimental.turbopackFileSystemCacheForDev` is ON (see `apps/sim/next.config.ts`)
66 * because it makes dev-server restarts 5.4x faster and cuts RSS ~1.9x. The tradeoff
7- * is that the cache is an append-heavy LSM store with no upstream GC: it grows with
7+ * is that the cache is an append-heavy LSM store without automatic GC: it grows with
88 * every route compiled and every code change, and it is never pruned. An abandoned
99 * cache in this repo reached 78 GB across 1,848 SST files.
1010 *
1111 * A stale cache is also slower to read back, so an unbounded one eventually erodes
1212 * the win it exists to provide. This script drops the whole cache once it crosses a
13- * threshold; Turbopack rebuilds it on the next run (one cold compile, ~32s).
13+ * threshold; Turbopack rebuilds it on the next run (one cold dev compile, ~32s).
1414 *
1515 * Also the remedy when Turbopack aborts with `Cache corruption detected:
1616 * checksum mismatch` — it does not self-heal from a corrupted cache, so prune
2121 * bun run scripts/prune-turbopack-cache.ts # prune if over the cap
2222 * bun run scripts/prune-turbopack-cache.ts --force # always prune
2323 * bun run scripts/prune-turbopack-cache.ts --dry-run # report only
24+ * bun run scripts/prune-turbopack-cache.ts --build # production cache, only between builds
25+ *
26+ * Build mode retains the mounted cache root and fails maintenance errors; dev
27+ * maintenance stays nonblocking. Size checks stream entries and stop once the
28+ * build cache exceeds the cap.
2429 *
2530 * Override the cap with `SIM_TURBOPACK_CACHE_MAX_GB` (default 20).
2631 *
3944 * `kill -9`. Turbopack discards a partially-written cache and rebuilds silently,
4045 * so a hard kill makes a real cache win read as no win at all.
4146 */
42- import { existsSync , statSync } from 'node:fs'
43- import { readdir , rm , stat } from 'node:fs/promises'
47+ import { existsSync , lstatSync , statSync } from 'node:fs'
48+ import { opendir , rm , stat } from 'node:fs/promises'
4449import path from 'node:path'
4550
4651const DEFAULT_MAX_GB = 20
4752const BYTES_PER_GB = 1024 ** 3
53+ const BUILD_CACHE = process . argv . includes ( '--build' )
4854
4955/**
50- * Resolved from the working directory, not hardcoded to one app: every Next app
51- * in the monorepo has its own dev cache (`apps/docs` runs `next dev` too and, with
52- * no override, uses the Next default where the cache is on). Each app caps its
53- * own, so one app's dev start never prunes a cache another app is holding open.
56+ * Each app owns its cache under its working directory. Build maintenance selects
57+ * a separate root, so it cannot prune a live dev server's state.
5458 */
55- const CACHE_DIR = path . resolve ( process . cwd ( ) , '.next' , 'dev' , 'cache' , 'turbopack' )
59+ const CACHE_DIR = path . resolve (
60+ process . cwd ( ) ,
61+ BUILD_CACHE ? '.next/cache' : '.next/dev/cache/turbopack'
62+ )
63+
64+ function isMissingPath ( error : unknown ) : boolean {
65+ return error instanceof Error && 'code' in error && error . code === 'ENOENT'
66+ }
5667
57- /** Sums the size of every regular file under `dir`, skipping symlinks . */
58- async function directorySize ( dir : string ) : Promise < number > {
68+ /** Counts regular files without following symlinks or retaining the tree in memory . */
69+ async function directorySize ( dir : string , limit : number ) : Promise < number > {
5970 let total = 0
60- const entries = await readdir ( dir , { withFileTypes : true , recursive : true } )
61- for ( const entry of entries ) {
62- if ( ! entry . isFile ( ) ) continue
63- try {
64- total += ( await stat ( path . join ( entry . parentPath , entry . name ) ) ) . size
65- } catch {
66- // Turbopack compacts while we walk; a file vanishing mid-scan is expected.
71+ let entries : Awaited < ReturnType < typeof opendir > >
72+ try {
73+ entries = await opendir ( dir )
74+ } catch ( error ) {
75+ if ( BUILD_CACHE && isMissingPath ( error ) ) return 0
76+ throw error
77+ }
78+ for await ( const entry of entries ) {
79+ const entryPath = path . join ( dir , entry . name )
80+ if ( entry . isDirectory ( ) ) {
81+ total += await directorySize ( entryPath , limit - total )
82+ } else if ( entry . isFile ( ) ) {
83+ try {
84+ total += ( await stat ( entryPath ) ) . size
85+ } catch ( error ) {
86+ if ( BUILD_CACHE && ! isMissingPath ( error ) ) throw error
87+ }
6788 }
89+ if ( total > limit ) break
6890 }
6991 return total
7092}
@@ -86,12 +108,27 @@ async function main() {
86108 const force = process . argv . includes ( '--force' )
87109 const dryRun = process . argv . includes ( '--dry-run' )
88110
89- if ( ! existsSync ( CACHE_DIR ) || ! statSync ( CACHE_DIR ) . isDirectory ( ) ) return
111+ if ( BUILD_CACHE ) {
112+ let root : ReturnType < typeof lstatSync >
113+ try {
114+ root = lstatSync ( CACHE_DIR )
115+ } catch ( error ) {
116+ if ( isMissingPath ( error ) ) return
117+ throw error
118+ }
119+ if ( root . isSymbolicLink ( ) || ! root . isDirectory ( ) )
120+ throw new Error ( 'Build cache root must be a directory without symlinks' )
121+ } else if ( ! existsSync ( CACHE_DIR ) || ! statSync ( CACHE_DIR ) . isDirectory ( ) ) {
122+ return
123+ }
90124
91125 const maxGb = parseMaxGb ( )
92- const bytes = await directorySize ( CACHE_DIR )
126+ const limit = BUILD_CACHE ? maxGb * BYTES_PER_GB : Number . POSITIVE_INFINITY
127+ const bytes = force ? 0 : await directorySize ( CACHE_DIR , limit )
93128 const gb = bytes / BYTES_PER_GB
94- const sizeLabel = `${ gb . toFixed ( 1 ) } GB`
129+ const sizeLabel = force
130+ ? 'cache'
131+ : `${ BUILD_CACHE && bytes > limit ? 'at least ' : '' } ${ gb . toFixed ( 1 ) } GB`
95132
96133 if ( ! force && gb <= maxGb ) {
97134 if ( dryRun ) console . log ( `[turbopack-cache] ${ sizeLabel } (cap ${ maxGb } GB) — keeping` )
@@ -105,10 +142,15 @@ async function main() {
105142 }
106143
107144 console . log ( `[turbopack-cache] ${ sizeLabel } is ${ reason } — pruning; next start recompiles cold` )
108- await rm ( CACHE_DIR , { recursive : true , force : true } )
145+ if ( BUILD_CACHE ) {
146+ for await ( const entry of await opendir ( CACHE_DIR ) )
147+ await rm ( path . join ( CACHE_DIR , entry . name ) , { recursive : true , force : true } )
148+ } else {
149+ await rm ( CACHE_DIR , { recursive : true , force : true } )
150+ }
109151}
110152
111153main ( ) . catch ( ( error ) => {
112- // Never block `next dev` on a cache-maintenance failure.
113154 console . warn ( '[turbopack-cache] prune skipped:' , error )
155+ if ( BUILD_CACHE ) process . exitCode = 1
114156} )
0 commit comments