diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index d0b6347805d..4d52e46d4a1 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -123,6 +123,12 @@ graph. Local affected tests do not replace CI. Use `pnpm test`, not raw `vitest`: only it keeps the transform cache that makes repeat runs fast. +Most unit files share a worker and its module cache with the files before them. After each file +the worker undoes mocks, fake timers, stubs, env, cwd, and leftover timers, listeners, and globals. +Files using `vi.doMock`, `vi.resetModules`, a DOM environment, or direct `globalThis` writes keep +their own worker. If a file passes alone but fails after others, add `// @orca-isolated-worker` +to it; `ORCA_VITEST_ISOLATE_EVERY_FILE=1` isolates every file to confirm a leak. + To reduce main-process rebuild CPU and memory when you do not need its source maps, set `ORCA_DEV_MAIN_SOURCEMAPS=0` before `pnpm dev`. In Bash/zsh: diff --git a/config/scripts/test-runtime/environment/vitest-shared-worker-file-reset.mjs b/config/scripts/test-runtime/environment/vitest-shared-worker-file-reset.mjs new file mode 100644 index 00000000000..dff75d916d4 --- /dev/null +++ b/config/scripts/test-runtime/environment/vitest-shared-worker-file-reset.mjs @@ -0,0 +1,201 @@ +// Shared-worker files run back to back in one module cache; undo each file's mocks and process state. +// A file that leaves timers, listeners or globals behind also drops the module cache, as isolation would. +// Plain JS: it rewires Vitest's mocker and the timer globals, which have no public types. +import { promisify } from 'node:util' +import { afterAll, vi } from 'vitest' + +const VITEST_INTERNAL = /\/vitest\/dist\/|\/@vitest\/|vitest-virtual-/ +// Registries their owners create on first use and look up again on every access. +const LAZY_GLOBAL_REGISTRY = /^(?:Symbol\(orca\.|__zod_global)/ +const PROCESS_KEYS = ['platform', 'arch', 'execPath', 'argv', 'exitCode'] + +function resetNode(node) { + node.evaluated = false + node.promise = undefined + node.exports = undefined +} + +// Same reset Vitest's isolation applies, limited to modules that saw the mocked one. +function resetImportersOf(modules, id) { + const pending = [...(modules.getModuleById(id)?.importers ?? [])] + const seen = new Set() + while (pending.length > 0) { + const importer = pending.pop() + if (seen.has(importer) || VITEST_INTERNAL.test(importer)) { + continue + } + seen.add(importer) + const node = modules.getModuleById(importer) + if (node) { + resetNode(node) + pending.push(...node.importers) + } + } +} + +function resetAllModules(modules) { + for (const [id, node] of modules.idToModuleMap) { + if (!VITEST_INTERNAL.test(id)) { + resetNode(node) + node.importers.clear() + } + } +} + +function trackTimers(state) { + const real = { setTimeout, setInterval, clearTimeout, clearInterval } + function trackedSetTimeout(callback, ms, ...args) { + if (typeof callback !== 'function') { + return real.setTimeout(callback, ms, ...args) + } + const handle = real.setTimeout( + (...callbackArgs) => { + state.pendingTimers.delete(handle) + callback(...callbackArgs) + }, + ms, + ...args + ) + state.pendingTimers.add(handle) + return handle + } + trackedSetTimeout[promisify.custom] = real.setTimeout[promisify.custom] + globalThis.setTimeout = trackedSetTimeout + globalThis.setInterval = (...args) => { + const handle = real.setInterval(...args) + state.pendingTimers.add(handle) + return handle + } + globalThis.clearTimeout = (handle) => { + state.pendingTimers.delete(handle) + real.clearTimeout(handle) + } + globalThis.clearInterval = (handle) => { + state.pendingTimers.delete(handle) + real.clearInterval(handle) + } + state.clearTimer = (handle) => { + real.clearTimeout(handle) + real.clearInterval(handle) + } +} + +// The mocker lives across files, unlike this module, so it carries the once-per-worker state. +function installOnce(mocker) { + if (mocker.orcaSharedWorkerState) { + return mocker.orcaSharedWorkerState + } + const mockPath = mocker.mockPath.bind(mocker) + // Modules cached by an earlier file hold the real binding; re-run them against this file's mock. + mocker.mockPath = (originalId, id, ...rest) => { + mockPath(originalId, id, ...rest) + resetImportersOf(mocker.evaluatedModules, id) + } + const reset = mocker.reset.bind(mocker) + // And re-run them against the real module before the next file imports them. + mocker.reset = () => { + for (const registry of mocker.registries.values()) { + for (const url of registry.keys()) { + resetImportersOf(mocker.evaluatedModules, registry.get(url).id) + } + } + reset() + } + const state = { pendingTimers: new Set(), clearTimer: () => {} } + trackTimers(state) + mocker.orcaSharedWorkerState = state + return state +} + +const mocker = globalThis.__vitest_mocker__ +const state = mocker ? installOnce(mocker) : undefined +state?.pendingTimers.clear() + +const initialGlobals = Object.getOwnPropertyDescriptors(globalThis) +const initialProcess = Object.fromEntries( + PROCESS_KEYS.map((key) => [key, Object.getOwnPropertyDescriptor(process, key)]) +) +const initialListeners = new Map( + process.eventNames().map((event) => [event, new Set(process.listeners(event))]) +) +const initialEnv = { ...process.env } +const initialCwd = process.cwd() +const evaluatedBefore = new Set( + [...(mocker?.evaluatedModules.idToModuleMap ?? [])] + .filter(([, node]) => node.evaluated) + .map(([id]) => id) +) + +/** Puts process state back and reports what this file had left behind. */ +function restoreProcessState() { + const leaks = { globals: [], process: [] } + for (const key of Reflect.ownKeys(globalThis)) { + if (!(key in initialGlobals) && !LAZY_GLOBAL_REGISTRY.test(String(key))) { + leaks.globals.push(String(key)) + Reflect.deleteProperty(globalThis, key) + } + } + for (const [key, descriptor] of Object.entries(initialGlobals)) { + const current = Object.getOwnPropertyDescriptor(globalThis, key) + if (!Object.is(current?.value, descriptor.value) || current?.get !== descriptor.get) { + leaks.globals.push(key) + try { + Object.defineProperty(globalThis, key, descriptor) + } catch { + // Non-configurable globals cannot be put back; the module reset below still applies. + } + } + } + for (const key of PROCESS_KEYS) { + const descriptor = initialProcess[key] + if (descriptor) { + Object.defineProperty(process, key, descriptor) + } else { + Reflect.deleteProperty(process, key) + } + } + for (const event of process.eventNames()) { + const before = initialListeners.get(event) + for (const listener of process.listeners(event)) { + if (!before?.has(listener)) { + leaks.process.push(`${String(event)} listener`) + process.removeListener(event, listener) + } + } + } + for (const handle of state?.pendingTimers ?? []) { + leaks.process.push('timer') + state.clearTimer(handle) + } + state?.pendingTimers.clear() + for (const key of Object.keys(process.env)) { + if (!(key in initialEnv)) { + delete process.env[key] + } + } + Object.assign(process.env, initialEnv) + if (process.cwd() !== initialCwd) { + process.chdir(initialCwd) + } + return leaks +} + +afterAll(() => { + // Runs Vitest's end-of-file spy restore now: a spy restored by hand would otherwise be re-applied after this hook. + vi.restoreAllMocks() + vi.useRealTimers() + vi.unstubAllEnvs() + vi.unstubAllGlobals() + const leaks = restoreProcessState() + if (mocker && leaks.process.length > 0) { + // Any cached module may own a cleared timer or listener. + resetAllModules(mocker.evaluatedModules) + } else if (mocker && leaks.globals.length > 0) { + // A module this file loaded may have installed the global it now expects. + for (const [id, node] of mocker.evaluatedModules.idToModuleMap) { + if (node.evaluated && !evaluatedBefore.has(id) && !VITEST_INTERNAL.test(id)) { + resetNode(node) + } + } + } +}) diff --git a/config/scripts/test-runtime/execution-policy/vitest-shared-worker-files.mjs b/config/scripts/test-runtime/execution-policy/vitest-shared-worker-files.mjs new file mode 100644 index 00000000000..e436fafecca --- /dev/null +++ b/config/scripts/test-runtime/execution-policy/vitest-shared-worker-files.mjs @@ -0,0 +1,94 @@ +import { readFileSync, statSync } from 'node:fs' +import { dirname, join, relative, resolve } from 'node:path' + +// vitest-shared-worker-file-reset.mjs undoes mocks, timers, stubs, env and cwd after each file. +// These can't be undone between files, so a file that uses them keeps its own worker. +const ISOLATION_REQUIRING_API = new RegExp( + [ + // Opt-out for a file whose state leaks in ways the reset cannot see. + String.raw`@orca-isolated-worker\b`, + String.raw`\bvi\.(?:doMock|unmock|doUnmock|resetModules)\(`, + String.raw`@vitest-environment\s+(?!node\b)`, + String.raw`\bglobalThis(?:\.\w+|\[[^\]]+\])\s*=[^=]`, + String.raw`\bObject\.defineProperty\(\s*(?:globalThis|process|navigator|window|document)\b` + ].join('|') +) +const IMPORT_SPECIFIER = /(?:\bfrom\s*|\bimport\s*\(\s*|\bimport\s+)['"]([^'"]+)['"]/g +const SOURCE_EXTENSIONS = ['.ts', '.tsx', '.mts', '.mjs', '.js'] + +function isFile(path) { + try { + return statSync(path).isFile() + } catch { + return false + } +} + +function resolveLocal(fromFile, specifier, aliases) { + let base = null + if (specifier.startsWith('./') || specifier.startsWith('../')) { + base = resolve(dirname(fromFile), specifier) + } else { + const alias = aliases.find(({ find }) => specifier === find || specifier.startsWith(`${find}/`)) + if (!alias) { + return null + } + base = join(alias.replacement, specifier.slice(alias.find.length)) + } + const stem = base.replace(/\.[cm]?jsx?$/, '') + const candidates = [ + base, + ...SOURCE_EXTENSIONS.map((extension) => stem + extension), + ...SOURCE_EXTENSIONS.map((extension) => join(base, `index${extension}`)) + ] + return ( + candidates.find( + (candidate) => !/[\\/]node_modules[\\/]/.test(candidate) && isFile(candidate) + ) ?? null + ) +} + +/** + * Returns the test files (root-relative, forward slashes) that can share a worker's module cache. + * Only the test file and the test-support files it imports are scanned, not production modules. + */ +export function filesSafeToShareWorker(root, files, aliases = []) { + const sources = new Map() + const read = (file) => { + let source = sources.get(file) + if (source === undefined) { + source = readFileSync(file, 'utf8') + sources.set(file, source) + } + return source + } + const isTestSupport = (file) => /\bfrom\s+['"]vitest['"]/.test(read(file)) + const memo = new Map() + const visiting = new Set() + const requiresIsolation = (file) => { + const known = memo.get(file) + if (known !== undefined) { + return known + } + if (visiting.has(file)) { + return false + } + visiting.add(file) + const source = read(file) + let result = ISOLATION_REQUIRING_API.test(source) + for (const match of result ? [] : source.matchAll(IMPORT_SPECIFIER)) { + const target = resolveLocal(file, match[1], aliases) + if (target && isTestSupport(target) && requiresIsolation(target)) { + result = true + break + } + } + visiting.delete(file) + memo.set(file, result) + return result + } + return files + .map((file) => resolve(root, file)) + .filter((file) => isFile(file) && !requiresIsolation(file)) + .map((file) => relative(root, file).replaceAll('\\', '/')) +} diff --git a/config/vitest.config.ts b/config/vitest.config.ts index 6202566aa32..2c2d9316496 100644 --- a/config/vitest.config.ts +++ b/config/vitest.config.ts @@ -1,4 +1,4 @@ -import { readFileSync } from 'node:fs' +import { globSync, readFileSync } from 'node:fs' import { createRequire } from 'node:module' import { resolve } from 'node:path' import { normalizePath, transformWithOxc, type Plugin } from 'vite' @@ -9,6 +9,7 @@ import { UNIT_SETUP_FILES } from './scripts/ci/unit-selection/dependency-graph/ci-unit-files.mjs' import TimingSequencer from './scripts/ci/sharding/ci-unit-sequencer.mjs' +import { filesSafeToShareWorker } from './scripts/test-runtime/execution-policy/vitest-shared-worker-files.mjs' import RuntimeSequencer from './scripts/test-runtime/execution-policy/vitest-runtime-sequencer.mjs' import { getNodeRuntimeInclude } from './scripts/test-runtime/sqlite-admission/vitest-node-runtime-files.mjs' import { nodeRuntimePool } from './scripts/test-runtime/node-worker/vitest-node-runtime-pool' @@ -121,7 +122,46 @@ const bunProject = { sequence: { groupOrder: 1 } } } -const projects = [...(process.versions.bun ? [bunProject] : []), nodeProject] +// Files whose state the per-file reset can undo share one worker's module cache instead of +// re-importing thousands of modules each. ORCA_VITEST_ISOLATE_EVERY_FILE=1 turns it off. +const runtimeProject = process.versions.bun ? bunProject : nodeProject +const nodeRuntimeFiles = new Set(nodeRuntimeInclude.map((file) => file.replaceAll('\\', '/'))) +const sharedWorkerFiles = + process.env.ORCA_VITEST_ISOLATE_EVERY_FILE === '1' + ? [] + : filesSafeToShareWorker( + process.cwd(), + globSync(UNIT_INCLUDE) + .map((file) => file.replaceAll('\\', '/')) + .filter((file) => !nodeRuntimeFiles.has(file)), + [ + { find: '@renderer', replacement: resolve('src/renderer/src') }, + { find: '@', replacement: resolve('src/renderer/src') } + ] + ) +const sharedProjects = + sharedWorkerFiles.length > 0 + ? [ + { + ...runtimeProject, + test: { + ...runtimeProject.test, + name: `${runtimeProject.test.name}-shared`, + include: sharedWorkerFiles, + isolate: false, + setupFiles: [ + ...runtimeProject.test.setupFiles, + resolve('config/scripts/test-runtime/environment/vitest-shared-worker-file-reset.mjs') + ] + } + } + ] + : [] +runtimeProject.test.exclude = [ + ...(runtimeProject.test.exclude ?? testOptions.exclude), + ...sharedWorkerFiles +] +const projects = [...(process.versions.bun ? [bunProject] : []), nodeProject, ...sharedProjects] export default defineConfig({ ...transforms,