From 8d52dcabe2196f1195203cd2fe9ff7b73e61c9c8 Mon Sep 17 00:00:00 2001 From: Diego Rodrigues de Sa e Souza Date: Fri, 11 Sep 2026 22:04:44 -0300 Subject: [PATCH] fix(cli): run Node runtime guard before heavy import chain (#12296) (#13253) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Merged as part of the 39-PR owner batch of 2026-09-11, validated as a unit. Boarded into one consolidated worktree cut from `release/v3.8.51` with the other 38 — zero conflicts between them. - ESLint over every changed file: no errors (the only finding was one suppression entry the batch emptied, pruned on #13243) - `typecheck:core` clean; `check:dashboard-typecheck` OK (206 pre-existing, within baseline); `check:changelog-integrity` OK - complexity 2821 / baseline 3218 and cognitive-complexity 1272 / baseline 1437 — both under baseline - 256 assertions green: 246 under node:test and 10 under vitest, which is where `tests/unit/**/*.test.tsx` actually runs - `check-file-size`: `chatCore.ts` rebaselined 6144 → 6146 for #13278 and #13276, annotated and landed on #13243 ⚠️ base-red inherited: #12732 — the provider count (356 in the docs vs the 358 the modules define) and `open-sse/utils/stream.ts` at 3115 > frozen 3098 both reproduce on the pure tip with zero contribution from this batch. --- bin/omniroute.mjs | 28 +++++++++ .../fixes/12296-node-runtime-guard-early.md | 1 + .../issue-12296-node-runtime-guard.test.ts | 63 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 changelog.d/fixes/12296-node-runtime-guard-early.md create mode 100644 tests/unit/issue-12296-node-runtime-guard.test.ts diff --git a/bin/omniroute.mjs b/bin/omniroute.mjs index de51120643..751f18f583 100755 --- a/bin/omniroute.mjs +++ b/bin/omniroute.mjs @@ -50,6 +50,34 @@ if (isVersionFastPath(process.argv)) { process.exit(0); } +// Detect an unsupported Node.js runtime BEFORE the heavy `tsx/esm` import and +// Commander's ~70-command registration chain run. That chain pulls in `ora` -> +// the hoisted `string-width` package, whose module contains top-level ES2024 +// Unicode-set (`v` flag) regex literals. On a Node/V8 build that predates +// `v`-flag support, those literals fail to even *parse*, throwing a bare +// `SyntaxError: Invalid regular expression flags` deep inside a transitive +// dependency instead of an actionable message (#12296). Skip this for the +// same read-only invocations `shouldProvisionStorageKey` already exempts +// (`--help`/`-h`, `help`/`completion`) — those still need the full command +// registry to render their output, so an incompatible runtime crashing there +// is a separate, pre-existing limitation this fix does not attempt to solve. +if (shouldProvisionStorageKey(process.argv)) { + const nodeSupport = getNodeRuntimeSupport(); + if (!nodeSupport.nodeCompatible) { + const runtimeWarning = getNodeRuntimeWarning() || "Unsupported Node.js runtime detected."; + console.error( + `\x1b[31m✖ Node.js ${nodeSupport.nodeVersion} is not supported.\x1b[0m\n` + + ` ${runtimeWarning}\n` + + ` Supported runtimes: ${nodeSupport.supportedDisplay}\n` + + ` Recommended: Node.js ${nodeSupport.recommendedVersion}\n` + + ` If you installed OmniRoute globally, run \`node -v\` and confirm \`omniroute\` is not resolving to\n` + + ` a stale/distro-packaged \`nodejs\` binary (e.g. /usr/bin/node) instead of the version you expect —\n` + + ` that mismatch is the most common cause even when package.json's engines range is correct.` + ); + process.exit(1); + } +} + // MCP stdio transport uses stdout exclusively for JSON-RPC messages. Redirect // console.log/warn to stderr before anything else runs — including the tsx/esm and // polyfill imports below, since those (and their transitive module graphs, e.g. DB diff --git a/changelog.d/fixes/12296-node-runtime-guard-early.md b/changelog.d/fixes/12296-node-runtime-guard-early.md new file mode 100644 index 0000000000..1c2439f287 --- /dev/null +++ b/changelog.d/fixes/12296-node-runtime-guard-early.md @@ -0,0 +1 @@ +- fix(cli): run the Node.js runtime compatibility guard before the heavy `tsx/esm` + Commander import chain so an unsupported runtime gets a clear message instead of a raw `Invalid regular expression flags` crash (#12296) diff --git a/tests/unit/issue-12296-node-runtime-guard.test.ts b/tests/unit/issue-12296-node-runtime-guard.test.ts new file mode 100644 index 0000000000..d7dfb4a766 --- /dev/null +++ b/tests/unit/issue-12296-node-runtime-guard.test.ts @@ -0,0 +1,63 @@ +// Regression test for issue #12296: "Invalid regular expression flags" crash +// right after STORAGE_ENCRYPTION_KEY generation on first run. +// +// Root cause: bin/omniroute.mjs imports getNodeRuntimeSupport/getNodeRuntimeWarning +// from ./nodeRuntimeSupport.mjs (intended to detect an unsupported Node.js runtime +// and print a friendly warning) but never actually CALLED either function before +// doing the heavy `await import("tsx/esm")` + Commander command-registration import +// chain. That chain pulls in `ora` -> `string-width@8.x`, whose index.js contains +// top-level ES2024 Unicode-set (`v` flag) regex literals +// (e.g. `/^\p{RGI_Emoji}$/v`) that fail to even PARSE on a V8/Node build that +// predates `v`-flag support - throwing exactly +// `SyntaxError: Invalid regular expression flags` (no flag value in the message, +// matching the report) deep inside a transitive dependency's module graph, instead +// of the intended actionable "Node.js vX is not supported" message. +// +// The only two call sites of getNodeRuntimeSupport/getNodeRuntimeWarning in bin/ +// used to be inside `serve.mjs` and `doctor.mjs` - both unreachable if the import +// chain itself crashed first. This test asserts the guard actually runs (is +// called) in bin/omniroute.mjs, and that it runs BEFORE the heavy import chain +// that pulls in string-width. + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { fileURLToPath } from "node:url"; +import { join, dirname } from "node:path"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); +const OMNIROUTE_MJS = join(__dirname, "..", "..", "bin", "omniroute.mjs"); + +test("bin/omniroute.mjs invokes the Node runtime compatibility guard before the heavy tsx/esm + command-registration import chain", () => { + const src = readFileSync(OMNIROUTE_MJS, "utf8"); + + const heavyImportIdx = src.indexOf('await import("tsx/esm")'); + assert.ok( + heavyImportIdx > -1, + "expected bin/omniroute.mjs to still contain the tsx/esm dynamic import this test anchors on" + ); + + const callPattern = /getNodeRuntime(?:Support|Warning)\s*\(/g; + let firstCallIdx = -1; + for (const match of src.matchAll(callPattern)) { + firstCallIdx = match.index ?? -1; + break; + } + + assert.notEqual( + firstCallIdx, + -1, + "getNodeRuntimeSupport()/getNodeRuntimeWarning() is imported in bin/omniroute.mjs but never called there - " + + "an unsupported/too-old Node.js runtime gets no early friendly warning and instead crashes with a raw " + + "native SyntaxError (e.g. 'Invalid regular expression flags' from string-width@8's v-flag regex literals) " + + "deep inside the tsx/esm + Commander import chain. See issue #12296." + ); + + assert.ok( + firstCallIdx < heavyImportIdx, + "the Node runtime compatibility guard must run BEFORE `await import(\"tsx/esm\")` and the rest of the heavy " + + "import chain (Commander command registry, ora/boxen/update-notifier, etc.) so an unsupported Node.js " + + "version is reported with a clear message and a clean exit instead of crashing on a native parse error " + + "raised while loading a transitive dependency." + ); +});