From 8d94ed06570adb2b7300cf16fe9f43b4680e3f2b Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Fri, 23 Oct 2026 03:16:09 -0700 Subject: [PATCH] perf(test): bundle Zod once per isolated Vitest invocation (#32298) --- config/scripts/run-vitest.mjs | 14 ++++++++++++-- config/scripts/vitest-zod-bundle.mjs | 28 ++++++++++++++++++++++++++++ config/vitest.config.ts | 3 +++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 config/scripts/vitest-zod-bundle.mjs diff --git a/config/scripts/run-vitest.mjs b/config/scripts/run-vitest.mjs index 0cca629306d..1091c6ca313 100644 --- a/config/scripts/run-vitest.mjs +++ b/config/scripts/run-vitest.mjs @@ -1,5 +1,6 @@ import { resolve } from 'node:path' import { runProcessSync } from '@orca/process-host' +import { createVitestZodBundle } from './vitest-zod-bundle.mjs' import { checkBunVersion, probeBunVersion, @@ -18,7 +19,9 @@ if (bunCheck.message) { if (!bunCheck.ok) { process.exitCode = 1 } else { + let zodBundle try { + zodBundle = createVitestZodBundle(root) const result = runProcessSync({ program: pinnedBun ?? 'bun', args: ['--bun', resolve(root, 'node_modules/vitest/vitest.mjs'), ...process.argv.slice(2)], @@ -27,7 +30,8 @@ if (!bunCheck.ok) { ...process.env, ORCA_BACKGROUND_LAUNCH: '1', ORCA_TEST_NODE_EXECUTABLE: process.execPath, - ORCA_TEST_NODE_VERSION: process.versions.node + ORCA_TEST_NODE_VERSION: process.versions.node, + ORCA_UNIT_ZOD_BUNDLE: zodBundle.path }, stdio: 'inherit', timeoutMs: null @@ -35,7 +39,13 @@ if (!bunCheck.ok) { process.exitCode = result.code ?? 1 } catch (error) { - console.error('Could not start Vitest. Install the Bun version in config/.bun-version.', error) + console.error('Could not prepare or start Vitest:', error) process.exitCode = 1 + } finally { + try { + zodBundle?.cleanup() + } catch (error) { + console.warn('Could not remove the temporary Vitest Zod bundle:', error) + } } } diff --git a/config/scripts/vitest-zod-bundle.mjs b/config/scripts/vitest-zod-bundle.mjs new file mode 100644 index 00000000000..e51cdb5de98 --- /dev/null +++ b/config/scripts/vitest-zod-bundle.mjs @@ -0,0 +1,28 @@ +import { mkdirSync, mkdtempSync, rmSync } from 'node:fs' +import { join } from 'node:path' +import { fileURLToPath } from 'node:url' +import { buildSync } from 'esbuild' + +export function createVitestZodBundle(root) { + const temporary = join(root, '.tmp') + mkdirSync(temporary, { recursive: true }) + const directory = mkdtempSync(join(temporary, 'vitest-zod-')) + const path = join(directory, 'zod.mjs') + const cleanup = () => rmSync(directory, { recursive: true, force: true, maxRetries: 3 }) + try { + buildSync({ + absWorkingDir: root, + entryPoints: [fileURLToPath(import.meta.resolve('zod'))], + outfile: path, + bundle: true, + platform: 'node', + format: 'esm', + target: 'node24', + sourcemap: true + }) + return { path, cleanup } + } catch (error) { + cleanup() + throw error + } +} diff --git a/config/vitest.config.ts b/config/vitest.config.ts index fb2b889a7dc..aac0a83051f 100644 --- a/config/vitest.config.ts +++ b/config/vitest.config.ts @@ -39,6 +39,9 @@ const transforms = { }, resolve: { alias: [ + ...(process.env.ORCA_UNIT_ZOD_BUNDLE + ? [{ find: /^zod$/, replacement: process.env.ORCA_UNIT_ZOD_BUNDLE }] + : []), ...createWorkspaceSourceResolver().aliases, { find: '@renderer', replacement: resolve('src/renderer/src') }, { find: '@', replacement: resolve('src/renderer/src') },