Repository navigation
build: clear Deno's compiled-code cache before deno's next builds #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -134,6 +134,39 @@ 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. 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. | ||||||||||
| 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 | ||||||||||
| [ -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" | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fail when a Deno code-cache entry remains after removal.
Keep Suggested fix
Committable suggestion
|
||||||||||
| done | ||||||||||
| } | ||||||||||
|
|
||||||||||
| # Function to safely clean vp cache | ||||||||||
| clean_vp_cache() { | ||||||||||
| if command -v vp &> /dev/null; then | ||||||||||
|
|
@@ -322,6 +355,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 +408,9 @@ else | |||||||||
| clean_deno_cache) | ||||||||||
| clean_deno_cache | ||||||||||
| ;; | ||||||||||
| clean_deno_code_cache) | ||||||||||
| clean_deno_code_cache | ||||||||||
| ;; | ||||||||||
| clean_aube_cache) | ||||||||||
| clean_aube_cache | ||||||||||
| ;; | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <path to v8_code_cache_v2> <project dir> | ||
| // | ||
| // 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 <v8_code_cache_v2> <project dir>"); | ||
| } | ||
| 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); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decode the JSON string before using
denoDir.The
sedexpression on line 152 copies the JSON string as written. A cache path with a backslash therefore keeps each backslash doubled. The non-empty value skips the fallback. The quoted glob on line 157 then targets a directory name that does not exist, so the stalev8_code_cache_v2*files remain. Decode the escaped backslashes before the path is used. Platforms whose cache paths always contain backslashes can hit this on every run.Suggested fix
Committable suggestion