From 4ef3819cac01319c39d89fd62290e14a7d7907a3 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Wed, 18 Nov 2026 08:43:31 +0000 Subject: [PATCH 1/2] build: clear Deno's compiled-code cache before deno's next builds Deno keeps compiled code for CommonJS files in $DENO_DIR/v8_code_cache_v2. `next build` emits .next/server/chunks/961.js in two variants with the same path and size but different content; when the variant flips between builds, Deno runs the old compiled code against the new file and webpack's runtime throws `TypeError: e[o] is not a function`. A failed run doesn't refresh the entry, so it keeps failing until the variant flips back. In the suite, build and build-cache run deno builds at the same path with nothing clearing that cache in between. Add clean_deno_code_cache, which removes v8_code_cache_v2* from the directory `deno info --json` reports (DENO_DIR / XDG_CACHE_HOME / default), leaving the npm cache alone, and call it in deno's prepare: in build after the install and before the timed build; in build-cache before the warm build and again before the timed rebuild. The cold build pays a cold compile cache (~2.7s of ~27s here, as node does on every build); the cached rebuild shows no measurable difference. Fixes #215 Co-Authored-By: Claude Opus 5.5 --- suite/scripts/clean-helpers.sh | 29 +++++++++++ suite/scripts/clean-helpers.test.js | 69 +++++++++++++++++++++++++ suite/scripts/variations/build-cache.sh | 8 ++- suite/scripts/variations/build.sh | 6 ++- 4 files changed, 110 insertions(+), 2 deletions(-) diff --git a/suite/scripts/clean-helpers.sh b/suite/scripts/clean-helpers.sh index 978676e19c..d7793eb139 100644 --- a/suite/scripts/clean-helpers.sh +++ b/suite/scripts/clean-helpers.sh @@ -134,6 +134,31 @@ clean_deno_cache() { fi } +# Deno keeps compiled code for the CommonJS files it runs in +# $DENO_DIR/v8_code_cache_v2 (a SQLite database, with -wal/-shm sidecars). +# When a build writes a file with the same path and size as the previous +# build but different content (`next build` emits `.next/server/chunks/961.js` +# in two such variants), Deno runs the old compiled code against the new file +# and webpack's runtime throws `TypeError: e[o] is not a function`. A failed +# run doesn't refresh the entry, so it keeps failing until the variant flips +# back. Remove just the compiled-code cache, not the npm cache or the rest of +# $DENO_DIR, so the next build compiles from the files on disk. (#215) +# +# `deno info --json` reports the directory wherever it is (DENO_DIR, XDG_CACHE_HOME +# or the platform default); fall back to the Linux default if it can't be asked. +clean_deno_code_cache() { + local deno_dir="" entry + if command -v deno &> /dev/null; then + deno_dir="$(deno info --json 2>/dev/null | sed -n 's/^ *"denoDir": *"\(.*\)",\{0,1\}$/\1/p' || true)" + fi + if [ -z "$deno_dir" ]; then + deno_dir="${DENO_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/deno}" + fi + for entry in "$deno_dir"/v8_code_cache_v2*; do + safe_remove "$entry" + done +} + # Function to safely clean vp cache clean_vp_cache() { if command -v vp &> /dev/null; then @@ -322,6 +347,7 @@ show_help() { echo " clean_bun_cache" echo " clean_nx_cache" echo " clean_deno_cache" + echo " clean_deno_code_cache" echo " clean_aube_cache" echo " clean_aube_metadata_cache" echo " clean_upm_cache" @@ -374,6 +400,9 @@ else clean_deno_cache) clean_deno_cache ;; + clean_deno_code_cache) + clean_deno_code_cache + ;; clean_aube_cache) clean_aube_cache ;; diff --git a/suite/scripts/clean-helpers.test.js b/suite/scripts/clean-helpers.test.js index 1d8fc9dfbb..34426bac70 100644 --- a/suite/scripts/clean-helpers.test.js +++ b/suite/scripts/clean-helpers.test.js @@ -281,6 +281,75 @@ test("clean_berry_cache removes berry's global folder, with and without XDG_DATA ]); }); +// Run clean_deno_code_cache against a fake deno dir and record what it +// removes. `denoDir` is what the fake `deno info --json` reports, or null for +// no deno on PATH (the fallback to DENO_DIR / XDG_CACHE_HOME / ~/.cache). +const denoCodeCacheRemovedBy = (denoDir, env = {}) => { + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "deno-clean-test-")); + try { + const binDir = path.join(tempDir, "bin"); + const removeLog = path.join(tempDir, "removed.txt"); + fs.mkdirSync(binDir); + fs.writeFileSync(removeLog, ""); + if (denoDir) { + fs.writeFileSync( + path.join(binDir, "deno"), + `#!/bin/sh\n[ "$1 $2" = "info --json" ] || exit 1\nprintf '{\\n "version": 1,\\n "denoDir": "%s",\\n "npmCache": "%s/npm"\\n}\\n' "$FAKE_DENO_DIR" "$FAKE_DENO_DIR"\n`, + { mode: 0o755 }, + ); + // The code cache is a SQLite database with -wal and -shm sidecars; the + // npm cache sits next to it and must stay. + fs.mkdirSync(path.join(denoDir, "npm"), { recursive: true }); + for (const name of ["v8_code_cache_v2", "v8_code_cache_v2-wal", "v8_code_cache_v2-shm"]) { + fs.writeFileSync(path.join(denoDir, name), ""); + } + } + const result = spawnSync( + "bash", + ["-c", `source "$HELPERS" >/dev/null; safe_remove() { printf '%s\\n' "$1" >> "$REMOVE_LOG"; }; clean_deno_code_cache`], + { + encoding: "utf8", + env: { + // Only the fake bin dir (plus sed), so a real deno is never asked. + PATH: `${binDir}${path.delimiter}/usr/bin${path.delimiter}/bin`, + HOME: "/home/runner", + HELPERS: path.join(__dirname, "clean-helpers.sh"), + REMOVE_LOG: removeLog, + FAKE_DENO_DIR: denoDir ?? "", + ...env, + }, + }, + ); + assert.equal(result.status, 0, result.stderr); + return fs.readFileSync(removeLog, "utf8").trim().split("\n").filter(Boolean); + } finally { + fs.rmSync(tempDir, { recursive: true, force: true }); + } +}; + +test("clean_deno_code_cache removes only v8_code_cache_v2* from the dir deno reports", () => { + const denoDir = path.join(os.tmpdir(), `deno dir ${process.pid}`); + try { + assert.deepEqual(denoCodeCacheRemovedBy(denoDir).sort(), [ + path.join(denoDir, "v8_code_cache_v2"), + path.join(denoDir, "v8_code_cache_v2-shm"), + path.join(denoDir, "v8_code_cache_v2-wal"), + ]); + } finally { + fs.rmSync(denoDir, { recursive: true, force: true }); + } +}); + +test("clean_deno_code_cache falls back to DENO_DIR, XDG_CACHE_HOME, then ~/.cache", () => { + // Without deno nothing exists to glob, so the literal pattern is what + // safe_remove is asked about. + assert.deepEqual(denoCodeCacheRemovedBy(null, { DENO_DIR: "/deno dir" }), ["/deno dir/v8_code_cache_v2*"]); + assert.deepEqual(denoCodeCacheRemovedBy(null, { XDG_CACHE_HOME: "/xdg/cache" }), [ + "/xdg/cache/deno/v8_code_cache_v2*", + ]); + assert.deepEqual(denoCodeCacheRemovedBy(null), ["/home/runner/.cache/deno/v8_code_cache_v2*"]); +}); + test("clean_nx_cache does nothing outside an Nx workspace", () => { const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "nx-clean-test-")); try { diff --git a/suite/scripts/variations/build-cache.sh b/suite/scripts/variations/build-cache.sh index 244f47452d..3a46abdefe 100644 --- a/suite/scripts/variations/build-cache.sh +++ b/suite/scripts/variations/build-cache.sh @@ -65,6 +65,12 @@ BENCH_COMMAND_NODE="timeout $BENCH_TIMEOUT node --run build > $BENCH_OUTPUT_FOLD # Run the benchmark build-cache benchmarks. # Prepare: install deps + run a warm build (populates .next/ cache). # Timed command: rebuild with .next/ cache present. +# deno's prepare drops Deno's compiled-code cache twice: before the warm build, +# so it can't fail on a stale entry from the previous iteration and leave a +# half-written .next/, and again right before the timed rebuild, in case the +# rebuild emits the other variant of a server chunk than its warm build did +# (see clean_deno_code_cache). A cached rebuild loads little code, so a cold +# compile cache costs it nothing measurable. hyperfine --ignore-failure \ --time-unit=millisecond \ --export-json="$BENCH_OUTPUT_FOLDER/benchmarks.json" \ @@ -85,7 +91,7 @@ hyperfine --ignore-failure \ ${BENCH_INCLUDE_VLT:+--command-name="vlt" "$BENCH_COMMAND_VLT"} \ ${BENCH_INCLUDE_BUN:+--prepare="$BENCH_INSTALL_PREPARE_BUN || true; bash $BENCH_SCRIPTS/clean-helpers.sh clean_build_output; $BENCH_WARM_BUILD_BUN || true"} \ ${BENCH_INCLUDE_BUN:+--command-name="bun" "$BENCH_COMMAND_BUN"} \ - ${BENCH_INCLUDE_DENO:+--prepare="$BENCH_INSTALL_PREPARE_DENO || true; bash $BENCH_SCRIPTS/clean-helpers.sh clean_build_output; $BENCH_WARM_BUILD_DENO || true"} \ + ${BENCH_INCLUDE_DENO:+--prepare="$BENCH_INSTALL_PREPARE_DENO || true; bash $BENCH_SCRIPTS/clean-helpers.sh clean_deno_code_cache clean_build_output; $BENCH_WARM_BUILD_DENO || true; bash $BENCH_SCRIPTS/clean-helpers.sh clean_deno_code_cache"} \ ${BENCH_INCLUDE_DENO:+--command-name="deno" "$BENCH_COMMAND_DENO"} \ ${BENCH_INCLUDE_AUBE:+--prepare="$BENCH_INSTALL_PREPARE_AUBE || true; bash $BENCH_SCRIPTS/clean-helpers.sh clean_build_output; $BENCH_WARM_BUILD_AUBE || true"} \ ${BENCH_INCLUDE_AUBE:+--command-name="aube" "$BENCH_COMMAND_AUBE"} \ diff --git a/suite/scripts/variations/build.sh b/suite/scripts/variations/build.sh index c0876b78e3..5e5fc0efdb 100644 --- a/suite/scripts/variations/build.sh +++ b/suite/scripts/variations/build.sh @@ -52,6 +52,10 @@ BENCH_COMMAND_NODE="timeout $BENCH_TIMEOUT node --run build > $BENCH_OUTPUT_FOLD # The install step is defined as a "prepare" option so that time spent # on the install is not accounted for in the benchmark results. # Before each timed run we also remove .next/ so every run is a cold build. +# deno's prepare also drops Deno's compiled-code cache: left in place, it makes +# the build fail whenever `next build` emits the other variant of a server chunk +# (same path and size, different content; see clean_deno_code_cache). So deno's +# timed build compiles next's code from scratch, as node does on every build. hyperfine --ignore-failure \ --time-unit=millisecond \ --export-json="$BENCH_OUTPUT_FOLDER/benchmarks.json" \ @@ -72,7 +76,7 @@ hyperfine --ignore-failure \ ${BENCH_INCLUDE_VLT:+--command-name="vlt" "$BENCH_COMMAND_VLT"} \ ${BENCH_INCLUDE_BUN:+--prepare="$BENCH_INSTALL_PREPARE_BUN; bash $BENCH_SCRIPTS/clean-helpers.sh clean_build_output"} \ ${BENCH_INCLUDE_BUN:+--command-name="bun" "$BENCH_COMMAND_BUN"} \ - ${BENCH_INCLUDE_DENO:+--prepare="$BENCH_INSTALL_PREPARE_DENO; bash $BENCH_SCRIPTS/clean-helpers.sh clean_build_output"} \ + ${BENCH_INCLUDE_DENO:+--prepare="$BENCH_INSTALL_PREPARE_DENO; bash $BENCH_SCRIPTS/clean-helpers.sh clean_deno_code_cache clean_build_output"} \ ${BENCH_INCLUDE_DENO:+--command-name="deno" "$BENCH_COMMAND_DENO"} \ ${BENCH_INCLUDE_AUBE:+--prepare="$BENCH_INSTALL_PREPARE_AUBE || true; bash $BENCH_SCRIPTS/clean-helpers.sh clean_build_output"} \ ${BENCH_INCLUDE_AUBE:+--command-name="aube" "$BENCH_COMMAND_AUBE"} \ From c07691fa5abac5a5e3ceb5ef2b348d1bea982bb1 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Wed, 18 Nov 2026 08:53:58 +0000 Subject: [PATCH 2/2] build: prune only the .next/ entries from Deno's code cache Removing the whole v8_code_cache_v2 made deno's timed build recompile next's own code, which users keep cached by default; only the entries for the regenerated .next/ files can go stale. clean_deno_code_cache now runs deno-code-cache.js, which opens the SQLite database with node:sqlite and deletes the codecache rows whose specifier is under the current project's .next/ (as given and with symlinks resolved), keeping everything else. If the database isn't shaped as expected it warns and removes the whole code cache as before. Tests cover the targeted delete, the fallback, a missing cache, and finding the cache through DENO_DIR / XDG_CACHE_HOME. Co-Authored-By: Claude Opus 5.5 --- suite/scripts/clean-helpers.sh | 12 ++- suite/scripts/clean-helpers.test.js | 121 +++++++++++++++++------- suite/scripts/deno-code-cache.js | 66 +++++++++++++ suite/scripts/variations/build-cache.sh | 12 +-- suite/scripts/variations/build.sh | 8 +- 5 files changed, 174 insertions(+), 45 deletions(-) create mode 100644 suite/scripts/deno-code-cache.js diff --git a/suite/scripts/clean-helpers.sh b/suite/scripts/clean-helpers.sh index d7793eb139..b16e182e8c 100644 --- a/suite/scripts/clean-helpers.sh +++ b/suite/scripts/clean-helpers.sh @@ -141,8 +141,11 @@ clean_deno_cache() { # in two such variants), Deno runs the old compiled code against the new file # and webpack's runtime throws `TypeError: e[o] is not a function`. A failed # run doesn't refresh the entry, so it keeps failing until the variant flips -# back. Remove just the compiled-code cache, not the npm cache or the rest of -# $DENO_DIR, so the next build compiles from the files on disk. (#215) +# back. Only the entries for the regenerated files can go stale, so remove just +# those for the current project's .next/ (deno-code-cache.js) and keep the rest: +# the entries for next's own files in node_modules are what a user's build runs +# with. If the database isn't shaped as expected, remove the whole code cache +# instead; the npm cache and the rest of $DENO_DIR always stay. (#215) # # `deno info --json` reports the directory wherever it is (DENO_DIR, XDG_CACHE_HOME # or the platform default); fall back to the Linux default if it can't be asked. @@ -154,6 +157,11 @@ clean_deno_code_cache() { if [ -z "$deno_dir" ]; then deno_dir="${DENO_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/deno}" fi + [ -e "$deno_dir/v8_code_cache_v2" ] || return 0 + if node "$(dirname "${BASH_SOURCE[0]}")/deno-code-cache.js" "$deno_dir/v8_code_cache_v2" "$PWD"; then + return 0 + fi + echo "Warning: couldn't prune Deno's code cache for $PWD/.next, removing all of it" for entry in "$deno_dir"/v8_code_cache_v2*; do safe_remove "$entry" done diff --git a/suite/scripts/clean-helpers.test.js b/suite/scripts/clean-helpers.test.js index 34426bac70..7400f9e369 100644 --- a/suite/scripts/clean-helpers.test.js +++ b/suite/scripts/clean-helpers.test.js @@ -2,6 +2,7 @@ const assert = require("node:assert/strict"); const fs = require("node:fs"); const os = require("node:os"); const path = require("node:path"); +const { pathToFileURL } = require("node:url"); const { execFileSync, spawnSync } = require("node:child_process"); const test = require("node:test"); @@ -281,73 +282,127 @@ test("clean_berry_cache removes berry's global folder, with and without XDG_DATA ]); }); -// Run clean_deno_code_cache against a fake deno dir and record what it -// removes. `denoDir` is what the fake `deno info --json` reports, or null for -// no deno on PATH (the fallback to DENO_DIR / XDG_CACHE_HOME / ~/.cache). -const denoCodeCacheRemovedBy = (denoDir, env = {}) => { +// Run clean_deno_code_cache from `cwd` against a fake deno dir (reported by a +// fake `deno info --json`, or found through env when `denoOnPath` is false) +// and record what it removes and what is left in the code cache database. +const { DatabaseSync } = require("node:sqlite"); +const CODECACHE_SCHEMA = + "CREATE TABLE codecache (specifier TEXT NOT NULL, type INTEGER NOT NULL, source_hash INTEGER NOT NULL, data BLOB NOT NULL, PRIMARY KEY (specifier, type))"; +const runDenoCodeCacheClean = ({ denoDir, denoOnPath = true, schema, rows = [], env = {} }) => { const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "deno-clean-test-")); try { const binDir = path.join(tempDir, "bin"); + const cwd = path.join(tempDir, "my project"); const removeLog = path.join(tempDir, "removed.txt"); fs.mkdirSync(binDir); + fs.mkdirSync(cwd); fs.writeFileSync(removeLog, ""); - if (denoDir) { + if (denoOnPath) { fs.writeFileSync( path.join(binDir, "deno"), `#!/bin/sh\n[ "$1 $2" = "info --json" ] || exit 1\nprintf '{\\n "version": 1,\\n "denoDir": "%s",\\n "npmCache": "%s/npm"\\n}\\n' "$FAKE_DENO_DIR" "$FAKE_DENO_DIR"\n`, { mode: 0o755 }, ); - // The code cache is a SQLite database with -wal and -shm sidecars; the - // npm cache sits next to it and must stay. + } + const dbPath = path.join(denoDir, "v8_code_cache_v2"); + if (schema) { + // Like Deno's: a WAL-mode database, with the npm cache next to it. fs.mkdirSync(path.join(denoDir, "npm"), { recursive: true }); - for (const name of ["v8_code_cache_v2", "v8_code_cache_v2-wal", "v8_code_cache_v2-shm"]) { - fs.writeFileSync(path.join(denoDir, name), ""); + const db = new DatabaseSync(dbPath); + db.exec("PRAGMA journal_mode = WAL"); + db.exec(schema); + for (const [specifier, type] of rows.map((r) => r.replace(/^/, pathToFileURL(cwd).href).split(" "))) { + db.prepare("INSERT INTO codecache VALUES (?, ?, ?, ?)").run(specifier, Number(type), 1, Buffer.from("code")); } + db.close(); } const result = spawnSync( "bash", ["-c", `source "$HELPERS" >/dev/null; safe_remove() { printf '%s\\n' "$1" >> "$REMOVE_LOG"; }; clean_deno_code_cache`], { encoding: "utf8", + cwd, env: { - // Only the fake bin dir (plus sed), so a real deno is never asked. - PATH: `${binDir}${path.delimiter}/usr/bin${path.delimiter}/bin`, - HOME: "/home/runner", + // Only the fake bin dir, this node and sed, so a real deno is never asked. + PATH: [binDir, path.dirname(process.execPath), "/usr/bin", "/bin"].join(path.delimiter), + HOME: path.join(tempDir, "home"), HELPERS: path.join(__dirname, "clean-helpers.sh"), REMOVE_LOG: removeLog, - FAKE_DENO_DIR: denoDir ?? "", + FAKE_DENO_DIR: denoDir, ...env, }, }, ); assert.equal(result.status, 0, result.stderr); - return fs.readFileSync(removeLog, "utf8").trim().split("\n").filter(Boolean); + let left = null; + if (fs.existsSync(dbPath) && schema === CODECACHE_SCHEMA) { + const db = new DatabaseSync(dbPath, { readOnly: true }); + left = db + .prepare("SELECT specifier, type FROM codecache ORDER BY specifier, type") + .all() + .map((r) => `${r.specifier.replace(pathToFileURL(cwd).href, "")} ${r.type}`) + .sort(); + db.close(); + } + return { + removed: fs.readFileSync(removeLog, "utf8").trim().split("\n").filter(Boolean), + left, + stdout: result.stdout.replaceAll(cwd, ""), + }; } finally { fs.rmSync(tempDir, { recursive: true, force: true }); + fs.rmSync(denoDir, { recursive: true, force: true }); } }; +const tempDenoDir = () => path.join(os.tmpdir(), `deno dir ${process.pid} ${Math.random().toString(36).slice(2)}`); -test("clean_deno_code_cache removes only v8_code_cache_v2* from the dir deno reports", () => { - const denoDir = path.join(os.tmpdir(), `deno dir ${process.pid}`); - try { - assert.deepEqual(denoCodeCacheRemovedBy(denoDir).sort(), [ - path.join(denoDir, "v8_code_cache_v2"), - path.join(denoDir, "v8_code_cache_v2-shm"), - path.join(denoDir, "v8_code_cache_v2-wal"), - ]); - } finally { - fs.rmSync(denoDir, { recursive: true, force: true }); - } +test("clean_deno_code_cache removes only the entries under the project's .next/", () => { + const { removed, left, stdout } = runDenoCodeCacheClean({ + denoDir: tempDenoDir(), + schema: CODECACHE_SCHEMA, + rows: [ + "/.next/server/chunks/961.js 0", + "/.next/server/chunks/961.js 1", + "/.next/server/webpack-runtime.js 1", + "/node_modules/next/dist/build/index.js 1", + "/.next-other/x.js 1", + "file:///other%20project/.next/server/a.js 1", + ], + }); + assert.deepEqual(removed, [], "fell back to removing the whole cache"); + assert.deepEqual(left, [ + "/.next-other/x.js 1", + "/node_modules/next/dist/build/index.js 1", + "file:///other%20project/.next/server/a.js 1", + ]); + assert.match(stdout, /removed 3 of 6 entries under \/\.next, kept 3/); }); -test("clean_deno_code_cache falls back to DENO_DIR, XDG_CACHE_HOME, then ~/.cache", () => { - // Without deno nothing exists to glob, so the literal pattern is what - // safe_remove is asked about. - assert.deepEqual(denoCodeCacheRemovedBy(null, { DENO_DIR: "/deno dir" }), ["/deno dir/v8_code_cache_v2*"]); - assert.deepEqual(denoCodeCacheRemovedBy(null, { XDG_CACHE_HOME: "/xdg/cache" }), [ - "/xdg/cache/deno/v8_code_cache_v2*", - ]); - assert.deepEqual(denoCodeCacheRemovedBy(null), ["/home/runner/.cache/deno/v8_code_cache_v2*"]); +test("clean_deno_code_cache removes the whole code cache when the schema is not the expected one", () => { + const denoDir = tempDenoDir(); + const { removed, stdout } = runDenoCodeCacheClean({ + denoDir, + schema: "CREATE TABLE codecache (key TEXT PRIMARY KEY, data BLOB NOT NULL)", + }); + // Closing the test database checkpointed its -wal and -shm away; the glob + // covers them when Deno leaves them behind. + assert.deepEqual(removed, [path.join(denoDir, "v8_code_cache_v2")]); + assert.match(stdout, /Warning: couldn't prune Deno's code cache/); +}); + +test("clean_deno_code_cache does nothing without a code cache", () => { + assert.deepEqual(runDenoCodeCacheClean({ denoDir: tempDenoDir() }).removed, []); +}); + +test("clean_deno_code_cache finds the cache through DENO_DIR or XDG_CACHE_HOME when deno isn't on PATH", () => { + const badSchema = "CREATE TABLE codecache (key TEXT PRIMARY KEY, data BLOB NOT NULL)"; + for (const [denoDir, env] of [ + [tempDenoDir(), (d) => ({ DENO_DIR: d })], + [path.join(tempDenoDir(), "deno"), (d) => ({ XDG_CACHE_HOME: path.dirname(d) })], + ]) { + const { removed } = runDenoCodeCacheClean({ denoDir, denoOnPath: false, schema: badSchema, env: env(denoDir) }); + assert.deepEqual(removed, [path.join(denoDir, "v8_code_cache_v2")]); + } }); test("clean_nx_cache does nothing outside an Nx workspace", () => { diff --git a/suite/scripts/deno-code-cache.js b/suite/scripts/deno-code-cache.js new file mode 100644 index 0000000000..ce0e94b8cc --- /dev/null +++ b/suite/scripts/deno-code-cache.js @@ -0,0 +1,66 @@ +#!/usr/bin/env node +// Remove the entries for one project's `.next/` output from Deno's compiled-code +// cache, and keep everything else (next's own files in node_modules never go +// stale). Called by clean_deno_code_cache in clean-helpers.sh: +// +// node deno-code-cache.js +// +// The cache is a SQLite database in WAL mode with one table, +// codecache (specifier TEXT, type INTEGER, source_hash INTEGER, data BLOB, +// PRIMARY KEY (specifier, type)) +// where specifier is the module's file:// URL. Prints what it removed. Exits 1, +// with the reason on stderr, when the database isn't shaped like that, so the +// caller can fall back to removing the whole cache. +const fs = require("node:fs"); +const path = require("node:path"); +const { pathToFileURL } = require("node:url"); +const { DatabaseSync } = require("node:sqlite"); + +const EXPECTED_COLUMNS = ["specifier", "type", "source_hash", "data"]; + +const main = (dbPath, projectDir) => { + if (!dbPath || !projectDir) { + throw new Error("usage: deno-code-cache.js "); + } + if (!fs.existsSync(dbPath)) { + return; + } + const db = new DatabaseSync(dbPath); + try { + const columns = db + .prepare("SELECT name FROM pragma_table_info('codecache')") + .all() + .map((row) => row.name); + if (columns.join() !== EXPECTED_COLUMNS.join()) { + throw new Error(`unexpected codecache columns: ${JSON.stringify(columns)}`); + } + const total = db.prepare("SELECT count(*) AS n FROM codecache").get().n; + let removed = 0; + const remove = db.prepare("DELETE FROM codecache WHERE specifier LIKE ? ESCAPE '\\'"); + for (const prefix of nextOutputUrls(projectDir)) { + removed += remove.run(`${prefix.replace(/[\\%_]/g, "\\$&")}%`).changes; + } + console.log( + `deno code cache: removed ${removed} of ${total} entries under ${path.join(projectDir, ".next")}, kept ${total - removed}`, + ); + } finally { + db.close(); + } +}; + +// file:// URL prefixes of the project's .next/ directory: as given and with +// symlinks resolved, since Deno records the path it loaded the file from. +const nextOutputUrls = (projectDir) => { + const dirs = new Set([path.resolve(projectDir)]); + try { + dirs.add(fs.realpathSync(projectDir)); + } catch {} + return [...dirs].map((dir) => `${pathToFileURL(path.join(dir, ".next")).href}/`); +}; + +try { + main(process.argv[2], process.argv[3]); +} catch (err) { + console.error(`deno-code-cache.js: ${err.message}`); + process.exit(1); +} diff --git a/suite/scripts/variations/build-cache.sh b/suite/scripts/variations/build-cache.sh index 3a46abdefe..a684145cde 100644 --- a/suite/scripts/variations/build-cache.sh +++ b/suite/scripts/variations/build-cache.sh @@ -65,12 +65,12 @@ BENCH_COMMAND_NODE="timeout $BENCH_TIMEOUT node --run build > $BENCH_OUTPUT_FOLD # Run the benchmark build-cache benchmarks. # Prepare: install deps + run a warm build (populates .next/ cache). # Timed command: rebuild with .next/ cache present. -# deno's prepare drops Deno's compiled-code cache twice: before the warm build, -# so it can't fail on a stale entry from the previous iteration and leave a -# half-written .next/, and again right before the timed rebuild, in case the -# rebuild emits the other variant of a server chunk than its warm build did -# (see clean_deno_code_cache). A cached rebuild loads little code, so a cold -# compile cache costs it nothing measurable. +# deno's prepare drops the .next/ entries from Deno's compiled-code cache twice: +# before the warm build, so it can't fail on a stale entry from the previous +# iteration and leave a half-written .next/, and again right before the timed +# rebuild, in case the rebuild emits the other variant of a server chunk than +# its warm build did (see clean_deno_code_cache). The entries for next's own +# code stay, so the rebuild runs with the warm compile cache a user's would. hyperfine --ignore-failure \ --time-unit=millisecond \ --export-json="$BENCH_OUTPUT_FOLDER/benchmarks.json" \ diff --git a/suite/scripts/variations/build.sh b/suite/scripts/variations/build.sh index 5e5fc0efdb..5cd0fe114e 100644 --- a/suite/scripts/variations/build.sh +++ b/suite/scripts/variations/build.sh @@ -52,10 +52,10 @@ BENCH_COMMAND_NODE="timeout $BENCH_TIMEOUT node --run build > $BENCH_OUTPUT_FOLD # The install step is defined as a "prepare" option so that time spent # on the install is not accounted for in the benchmark results. # Before each timed run we also remove .next/ so every run is a cold build. -# deno's prepare also drops Deno's compiled-code cache: left in place, it makes -# the build fail whenever `next build` emits the other variant of a server chunk -# (same path and size, different content; see clean_deno_code_cache). So deno's -# timed build compiles next's code from scratch, as node does on every build. +# deno's prepare also drops the entries for .next/ from Deno's compiled-code +# cache: left in place, they make the build fail whenever `next build` emits the +# other variant of a server chunk (same path and size, different content; see +# clean_deno_code_cache). The entries for next's own code stay. hyperfine --ignore-failure \ --time-unit=millisecond \ --export-json="$BENCH_OUTPUT_FOLDER/benchmarks.json" \