Skip to content

The agent that grows with you

fix(desktop): pack no longer runs through cross-env (#110121, #103010)

teknium1d968ee260bfb
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index 27ebf66f46a..0072adf0bee 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -31,7 +31,7 @@
"i18n:keys": "node scripts/i18n-keys.mjs",
"postbuild": "node scripts/assert-dist-built.mjs",
"prebuilder": "node scripts/patch-electron-builder-mac-binary.mjs",
- "builder": "cross-env NODE_OPTIONS=--max-old-space-size=16384 node scripts/run-electron-builder.mjs",
+ "builder": "node scripts/run-electron-builder.mjs",
"pack": "npm run build && npm run builder -- --dir --publish never",
"dist": "npm run build && npm run builder",
"payload": "node ../../scripts/build/python.mjs ../../scripts/bundles/stage.py --out build/agent-payload",
diff --git a/apps/desktop/scripts/run-electron-builder.mjs b/apps/desktop/scripts/run-electron-builder.mjs
index 806e1d40bee..2d34d041caf 100644
--- a/apps/desktop/scripts/run-electron-builder.mjs
+++ b/apps/desktop/scripts/run-electron-builder.mjs
@@ -14,6 +14,21 @@ const platformFlags = new Map([['--win', 'win32'], ['-w', 'win32'], ['--windows'
['--mac', 'darwin'], ['--macos', 'darwin'], ['-m', 'darwin'], ['-o', 'darwin'], ['--linux', 'linux'], ['-l', 'linux']])
const architectures = ['--x64', '--arm64', '--ia32', '--armv7l', '--universal']
+// electron-builder's asar/blockmap pass outgrows the default V8 heap. The flag
+// is set here, on the node children this wrapper already spawns, instead of a
+// `cross-env NODE_OPTIONS=…` prefix on the `builder` npm script: that prefix
+// needed the cross-env bin (#110121), and cross-env strips bare `'` from every
+// forwarded argument, so a staging dir under C:\Users\r'y'z reached
+// electron-builder as C:\Users\ryz (#103010).
+export const HEAP_FLAG = '--max-old-space-size=16384'
+
+/** @param {string | undefined} inherited @returns {string} */
+export function builderNodeOptions(inherited = process.env.NODE_OPTIONS) {
+ const parts = (inherited ?? '').split(/\s+/).filter(Boolean)
+ if (!parts.some(part => part.startsWith('--max-old-space-size'))) parts.push(HEAP_FLAG)
+ return parts.join(' ')
+}
+
/** @param {string[]} args @param {string} name @returns {string | undefined} */
function takeOption(args, name) {
const index = args.findIndex(arg => arg === name || arg.startsWith(`${name}=`))
@@ -100,7 +115,8 @@ function runSourceBuilds(args, nativeDeps, spawn) {
'--prepared', path.join(out, 'prepared.json'), '--native-deps', native,
...args.filter(arg => !architectures.includes(arg)), flag])
for (const command of commands) {
- const result = spawn(process.execPath, command, { cwd: app, stdio: 'inherit' })
+ const result = spawn(process.execPath, command, { cwd: app, stdio: 'inherit',
+ env: { ...process.env, NODE_OPTIONS: builderNodeOptions() } })
if (result.error) throw result.error
if (result.status !== 0) return result.status ?? 1
}
@@ -140,7 +156,7 @@ export function runElectronBuilder(args, { spawn = spawnSync } = {}) {
preloads.push('--require', path.join(import.meta.dirname, 'dmgbuild-diagnostics.cjs'))
}
/** @type {NodeJS.ProcessEnv} */
- const env = { ...process.env, HERMES_PREPARED_PACKAGING: manifest,
+ const env = { ...process.env, NODE_OPTIONS: builderNodeOptions(), HERMES_PREPARED_PACKAGING: manifest,
HERMES_PREPARED_NATIVE_DEPS: nativeDeps, HERMES_PREPARED_TARGET: inputs.target }
if (inputs.dmgbuild) env.CUSTOM_DMGBUILD_PATH = inputs.dmgbuild
if (inputs.windows?.dotnetRoot) env.DOTNET_ROOT = inputs.windows.dotnetRoot
diff --git a/apps/desktop/scripts/run-electron-builder.test.mjs b/apps/desktop/scripts/run-electron-builder.test.mjs
index ca96fd82fd9..05711af5d71 100644
--- a/apps/desktop/scripts/run-electron-builder.test.mjs
+++ b/apps/desktop/scripts/run-electron-builder.test.mjs
@@ -2,7 +2,8 @@ import assert from 'node:assert/strict'
import { test } from 'vitest'
import { spawnSync } from 'node:child_process'
import path from 'node:path'
-import { runElectronBuilder } from './run-electron-builder.mjs'
+import { HEAP_FLAG, builderNodeOptions, runElectronBuilder } from './run-electron-builder.mjs'
+import { createRequire } from 'node:module'
import fs from 'node:fs'
import os from 'node:os'
import { publishPackagingInputs } from './prepared-packaging.mjs'
@@ -66,3 +67,23 @@ test('strict builder refuses absent inputs before loading electron-builder', ()
assert.match(result.stderr, /run preparation again/)
assert.doesNotMatch(result.stdout, /electron-builder\s+version/)
})
+
+test('builder script is node itself, so npm-forwarded arguments reach the wrapper untouched', () => {
+ // A cross-env prefix needed its bin (#110121) and stripped bare quotes from
+ // forwarded arguments, so a staging dir under a profile like r'y'z lost its quotes (#103010).
+ const { scripts } = createRequire(import.meta.url)('../package.json')
+ assert.match(scripts.builder, /^node\s/)
+})
+
+test('source builds forward a quoted staging dir verbatim and set the heap flag once', () => {
+ const calls = []
+ const spawn = (_node, args, options) => { calls.push({ args, options }); return { status: 0 } }
+ const output = "-c.directories.output=/Users/r'y'z/apps/desktop/.staging-1"
+ assert.equal(runElectronBuilder(['--dir', output], { spawn }), 0)
+ const strict = calls.find(({ args }) => args[0].endsWith('run-electron-builder.mjs'))
+ assert.ok(strict.args.includes(output))
+ for (const { options } of calls) assert.match(options.env.NODE_OPTIONS, /--max-old-space-size/)
+ assert.equal(builderNodeOptions(''), HEAP_FLAG)
+ assert.equal(builderNodeOptions('--no-warnings'), `--no-warnings ${HEAP_FLAG}`)
+ assert.equal(builderNodeOptions('--max-old-space-size=4096'), '--max-old-space-size=4096')
+})