webui: skip Node's -- delimiter when finding the Console script

Node leaves the -- option delimiter out of process.execArgv, so
'node -- /abs/cli.mjs' picked '--' as the script and re-executed an
already absolute launch, running preloads twice. Review P3 on #1873
reqget-2.
This commit was merged in pull request #1876.
This commit is contained in:
jason.woltje committed 2026-11-09 12:57:06 -06:00
1 parent 744753d7eb
commit 7917913606
2 files changed
+20 -2

No files matched your search

+3 -1
View File
@@ -31,7 +31,9 @@ import { selectorScope } from '../../business/src/selector.mjs';
// the existing human and Console proof checks remain unchanged.
if (process.platform === 'linux') {
const raw = readFileSync('/proc/self/cmdline', 'utf8').split('\0');
const script = raw[1 + process.execArgv.length];
let at = 1 + process.execArgv.length;
if (raw[at] === '--') at += 1; // Node leaves its option delimiter out of execArgv.
const script = raw[at];
if (script && !isAbsolute(script)) {
if (typeof process.execve !== 'function') throw new Error('relative Console launch requires process.execve; use an absolute script path');
process.execve(process.execPath, [process.execPath, ...process.execArgv, fileURLToPath(import.meta.url), ...process.argv.slice(2)], process.env);
@@ -1,7 +1,8 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { spawn, spawnSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { fileURLToPath } from 'node:url';
import { join } from 'node:path';
@@ -35,3 +36,18 @@ test('a relative Console launch presents the same-tree absolute parent path to t
// Re-exec must not erase the ancestry marker that makes an agent fail human proof.
assert.ok(readFileSync(`/proc/${child.pid}/environ`, 'utf8').split('\0').includes('MOSAIC_AGENT_RUN=synthetic-test-marker'));
});
test('Node\'s -- delimiter: an absolute script loads once, a relative one re-execs once', { timeout: 30000 }, () => {
const dir = mkdtempSync(join(tmpdir(), 'rl'));
const count = join(dir, 'loads');
const preload = join(dir, 'count.cjs');
writeFileSync(preload, `require('node:fs').appendFileSync(${JSON.stringify(count)}, 'x');\n`);
const loads = args => {
writeFileSync(count, '');
const r = spawnSync(process.execPath, ['--require', preload, '--', ...args, '--help'], { cwd: root, env: process.env, encoding: 'utf8' });
assert.equal(r.status, 0, r.stderr);
return readFileSync(count, 'utf8').length;
};
assert.equal(loads([join(root, script)]), 1);
assert.equal(loads([script]), 2);
});