From 022835cce629ea4ca98626e3c1d6c5a1429eea62 Mon Sep 17 00:00:00 2001 From: Sam Ottenhoff Date: Wed, 25 Mar 2026 15:01:01 -0400 Subject: [PATCH] perf: make updater faster by streaming instead of reading into memory (#3630) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix system crash (OOM) caused by updater spending time in folders like docs/ - Skip hidden and vendor directories (node_modules, vendor, docs, etc.) to avoid OOM - free ASTs immediately - ts-morph dependency bumped to ^27.0.2 Closes https://github.com/denoland/fresh/issues/3483 --------- Co-authored-by: Bartek Iwańczuk Co-authored-by: Claude Opus 4.6 (1M context) --- deno.json | 2 +- deno.lock | 46 +++++++++++----------- packages/update/src/update.ts | 62 +++++++++++++++++------------- packages/update/src/update_test.ts | 5 ++- 4 files changed, 63 insertions(+), 52 deletions(-) diff --git a/deno.json b/deno.json index dbd5e8a9..8baef262 100644 --- a/deno.json +++ b/deno.json @@ -86,7 +86,7 @@ "tailwindcss": "npm:tailwindcss@^4.1.10", "postcss": "npm:postcss@8.5.6", - "ts-morph": "npm:ts-morph@^26.0.0", + "ts-morph": "npm:ts-morph@^27.0.2", "@std/front-matter": "jsr:@std/front-matter@^1.0.5", "github-slugger": "npm:github-slugger@^2.0.0", diff --git a/deno.lock b/deno.lock index 57fd40b1..35af7ce1 100644 --- a/deno.lock +++ b/deno.lock @@ -120,7 +120,7 @@ "npm:stripe@^19.1.0": "19.1.0_@types+node@24.9.2", "npm:tailwindcss@^3.4.17": "3.4.18_postcss@8.5.6_jiti@1.21.7", "npm:tailwindcss@^4.1.10": "4.1.16", - "npm:ts-morph@26": "26.0.0", + "npm:ts-morph@^27.0.2": "27.0.2", "npm:vite-plugin-inspect@^11.3.2": "11.3.3_vite@7.3.1__@types+node@24.9.2__picomatch@4.0.3_@types+node@24.9.2", "npm:vite@^7.1.4": "7.3.1_@types+node@24.9.2_picomatch@4.0.3", "npm:vite@^7.3.1": "7.3.1_@types+node@24.9.2_picomatch@4.0.3" @@ -863,15 +863,6 @@ "@ioredis/commands@1.4.0": { "integrity": "sha512-aFT2yemJJo+TZCmieA7qnYGQooOS7QfNmYrzGtsYd3g9j5iDP8AimYYAesf79ohjbLG12XxC4nG5DyEnC88AsQ==" }, - "@isaacs/balanced-match@4.0.1": { - "integrity": "sha512-yzMTt9lEb8Gv7zRioUilSglI0c0smZ9k5D65677DLWLtWJaXIS3CqcGyUFByYKlnUj6TkjLVs54fBl6+TiGQDQ==" - }, - "@isaacs/brace-expansion@5.0.0": { - "integrity": "sha512-ZT55BDLV0yv0RBm2czMiZ+SqCGO7AvmOM3G/w2xhVPH+te0aKgFjmBvGlL1dH+ql2tgGO3MVrbb3jCKyvpgnxA==", - "dependencies": [ - "@isaacs/balanced-match" - ] - }, "@isaacs/cliui@8.0.2": { "integrity": "sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==", "dependencies": [ @@ -1979,12 +1970,12 @@ "@trysound/sax@0.2.0": { "integrity": "sha512-L7z9BgrNEcYyUYtF+HaEfiS5ebkh9jXqbszz7pC0hRBPaatV0XjSD3+eHrpqFemQfgwiFF0QPIarnIihIDn7OA==" }, - "@ts-morph/common@0.27.0": { - "integrity": "sha512-Wf29UqxWDpc+i61k3oIOzcUfQt79PIT9y/MWfAGlrkjg6lBC1hwDECLXPVJAhWjiGbfBCxZd65F/LIZF3+jeJQ==", + "@ts-morph/common@0.28.1": { + "integrity": "sha512-W74iWf7ILp1ZKNYXY5qbddNaml7e9Sedv5lvU1V8lftlitkc9Pq1A+jlH23ltDgWYeZFFEqGCD1Ies9hqu3O+g==", "dependencies": [ - "fast-glob", - "minimatch@10.1.1", - "path-browserify" + "minimatch@10.2.4", + "path-browserify", + "tinyglobby" ] }, "@types/babel__core@7.20.5": { @@ -2095,6 +2086,9 @@ "balanced-match@1.0.2": { "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, + "balanced-match@4.0.4": { + "integrity": "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==" + }, "baseline-browser-mapping@2.8.21": { "integrity": "sha512-JU0h5APyQNsHOlAM7HnQnPToSDQoEBZqzu/YBlqDnEeymPnZDREeXJA3KBMQee+dKteAxZ2AtvQEvVYdZf241Q==", "bin": true @@ -2111,7 +2105,13 @@ "brace-expansion@2.0.2": { "integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==", "dependencies": [ - "balanced-match" + "balanced-match@1.0.2" + ] + }, + "brace-expansion@5.0.5": { + "integrity": "sha512-VZznLgtwhn+Mact9tfiwx64fA9erHH/MCXEUfB/0bX/6Fz6ny5EGTXYltMocqg4xFAQZtnO3DHWWXi8RiuN7cQ==", + "dependencies": [ + "balanced-match@4.0.4" ] }, "braces@3.0.3": { @@ -2902,16 +2902,16 @@ "mime-db@1.54.0": { "integrity": "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==" }, - "minimatch@10.1.1": { - "integrity": "sha512-enIvLvRAFZYXJzkCYG5RKmPfrFArdLv+R+lbQ53BmIMLIry74bjKzX6iHAm8WYamJkhSSEabrWN5D97XnKObjQ==", + "minimatch@10.2.4": { + "integrity": "sha512-oRjTw/97aTBN0RHbYCdtF1MQfvusSIBQM0IZEgzl6426+8jSC0nF1a/GmnVLpfB9yyr6g6FTqWqiZVbxrtaCIg==", "dependencies": [ - "@isaacs/brace-expansion" + "brace-expansion@5.0.5" ] }, "minimatch@9.0.5": { "integrity": "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow==", "dependencies": [ - "brace-expansion" + "brace-expansion@2.0.2" ] }, "minipass@7.1.2": { @@ -3780,8 +3780,8 @@ "ts-interface-checker@0.1.13": { "integrity": "sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==" }, - "ts-morph@26.0.0": { - "integrity": "sha512-ztMO++owQnz8c/gIENcM9XfCEzgoGphTv+nKpYNM1bgsdOVC/jRZuEBf6N+mLLDNg68Kl+GgUZfOySaRiG1/Ug==", + "ts-morph@27.0.2": { + "integrity": "sha512-fhUhgeljcrdZ+9DZND1De1029PrE+cMkIP7ooqkLRTrRLTqcki2AstsyJm0vRNbTbVCNJ0idGlbBrfqc7/nA8w==", "dependencies": [ "@ts-morph/common", "code-block-writer" @@ -4064,7 +4064,7 @@ "npm:redis@^5.8.2", "npm:rollup@^4.55.1", "npm:tailwindcss@^4.1.10", - "npm:ts-morph@26", + "npm:ts-morph@^27.0.2", "npm:vite@^7.3.1" ], "members": { diff --git a/packages/update/src/update.ts b/packages/update/src/update.ts index c223784c..826edb86 100644 --- a/packages/update/src/update.ts +++ b/packages/update/src/update.ts @@ -3,6 +3,7 @@ import * as JSONC from "@std/jsonc"; import * as tsmorph from "ts-morph"; import * as colors from "@std/fmt/colors"; import { ProgressBar } from "@std/cli/unstable-progress-bar"; +import { walk } from "@std/fs/walk"; export const SyntaxKind = tsmorph.ts.SyntaxKind; @@ -10,8 +11,10 @@ export const FRESH_VERSION = "2.2.1"; export const PREACT_VERSION = "10.28.3"; export const PREACT_SIGNALS_VERSION = "2.7.1"; -// Function to filter out node_modules and vendor directories from logs -const HIDE_FILES = /[\\/]+(node_modules|vendor)[\\/]+/; +// Directories to skip when walking the project tree. Covers hidden directories, +// dependency caches, and common build output folders. +const SKIP_DIRS = + /(?:[\\/]\.[^\\/]+(?:[\\/]|$)|[\\/](node_modules|vendor|\.git|\.next|\.turbo|_fresh|dist|target|\.cache)(?:[\\/]|$))/; export interface DenoJson { lock?: boolean; tasks?: Record; @@ -170,17 +173,26 @@ export async function updateProject(dir: string) { // Update routes folder const project = new tsmorph.Project(); - const sfs = project.addSourceFilesAtPaths( - path.join(dir, "**", "*.{js,jsx,ts,tsx}"), - ); - // Filter out node_modules and vendor files for user display - const userFiles = sfs.filter((sf) => !HIDE_FILES.test(sf.getFilePath())); + // Collect file paths so we can size the progress bar. + const filesToProcess: string[] = []; + for await ( + const entry of walk(dir, { + includeDirs: false, + includeFiles: true, + exts: ["js", "jsx", "ts", "tsx"], + skip: [SKIP_DIRS], + }) + ) { + filesToProcess.push(entry.path); + } // deno-lint-ignore no-console - console.log(colors.cyan(`📁 Found ${userFiles.length} files to process`)); + console.log( + colors.cyan(`📁 Found ${filesToProcess.length} files to process`), + ); - if (sfs.length === 0) { + if (filesToProcess.length === 0) { // deno-lint-ignore no-console console.log(colors.green("🎉 Migration completed successfully!")); return; @@ -195,21 +207,21 @@ export async function updateProject(dir: string) { // Create a progress bar const bar = new ProgressBar({ - max: sfs.length, + max: filesToProcess.length, formatter(x) { return `[${x.styledTime}] [${x.progressBar}] [${x.value}/${x.max} files]`; }, }); - // process files sequentially to show proper progress - await Promise.all(sfs.map(async (sourceFile) => { + // Process each file one-by-one to keep memory flat. We add a SourceFile, + // transform it, then immediately forget it so ts-morph releases the AST. + for (const filePath of filesToProcess) { + const sourceFile = project.addSourceFileAtPath(filePath); try { const wasModified = await updateFile(sourceFile); if (wasModified) { modifiedFilesList.push(sourceFile.getFilePath()); } - - return wasModified; } catch (err) { // deno-lint-ignore no-console console.error(`Could not process ${sourceFile.getFilePath()}`); @@ -217,36 +229,34 @@ export async function updateProject(dir: string) { } finally { completedFiles++; bar.value = completedFiles; + // Drop the AST to avoid unbounded memory growth + sourceFile.forget(); + project.removeSourceFile(sourceFile); } - })); + } // Clear the progress line and add a newline await bar.stop(); - // Filter modified files to show only user files - const modifiedFilesToShow = modifiedFilesList.filter((filePath) => - !HIDE_FILES.test(filePath) - ); - // add migration summary // deno-lint-ignore no-console console.log("\n" + colors.bold("📊 Migration Summary:")); // deno-lint-ignore no-console - console.log(` Total files processed: ${userFiles.length}`); + console.log(` Total files processed: ${filesToProcess.length}`); // deno-lint-ignore no-console - console.log(` Successfully modified: ${modifiedFilesToShow.length}`); + console.log(` Successfully modified: ${modifiedFilesList.length}`); // deno-lint-ignore no-console console.log( ` Unmodified (no changes needed): ${ - userFiles.length - modifiedFilesToShow.length + filesToProcess.length - modifiedFilesList.length }`, ); - // Display modified files list (filtered) - if (modifiedFilesToShow.length > 0) { + // Display modified files list + if (modifiedFilesList.length > 0) { // deno-lint-ignore no-console console.log("\n" + colors.bold("📝 Modified Files:")); - modifiedFilesToShow.forEach((filePath) => { + modifiedFilesList.forEach((filePath) => { // show relative path let relativePath = path.relative(dir, filePath); // Ensure consistent path separators for logging diff --git a/packages/update/src/update_test.ts b/packages/update/src/update_test.ts index e2abdac7..943c11cd 100644 --- a/packages/update/src/update_test.ts +++ b/packages/update/src/update_test.ts @@ -714,11 +714,12 @@ export default function Foo(props: PageProps) { const files = await readFiles(dir); + // Should leave dependency trees untouched expect(files["/node_modules/foo/bar.ts"]).toEqual( - `import { IS_BROWSER } from "fresh/runtime";`, + `import { IS_BROWSER } from "$fresh/runtime.ts";`, ); expect(files["/vendor/foo/bar.ts"]).toEqual( - `import { IS_BROWSER } from "fresh/runtime";`, + `import { IS_BROWSER } from "$fresh/runtime.ts";`, ); expect(files["/routes/index.tsx"]).toEqual( `import { PageProps } from "fresh";