Skip to content

The agent that grows with you

fix(desktop): syntax-check every renderer chunk in one process

Chip94c15c7b7073
diff --git a/apps/desktop/scripts/assert-dist-built.mjs b/apps/desktop/scripts/assert-dist-built.mjs
index 63d838d0d86..925263b333e 100644
--- a/apps/desktop/scripts/assert-dist-built.mjs
+++ b/apps/desktop/scripts/assert-dist-built.mjs
@@ -26,7 +26,10 @@ const ROUTER_CONTEXT_ERROR = "may be used only in the context of a"
// invariant as the react-router check above, same failure class.
const QUERY_CLIENT_CONTEXT_ERROR = "No QueryClient set, use QueryClientProvider to set one"
-// Pure check — returns { ok: true } or { ok: false, error: "..." }.
+// Pure check — returns { ok: true } or { ok: false, error: "...", kind?: "bundle" | "harness" }.
+// `kind: "harness"` marks a failure of the CHECK itself (node would not start, the checker
+// produced no verdict) rather than a defective bundle, so main() can point at the right cause
+// instead of telling the reader to re-run a build that was never the problem.
// Kept side-effect-free so it can be unit tested without spawning a process.
export function checkDistBuilt(distDir) {
if (!existsSync(distDir) || !statSync(distDir).isDirectory()) {
@@ -97,34 +100,67 @@ export function checkDistBuilt(distDir) {
// `{$:n,}` vs `{categories:n,}`, leaving an invalid destructuring pattern).
// Parse each emitted chunk as an ES module before packaging so a corrupted
// build fails loudly and the update retry rebuilds instead of shipping it.
+//
+// The parsing itself runs in ONE child process (`check-chunks-parse.mjs`,
+// vm.SourceTextModule), not one spawn per chunk: with ~1000 chunks, a single
+// stalled process creation on Windows (AV, memory pressure) used to abort the
+// whole update — the guard must not be the least reliable step in the build.
function verifyChunksParse(assetsDir) {
+ const chunks = readdirSync(assetsDir).filter(name => name.endsWith(".js"))
+ if (chunks.length === 0) {
+ return { ok: true }
+ }
+
const nodeBin = process.env.NODE ||
(process.execPath && process.execPath.endsWith("node") ? process.execPath : "node")
- const chunks = readdirSync(assetsDir).filter(name => name.endsWith(".js"))
- for (const name of chunks) {
- const file = join(assetsDir, name)
- const probe = spawnSync(nodeBin, ["--input-type=module", "--check"], {
- input: readFileSync(file),
- maxBuffer: 64 * 1024 * 1024,
- timeout: 60_000,
- })
- if (probe.error) {
- return {
- ok: false,
- error: `could not run node to syntax-check ${name}: ${probe.error.message}`,
- }
+ const checker = join(import.meta.dirname, "check-chunks-parse.mjs")
+ const probe = spawnSync(nodeBin, ["--experimental-vm-modules", checker, assetsDir], {
+ encoding: "utf8",
+ maxBuffer: 64 * 1024 * 1024,
+ timeout: 300_000,
+ })
+ if (probe.error) {
+ return {
+ ok: false,
+ kind: "harness",
+ error: `could not run node to syntax-check the renderer chunks: ${probe.error.message}`,
+ }
+ }
+
+ let verdict = null
+ try {
+ verdict = JSON.parse(String(probe.stdout || "").trim() || "null")
+ } catch {
+ verdict = null
+ }
+ if (!verdict) {
+ const detail = String(probe.stderr || "").trim().split("\n").filter(Boolean).slice(-4).join(" / ")
+ return {
+ ok: false,
+ kind: "harness",
+ error: `node could not syntax-check the renderer chunks (exit ${probe.status}) — ${detail}`,
}
- if (probe.status !== 0) {
- const detail = String(probe.stderr || "").trim().split("\n").slice(0, 4).join(" / ")
- return {
- ok: false,
- error: `built chunk is not valid ES module syntax: ${name} — ${detail}. ` +
- `A renderer chunk failed to parse, so packaging would ship an app that ` +
- `white-screens with "Uncaught SyntaxError" on launch. Re-run the build.`,
- }
+ }
+ if (verdict.ok) {
+ return { ok: true }
+ }
+ if (verdict.kind === "harness" || !verdict.name) {
+ // The check itself did not run (blocked/stalled node, no verdict, or the checker reporting
+ // that it could not read a chunk or use the vm module). Never call that a bundle defect.
+ const where = verdict.name ? ` (on ${verdict.name})` : ""
+ return {
+ ok: false,
+ kind: "harness",
+ error: `the renderer chunk check could not run${where}: ${verdict.detail}`,
}
}
- return { ok: true }
+ return {
+ ok: false,
+ kind: "bundle",
+ error: `built chunk is not valid ES module syntax: ${verdict.name} — ${verdict.detail}. ` +
+ `A renderer chunk failed to parse, so packaging would ship an app that ` +
+ `white-screens with "Uncaught SyntaxError" on launch. Re-run the build.`,
+ }
}
function main() {
@@ -134,6 +170,16 @@ function main() {
if (!result.ok) {
console.error(`\n✗ assert-dist-built: ${result.error}`)
+ if (result.kind === "harness") {
+ // The check could not run — the bundle is not implicated. Saying "re-run the build" here
+ // sent a past update investigation after a bundle defect that did not exist.
+ console.error(" This is a failure of the CHECK, not of the bundle: node did not run to")
+ console.error(" completion. On Windows that is usually a stalled process creation")
+ console.error(" (antivirus/EDR interference, memory pressure) and clears on a retry.")
+ console.error(" If it repeats, check the node binary and any AV exclusion for the")
+ console.error(` build tree, then re-run: cd ${desktopRoot} && npm run build\n`)
+ process.exit(1)
+ }
console.error(" The renderer bundle is missing or incomplete, so packaging")
console.error(" would produce an app that launches to a blank page.")
console.error(" Re-run the build and check the tsc/vite output above for the")
diff --git a/apps/desktop/scripts/check-chunks-parse.mjs b/apps/desktop/scripts/check-chunks-parse.mjs
new file mode 100644
index 00000000000..e26ff2c6c26
--- /dev/null
+++ b/apps/desktop/scripts/check-chunks-parse.mjs
@@ -0,0 +1,74 @@
+// Parse every emitted renderer chunk as an ES module in ONE node process.
+//
+// assert-dist-built.mjs used to spawn `node --input-type=module --check` once per
+// chunk. With ~1000 emitted chunks the update's desktop build therefore depended on
+// ~1000 successful process creations, and on Windows a single spawn can stall for
+// minutes under AV scanning or memory pressure. One such stall aborted the whole
+// update (2026-09-27: `could not run node to syntax-check nord-*.js: spawnSync
+// ...node.exe ETIMEDOUT`, which failed the update with exit 1 even though vite had
+// built the bundle cleanly). Measured on the failing tree: 960 chunks, ~80 s of
+// sequential spawns, worst single spawn 0.45 s on an idle box.
+//
+// `vm.SourceTextModule` is the in-process ES module parser, so the same check now
+// costs one spawn. It reports the same early errors as `node --check` (invalid
+// destructuring patterns such as the `{,:n}` token-drop this guard was written for,
+// duplicate declarations) and throws SyntaxError for them. The caller passes
+// --experimental-vm-modules, which the vm module API requires.
+//
+// Usage: node --experimental-vm-modules check-chunks-parse.mjs <assetsDir>
+// stdout: {"ok":true,"checked":N}
+// | {"ok":false,"kind":"bundle","name":...,"detail":...} the chunk does not parse
+// | {"ok":false,"kind":"harness","name"?:...,"detail":...} this CHECK could not run
+// `kind` is what lets the guard report the truth: only a SyntaxError means the bundle is
+// defective. A missing vm API (a node build ignoring --experimental-vm-modules) or an
+// unreadable chunk is a failure of the CHECK, and must not be reported as invalid bundle
+// syntax — that misattribution is exactly what the guard's `harness` branch exists to stop.
+import { readFileSync, readdirSync } from "fs"
+import { join } from "path"
+import vm from "vm"
+
+function report(payload) {
+ process.stdout.write(JSON.stringify(payload))
+}
+
+// The check could not run (no directory, unreadable chunk, vm API unavailable): say so
+// without claiming anything about the bundle's syntax.
+function harnessFailure(detail, name) {
+ report(name ? { ok: false, kind: "harness", name, detail } : { ok: false, kind: "harness", detail })
+ process.exit(2)
+}
+
+const assetsDir = process.argv[2]
+if (!assetsDir) {
+ harnessFailure("no assets directory given")
+}
+
+try {
+ const chunks = readdirSync(assetsDir).filter(name => name.endsWith(".js"))
+ for (const name of chunks) {
+ let source
+ try {
+ source = readFileSync(join(assetsDir, name), "utf8")
+ } catch (err) {
+ harnessFailure(`${err.name || "Error"}: ${String(err.message || err)}`, name)
+ }
+ try {
+ new vm.SourceTextModule(source, { identifier: name })
+ } catch (err) {
+ const detail = `${err.name || "Error"}: ${String(err.message || err)}`
+ .split("\n")
+ .slice(0, 4)
+ .join(" / ")
+ if (err instanceof SyntaxError) {
+ report({ ok: false, kind: "bundle", name, detail })
+ process.exit(1)
+ }
+ // e.g. `TypeError: vm.SourceTextModule is not a constructor` when this node build does
+ // not honour --experimental-vm-modules: the check did not run, the bundle is not at fault.
+ harnessFailure(detail, name)
+ }
+ }
+ report({ ok: true, checked: chunks.length })
+} catch (err) {
+ harnessFailure(`${err.name || "Error"}: ${err.message || err}`)
+}