Skip to content

Commit be5fc27

Browse files
committed
fix(tests): use Proxy in test mode to preserve Windows env behavior
In test mode (VITEST=1), use a Proxy that dynamically reads from process.env instead of spreading into a plain object. This preserves Windows process.env Proxy behavior through the entire spawn chain. In production, continue using a static snapshot for performance. Key benefits: - Proxy preserves case-insensitive env var access on Windows - Dynamic lookups ensure current env values are always used - No performance impact in production (still uses static snapshot) - Fixes empty CLI output issue on Windows CI runners
1 parent ec3a565 commit be5fc27

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

packages/cli/test/utils.mts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,50 @@ export async function spawnSocketCli(
266266
const commandArgs = isJsFile ? [entryPath, ...args] : args
267267

268268
try {
269+
// In test mode, use a Proxy to preserve Windows process.env Proxy behavior.
270+
// In production, use a static snapshot for performance.
271+
const env = process.env['VITEST']
272+
? new Proxy(
273+
{},
274+
{
275+
get(_target, prop) {
276+
// Priority: spawnEnv > constants.processEnv
277+
if (spawnEnv && prop in spawnEnv) {
278+
return spawnEnv[prop]
279+
}
280+
return constants.processEnv[prop]
281+
},
282+
ownKeys(_target) {
283+
const keys = new Set([
284+
...Object.keys(constants.processEnv),
285+
...(spawnEnv ? Object.keys(spawnEnv) : []),
286+
])
287+
return [...keys]
288+
},
289+
getOwnPropertyDescriptor(_target, prop) {
290+
const value =
291+
(spawnEnv && prop in spawnEnv
292+
? spawnEnv[prop]
293+
: constants.processEnv[prop]) ?? undefined
294+
return value !== undefined
295+
? {
296+
enumerable: true,
297+
configurable: true,
298+
value,
299+
}
300+
: undefined
301+
},
302+
},
303+
)
304+
: {
305+
...process.env,
306+
...constants.processEnv,
307+
...spawnEnv,
308+
}
309+
269310
const output = await spawn(command, commandArgs, {
270311
cwd,
271-
env: {
272-
...process.env,
273-
...constants.processEnv,
274-
...spawnEnv,
275-
},
312+
env,
276313
...restOptions,
277314
// Close stdin to prevent tests from hanging
278315
// when commands wait for input. Must be after restOptions

0 commit comments

Comments
 (0)