Skip to content

The agent that grows with you

fix(desktop): keep inherited NODE_OPTIONS intact when adding the builder heap

kshitijk4poorb9df1cccaec2
diff --git a/apps/desktop/scripts/run-electron-builder.mjs b/apps/desktop/scripts/run-electron-builder.mjs
index 2d34d041caf..ca780b8b181 100644
--- a/apps/desktop/scripts/run-electron-builder.mjs
+++ b/apps/desktop/scripts/run-electron-builder.mjs
@@ -14,19 +14,18 @@ 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'
+// electron-builder's asar/blockmap pass outgrows the default V8 heap. Set here,
+// not via a cross-env prefix on the `builder` script: cross-env strips `'` from
+// forwarded arguments (#103010) and needs its bin installed (#110121).
+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(' ')
+/**
+ * Inherited NODE_OPTIONS stay byte-identical (quoted preload paths survive); the
+ * heap flag goes last so it wins, as cross-env's replacement did.
+ * @param {string} [inherited] @returns {string}
+ */
+export function builderNodeOptions(inherited = process.env.NODE_OPTIONS ?? '') {
+ return `${inherited} ${HEAP_FLAG}`.trim()
}
/** @param {string[]} args @param {string} name @returns {string | undefined} */
@@ -98,6 +97,7 @@ function runSourceBuilds(args, nativeDeps, spawn) {
const requested = [...new Set(args.filter(arg => architectures.includes(arg)))]
if (requested.includes('--universal')) throw new Error('No prepared universal native payload; use --x64 --arm64 for separate packages')
if (nativeDeps && requested.length > 1) throw new Error('--native-deps selects one architecture, not multiple source targets')
+ const env = { ...process.env, NODE_OPTIONS: builderNodeOptions() }
for (const flag of requested.length ? requested : [`--${process.arch}`]) {
const arch = flag.slice(2)
const target = `${platform}-${arch}`
@@ -115,8 +115,7 @@ 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',
- env: { ...process.env, NODE_OPTIONS: builderNodeOptions() } })
+ const result = spawn(process.execPath, command, { cwd: app, stdio: 'inherit', env })
if (result.error) throw result.error
if (result.status !== 0) return result.status ?? 1
}
diff --git a/apps/desktop/scripts/run-electron-builder.test.mjs b/apps/desktop/scripts/run-electron-builder.test.mjs
index 05711af5d71..c20dbc8e342 100644
--- a/apps/desktop/scripts/run-electron-builder.test.mjs
+++ b/apps/desktop/scripts/run-electron-builder.test.mjs
@@ -2,8 +2,7 @@ import assert from 'node:assert/strict'
import { test } from 'vitest'
import { spawnSync } from 'node:child_process'
import path from 'node:path'
-import { HEAP_FLAG, builderNodeOptions, runElectronBuilder } from './run-electron-builder.mjs'
-import { createRequire } from 'node:module'
+import { builderNodeOptions, runElectronBuilder } from './run-electron-builder.mjs'
import fs from 'node:fs'
import os from 'node:os'
import { publishPackagingInputs } from './prepared-packaging.mjs'
@@ -68,22 +67,32 @@ test('strict builder refuses absent inputs before loading electron-builder', ()
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('npm run builder forwards an apostrophe path to the wrapper verbatim', () => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'builder-'))
+ const manifest = path.join(root, "r'y'z", 'missing.json')
+ const windows = process.platform === 'win32'
+ try {
+ // Windows needs a shell for npm.cmd, which joins argv unquoted.
+ const result = spawnSync('npm', ['run', 'builder', '--silent', '--ignore-scripts', '--',
+ '--prepared', windows ? `"${manifest}"` : manifest, '--native-deps', 'missing', '--dir'],
+ { cwd: path.join(import.meta.dirname, '..'), encoding: 'utf8', shell: windows })
+ assert.notEqual(result.status, 0)
+ assert.ok(result.stderr.includes(manifest), result.stderr)
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true })
+ }
})
-test('source builds forward a quoted staging dir verbatim and set the heap flag once', () => {
+test('source builds hand every child the builder heap without rewriting inherited NODE_OPTIONS', () => {
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')
+ assert.equal(runElectronBuilder(['--dir'], { spawn }), 0)
+ assert.ok(calls.length >= 2)
+ const heap = /--max-old-space-size=16384$/
+ for (const { options } of calls) assert.match(options.env.NODE_OPTIONS, heap)
+ assert.match(builderNodeOptions(''), heap)
+ assert.match(builderNodeOptions('--max-old-space-size=4096'), heap)
+ const quoted = '--require "/tmp/sp ace/p.cjs"'
+ assert.ok(builderNodeOptions(quoted).startsWith(quoted))
+ assert.match(builderNodeOptions('--max-old-space-size=16384 --max-old-space-size=4096'), heap)
})