Skip to content

Commit dac128e

Browse files
committed
fix(build-infra): replace exec wrappers with direct spawn calls
Remove exec() wrapper functions from all builder classes and replace with direct spawn() usage from @socketsecurity/lib/spawn. Changes: - emscripten-builder.mjs: Use shell:true for complex emcc/emcmake/emmake commands, shell:WIN32 for simple wasm-opt/wasm-strip calls - rust-builder.mjs: Replace all exec() calls with spawn(), use shell:true for cargo/wasm-bindgen, shell:WIN32 for version checks - cmake-builder.mjs: Replace all exec() calls with spawn() using proper command+args arrays All spawn() calls now include proper error handling with exit code checks.
1 parent ac7561f commit dac128e

File tree

3 files changed

+89
-68
lines changed

3 files changed

+89
-68
lines changed

packages/build-infra/lib/cmake-builder.mjs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,6 @@ import { spawn } from '@socketsecurity/lib/spawn'
1111

1212
import { printStep } from './build-output.mjs'
1313

14-
/**
15-
* Execute command using spawn with shell.
16-
*/
17-
async function exec(command, options = {}) {
18-
const result = await spawn(command, [], {
19-
stdio: 'inherit',
20-
shell: WIN32,
21-
...options,
22-
})
23-
if (result.code !== 0) {
24-
throw new Error(`Command failed with exit code ${result.code}: ${command}`)
25-
}
26-
}
27-
2814
export class CMakeBuilder {
2915
constructor(sourceDir, buildDir) {
3016
this.sourceDir = sourceDir
@@ -44,11 +30,14 @@ export class CMakeBuilder {
4430
.map(([key, value]) => [`-D${key}=${value}`])
4531
.flat()
4632

47-
await spawn(
33+
const result = await spawn(
4834
'cmake',
4935
['-S', this.sourceDir, '-B', this.buildDir, ...cmakeArgs],
5036
{ shell: WIN32, stdio: 'inherit' }
5137
)
38+
if (result.code !== 0) {
39+
throw new Error(`cmake configure failed with exit code ${result.code}`)
40+
}
5241
}
5342

5443
/**
@@ -63,11 +52,14 @@ export class CMakeBuilder {
6352
printStep('Building with CMake')
6453

6554
const jobs = parallel ? cpus().length : 1
66-
await spawn(
55+
const result = await spawn(
6756
'cmake',
6857
['--build', this.buildDir, '--target', target, '-j', String(jobs)],
6958
{ shell: WIN32, stdio: 'inherit' }
7059
)
60+
if (result.code !== 0) {
61+
throw new Error(`cmake build failed with exit code ${result.code}`)
62+
}
7163
}
7264

7365
/**
@@ -77,10 +69,13 @@ export class CMakeBuilder {
7769
*/
7870
async clean() {
7971
printStep('Cleaning CMake build')
80-
await spawn(
72+
const result = await spawn(
8173
'cmake',
8274
['--build', this.buildDir, '--target', 'clean'],
8375
{ shell: WIN32, stdio: 'inherit' }
8476
)
77+
if (result.code !== 0) {
78+
throw new Error(`cmake clean failed with exit code ${result.code}`)
79+
}
8580
}
8681
}

packages/build-infra/lib/emscripten-builder.mjs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@ import { spawn } from '@socketsecurity/lib/spawn'
1212

1313
import { printStep } from './build-output.mjs'
1414

15-
/**
16-
* Execute command using spawn with shell.
17-
*/
18-
async function exec(command, options = {}) {
19-
const result = await spawn(command, [], {
20-
stdio: 'inherit',
21-
shell: WIN32,
22-
...options,
23-
})
24-
if (result.code !== 0) {
25-
throw new Error(`Command failed with exit code ${result.code}: ${command}`)
26-
}
27-
}
28-
2915
/**
3016
* Aggressive WASM optimization flags.
3117
*/
@@ -90,7 +76,14 @@ export class EmscriptenBuilder {
9076
emccCommand += ` ${WASM_OPT_FLAGS}`
9177
}
9278

93-
await exec(emccCommand, { cwd: this.sourceDir, stdio: 'inherit' })
79+
const result = await spawn(emccCommand, [], {
80+
cwd: this.sourceDir,
81+
shell: true,
82+
stdio: 'inherit',
83+
})
84+
if (result.code !== 0) {
85+
throw new Error(`emcc build failed with exit code ${result.code}`)
86+
}
9487
}
9588

9689
/**
@@ -107,10 +100,14 @@ export class EmscriptenBuilder {
107100

108101
const wasmPath = path.join(this.buildDir, wasmFile)
109102

110-
await exec(
111-
`wasm-opt -O${optimizeLevel} -s ${shrinkLevel} ${wasmPath} -o ${wasmPath}`,
112-
{ stdio: 'inherit' }
103+
const result = await spawn(
104+
'wasm-opt',
105+
[`-O${optimizeLevel}`, '-s', shrinkLevel.toString(), wasmPath, '-o', wasmPath],
106+
{ shell: WIN32, stdio: 'inherit' }
113107
)
108+
if (result.code !== 0) {
109+
throw new Error(`wasm-opt failed with exit code ${result.code}`)
110+
}
114111
}
115112

116113
/**
@@ -124,7 +121,13 @@ export class EmscriptenBuilder {
124121

125122
const wasmPath = path.join(this.buildDir, wasmFile)
126123

127-
await exec(`wasm-strip ${wasmPath}`, { stdio: 'inherit' })
124+
const result = await spawn('wasm-strip', [wasmPath], {
125+
shell: WIN32,
126+
stdio: 'inherit',
127+
})
128+
if (result.code !== 0) {
129+
throw new Error(`wasm-strip failed with exit code ${result.code}`)
130+
}
128131
}
129132

130133
/**
@@ -155,10 +158,14 @@ export class EmscriptenBuilder {
155158
.map(([key, value]) => `-D${key}=${value}`)
156159
.join(' ')
157160

158-
await exec(
161+
const result = await spawn(
159162
`emcmake cmake -S ${this.sourceDir} -B ${this.buildDir} ${cmakeArgs}`,
160-
{ stdio: 'inherit' }
163+
[],
164+
{ shell: true, stdio: 'inherit' }
161165
)
166+
if (result.code !== 0) {
167+
throw new Error(`emcmake configure failed with exit code ${result.code}`)
168+
}
162169
}
163170

164171
/**
@@ -173,9 +180,13 @@ export class EmscriptenBuilder {
173180
printStep('Building with emmake')
174181

175182
const jobs = parallel ? cpus().length : 1
176-
await exec(
183+
const result = await spawn(
177184
`emmake cmake --build ${this.buildDir} --target ${target} -j ${jobs}`,
178-
{ stdio: 'inherit' }
185+
[],
186+
{ shell: true, stdio: 'inherit' }
179187
)
188+
if (result.code !== 0) {
189+
throw new Error(`emmake build failed with exit code ${result.code}`)
190+
}
180191
}
181192
}

packages/build-infra/lib/rust-builder.mjs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@ import { spawn } from '@socketsecurity/lib/spawn'
1212

1313
import { printStep } from './build-output.mjs'
1414

15-
/**
16-
* Execute command using spawn with shell.
17-
*/
18-
async function exec(command, options = {}) {
19-
const result = await spawn(command, [], {
20-
stdio: 'inherit',
21-
shell: WIN32,
22-
...options,
23-
})
24-
if (result.code !== 0) {
25-
throw new Error(`Command failed with exit code ${result.code}: ${command}`)
26-
}
27-
}
28-
2915
/**
3016
* Modern WASM feature flags for RUSTFLAGS.
3117
* Enables SIMD, bulk-memory, and other modern WASM instructions.
@@ -80,7 +66,14 @@ export class RustBuilder {
8066
*/
8167
async installWasmTarget() {
8268
printStep('Installing wasm32-unknown-unknown target')
83-
await exec('rustup target add wasm32-unknown-unknown', { stdio: 'inherit' })
69+
const result = await spawn(
70+
'rustup',
71+
['target', 'add', 'wasm32-unknown-unknown'],
72+
{ shell: WIN32, stdio: 'inherit' }
73+
)
74+
if (result.code !== 0) {
75+
throw new Error(`rustup target add failed with exit code ${result.code}`)
76+
}
8477
}
8578

8679
/**
@@ -110,10 +103,14 @@ export class RustBuilder {
110103
RUSTFLAGS: rustflags,
111104
}
112105

113-
await exec(
106+
const result = await spawn(
114107
`cargo build --target wasm32-unknown-unknown ${profileFlag} ${featuresFlag} -j ${jobs}`,
115-
{ cwd: this.projectDir, env, stdio: 'inherit' }
108+
[],
109+
{ cwd: this.projectDir, env, shell: true, stdio: 'inherit' }
116110
)
111+
if (result.code !== 0) {
112+
throw new Error(`cargo build failed with exit code ${result.code}`)
113+
}
117114
}
118115

119116
/**
@@ -140,10 +137,14 @@ export class RustBuilder {
140137
const tsFlag = typescript ? '--typescript' : '--no-typescript'
141138
const outputPath = path.join(this.projectDir, outDir)
142139

143-
await exec(
140+
const result = await spawn(
144141
`wasm-bindgen --target ${target} ${tsFlag} ${debugFlag} --out-dir ${outputPath} ${input}`,
145-
{ cwd: this.projectDir, stdio: 'inherit' }
142+
[],
143+
{ cwd: this.projectDir, shell: true, stdio: 'inherit' }
146144
)
145+
if (result.code !== 0) {
146+
throw new Error(`wasm-bindgen failed with exit code ${result.code}`)
147+
}
147148
}
148149

149150
/**
@@ -161,9 +162,14 @@ export class RustBuilder {
161162
const inputPath = path.join(this.projectDir, wasmFile)
162163
const outputPath = output ? path.join(this.projectDir, output) : inputPath
163164

164-
await exec(`wasm-opt ${flags} "${inputPath}" -o "${outputPath}"`, {
165-
stdio: 'inherit',
166-
})
165+
const result = await spawn(
166+
`wasm-opt ${flags} "${inputPath}" -o "${outputPath}"`,
167+
[],
168+
{ shell: true, stdio: 'inherit' }
169+
)
170+
if (result.code !== 0) {
171+
throw new Error(`wasm-opt failed with exit code ${result.code}`)
172+
}
167173
}
168174

169175
/**
@@ -173,8 +179,11 @@ export class RustBuilder {
173179
*/
174180
async checkRustInstalled() {
175181
try {
176-
await exec('rustc --version', { stdio: 'pipe' })
177-
return true
182+
const result = await spawn('rustc', ['--version'], {
183+
shell: WIN32,
184+
stdio: 'pipe',
185+
})
186+
return result.code === 0
178187
} catch {
179188
return false
180189
}
@@ -187,8 +196,11 @@ export class RustBuilder {
187196
*/
188197
async checkWasmBindgenInstalled() {
189198
try {
190-
await exec('wasm-bindgen --version', { stdio: 'pipe' })
191-
return true
199+
const result = await spawn('wasm-bindgen', ['--version'], {
200+
shell: WIN32,
201+
stdio: 'pipe',
202+
})
203+
return result.code === 0
192204
} catch {
193205
return false
194206
}
@@ -201,8 +213,11 @@ export class RustBuilder {
201213
*/
202214
async checkWasmOptInstalled() {
203215
try {
204-
await exec('wasm-opt --version', { stdio: 'pipe' })
205-
return true
216+
const result = await spawn('wasm-opt', ['--version'], {
217+
shell: WIN32,
218+
stdio: 'pipe',
219+
})
220+
return result.code === 0
206221
} catch {
207222
return false
208223
}

0 commit comments

Comments
 (0)