Skip to content

Commit 3c5bd78

Browse files
committed
fix(test): keep the Vitest fork --no-sparkplug flag out of worker_threads execArgv
vitest.config.ts starts every fork with `--no-sparkplug` (the nodejs/node#62393 workaround). Node validates a worker_threads.Worker's execArgv only when it is passed explicitly and rejects per-process V8 flags with ERR_WORKER_INVALID_EXEC_ARGV — the shape `@lix-js/sdk` (via @inlang/paraglide-js) uses, `new Worker(url, { execArgv: process.execArgv })`. Locally the sdk's native addon loads and no worker is created; on CI the addon fails to load and the worker fallback crashed the Unit Tests shard. - scripts/vitest-fork-exec-argv.mjs: single source of truth `FORK_EXEC_ARGV` plus the pure, idempotent `stripForkExecArgv(execArgv)`. - vitest.config.ts: `execArgv` comes from the shared constant. - src/test-setup.ts: replaces `process.execArgv` with the stripped list in each fork (V8 flags are process-wide, so the fork still runs without Sparkplug) and records what was removed in INTENT_VITEST_STRIPPED_EXEC_ARGV once per fork. - tests/unit/vitest-fork-exec-argv.test.ts: asserts the flag is gone from process.execArgv, was present before stripping, a Worker with an explicit `execArgv: process.execArgv` exits 0, and covers stripForkExecArgv.
1 parent 689dec4 commit 3c5bd78

4 files changed

Lines changed: 125 additions & 2 deletions

File tree

‎scripts/vitest-fork-exec-argv.mjs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Single source of truth for the Node flags vitest.config.ts passes to every
2+
// test fork (`test.execArgv`), and the helper src/test-setup.ts uses to keep
3+
// them out of `process.execArgv` inside the fork.
4+
//
5+
// V8 flags are process-wide, so a fork started with `--no-sparkplug` already
6+
// runs without Sparkplug; `process.execArgv` only matters for what the fork's
7+
// children and worker_threads inherit. Node validates a Worker's `execArgv`
8+
// only when it is passed explicitly, and rejects per-process V8 flags with
9+
// ERR_WORKER_INVALID_EXEC_ARGV — the shape `@lix-js/sdk` (via
10+
// @inlang/paraglide-js) uses: `new Worker(url, { execArgv: process.execArgv })`.
11+
// Stripping the fork-only flags at setup time makes that construction succeed
12+
// without test authors knowing about the Sparkplug workaround.
13+
14+
/**
15+
* Flags vitest passes to each test fork. Node 24's V8 Sparkplug/GC regression
16+
* (nodejs/node#62393) SIGSEGVs long test runs; disabling Sparkplug avoids it
17+
* until the pinned runtime contains the upstream fix.
18+
* @type {readonly string[]}
19+
*/
20+
export const FORK_EXEC_ARGV = Object.freeze(['--no-sparkplug']);
21+
22+
/**
23+
* Splits `execArgv` into the entries to keep and the fork-only flags removed.
24+
* Pure and idempotent: never mutates its input, and running it over `kept`
25+
* again removes nothing.
26+
* @param {readonly string[]} execArgv
27+
* @returns {{ kept: string[]; removed: string[] }}
28+
*/
29+
export function stripForkExecArgv(execArgv) {
30+
/** @type {string[]} */
31+
const kept = [];
32+
/** @type {string[]} */
33+
const removed = [];
34+
for (const arg of execArgv) {
35+
(FORK_EXEC_ARGV.includes(arg) ? removed : kept).push(arg);
36+
}
37+
return { kept, removed };
38+
}

‎src/test-setup.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ import { vi, afterEach } from 'vitest';
77
import * as path from 'path';
88
import { tmpdir } from 'os';
99
import { scrubHostNodeInjection } from './scrub-host-node-injection';
10+
import { stripForkExecArgv } from '../scripts/vitest-fork-exec-argv.mjs';
11+
12+
// vitest.config.ts starts each fork with V8 flags (`--no-sparkplug`) that Node
13+
// rejects with ERR_WORKER_INVALID_EXEC_ARGV when a worker_threads.Worker is
14+
// given them explicitly — the shape `@lix-js/sdk` (via @inlang/paraglide-js)
15+
// uses: `new Worker(url, { execArgv: process.execArgv })`. V8 flags are
16+
// process-wide, so the fork keeps running without Sparkplug; only what
17+
// children and workers inherit changes. The stripped list is recorded once per
18+
// fork so tests/unit/vitest-fork-exec-argv.test.ts can assert the workaround
19+
// still reached the fork (a later test file in the same fork sees nothing left
20+
// to strip and must not overwrite the record).
21+
const { kept: execArgvKept, removed: execArgvRemoved } = stripForkExecArgv(process.execArgv);
22+
process.execArgv = execArgvKept;
23+
if (execArgvRemoved.length > 0) {
24+
process.env.INTENT_VITEST_STRIPPED_EXEC_ARGV = JSON.stringify(execArgvRemoved);
25+
}
1026

1127
// Children spawned by tests must not inherit host-level Node injection: on a
1228
// Datadog-instrumented host, `NODE_OPTIONS=--require dd-trace/init` wrote tracer
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Regression coverage for the fork-only V8 flag leaking into worker_threads:
2+
// vitest.config.ts starts each fork with `--no-sparkplug` (nodejs/node#62393
3+
// workaround) and src/test-setup.ts strips it from `process.execArgv` again,
4+
// because Node validates a Worker's `execArgv` only when passed explicitly and
5+
// rejects per-process V8 flags with ERR_WORKER_INVALID_EXEC_ARGV — the shape
6+
// `@lix-js/sdk` (via @inlang/paraglide-js) uses. Default inheritance never
7+
// fails, so the Worker assertion below MUST pass `execArgv` explicitly.
8+
import { Worker } from 'node:worker_threads';
9+
import { describe, expect, it } from 'vitest';
10+
import { FORK_EXEC_ARGV, stripForkExecArgv } from '../../scripts/vitest-fork-exec-argv.mjs';
11+
12+
function runWorker(execArgv: string[]): Promise<number> {
13+
return new Promise((resolve, reject) => {
14+
const worker = new Worker('process.exit(0)', { eval: true, execArgv });
15+
worker.once('error', reject);
16+
worker.once('exit', resolve);
17+
});
18+
}
19+
20+
describe('vitest fork execArgv', () => {
21+
it('is stripped from process.execArgv inside the fork', () => {
22+
for (const flag of FORK_EXEC_ARGV) {
23+
expect(process.execArgv).not.toContain(flag);
24+
}
25+
});
26+
27+
it('was present before the setup file stripped it (the Sparkplug workaround still applies)', () => {
28+
const recorded = process.env.INTENT_VITEST_STRIPPED_EXEC_ARGV;
29+
expect(recorded, 'src/test-setup.ts did not record a stripped execArgv').toBeDefined();
30+
expect(JSON.parse(recorded as string)).toEqual(expect.arrayContaining(['--no-sparkplug']));
31+
});
32+
33+
it('lets a Worker start with process.execArgv passed explicitly', async () => {
34+
await expect(runWorker(process.execArgv)).resolves.toBe(0);
35+
});
36+
37+
it('is rejected by Node when handed to a Worker explicitly', async () => {
38+
await expect(runWorker([...process.execArgv, ...FORK_EXEC_ARGV])).rejects.toMatchObject({
39+
code: 'ERR_WORKER_INVALID_EXEC_ARGV',
40+
});
41+
});
42+
});
43+
44+
describe('stripForkExecArgv', () => {
45+
it('removes the fork-only flags and keeps everything else in order', () => {
46+
expect(stripForkExecArgv(['--max-old-space-size=4096', '--no-sparkplug', '--inspect'])).toEqual(
47+
{ kept: ['--max-old-space-size=4096', '--inspect'], removed: ['--no-sparkplug'] },
48+
);
49+
});
50+
51+
it('does not mutate its input', () => {
52+
const input = ['--no-sparkplug', '--inspect'];
53+
stripForkExecArgv(input);
54+
expect(input).toEqual(['--no-sparkplug', '--inspect']);
55+
});
56+
57+
it('is idempotent', () => {
58+
const first = stripForkExecArgv(['--no-sparkplug', '--inspect']);
59+
expect(stripForkExecArgv(first.kept)).toEqual({ kept: ['--inspect'], removed: [] });
60+
});
61+
62+
it('returns empty lists for an empty execArgv', () => {
63+
expect(stripForkExecArgv([])).toEqual({ kept: [], removed: [] });
64+
});
65+
});

‎vitest.config.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import os from 'os';
44
import { readFileSync } from 'fs';
55
import { gitignoreDirExcludes } from './scripts/gitignore-dir-excludes.mjs';
66
import { PARAGLIDE_STALE_MESSAGE, ensureRepoParaglide } from './scripts/paraglide-inputs-hash.mjs';
7+
import { FORK_EXEC_ARGV } from './scripts/vitest-fork-exec-argv.mjs';
78

89
// CI-only tuning for shared self-hosted runners (intent-hq/monorepo#3082; the
910
// recurrence class #3032/#2586/#1406/#1171/#545). The CI unit job runs on the
@@ -56,9 +57,12 @@ export default defineConfig(async () => {
5657
// Node 24's V8 Sparkplug/GC regression (nodejs/node#62393) SIGSEGVs long
5758
// test runs: forks surface it as dropped files, while threads crash the
5859
// controller directly. Keep process isolation and disable only Sparkplug
59-
// in workers until the pinned runtime contains the upstream fix.
60+
// in workers until the pinned runtime contains the upstream fix. The
61+
// setup file strips these flags from process.execArgv again inside each
62+
// fork so worker_threads constructed with an explicit execArgv (e.g. by
63+
// @lix-js/sdk) are not rejected with ERR_WORKER_INVALID_EXEC_ARGV.
6064
pool: 'forks',
61-
execArgv: ['--no-sparkplug'],
65+
execArgv: [...FORK_EXEC_ARGV],
6266
// Redirects every worker's os.tmpdir() into a private root and fails the
6367
// run if a test leaves a temp entry behind (see src/test-global-setup.ts).
6468
globalSetup: ['./src/test-global-setup.ts'],

0 commit comments

Comments
 (0)