Skip to content

Commit ce21dc4

Browse files
committed
Strip debug flags from subprocess NODE_OPTIONS
Fixes #14
1 parent 909589b commit ce21dc4

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

subprocess.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ export default function makeSynchronous(function_) {
3131
}
3232
`;
3333

34+
const env = {...process.env, ELECTRON_RUN_AS_NODE: '1'};
35+
36+
// Prevent debugger flags from the parent from forcing the child to open a debug port.
37+
env.NODE_OPTIONS &&= env.NODE_OPTIONS
38+
.split(/\s+/)
39+
.filter(option => !/^--(?:inspect|debug)/.test(option))
40+
.join(' ');
41+
3442
const {error: subprocessError, stdout, stderr} = childProcess.spawnSync(process.execPath, ['--input-type=module', '-'], {
3543
input,
3644
encoding: 'utf8',
3745
maxBuffer: HUNDRED_MEGABYTES,
38-
env: {
39-
...process.env,
40-
ELECTRON_RUN_AS_NODE: '1',
41-
},
46+
env,
4247
});
4348

4449
if (subprocessError) {

test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,23 @@ for (const {type, makeSynchronous} of [
6969
}
7070
});
7171
}
72+
73+
test.serial('[childprocess] strips debug flags from NODE_OPTIONS', t => {
74+
const function_ = makeSynchronousByChildprocess(async () => {
75+
const {default: inspector} = await import('node:inspector');
76+
return {inspectorUrl: inspector.url()};
77+
});
78+
79+
const originalNodeOptions = process.env.NODE_OPTIONS; // eslint-disable-line n/prefer-global/process
80+
process.env.NODE_OPTIONS = '--inspect=127.0.0.1:0'; // eslint-disable-line n/prefer-global/process
81+
try {
82+
const {inspectorUrl} = function_();
83+
t.is(inspectorUrl, undefined);
84+
} finally {
85+
if (originalNodeOptions === undefined) {
86+
delete process.env.NODE_OPTIONS; // eslint-disable-line n/prefer-global/process
87+
} else {
88+
process.env.NODE_OPTIONS = originalNodeOptions; // eslint-disable-line n/prefer-global/process
89+
}
90+
}
91+
});

0 commit comments

Comments
 (0)