Skip to content

Commit b45c820

Browse files
vzaidmanmeta-codesync[bot]
authored andcommitted
Fix tests and reland "[debugger-shell] add debug statements to debugger shell launching path" (#54978)
Summary: Pull Request resolved: #54978 There's a bug in Node.JS: nodejs/node#51018 where spawning a process on Windows with detached mode and unref will not actually inherit stdio. You need to manually pipe these to get to see stdout and stderr. It works well on MacOS though. Changelog: [Internal] Reviewed By: robhogan Differential Revision: D89765226 fbshipit-source-id: c330df6ed0dbb27b13df89cd2c544a821077dd3f
1 parent 68aab49 commit b45c820

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

packages/debugger-shell/src/node/index.flow.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from './private/LaunchUtils';
1515

1616
const {spawn} = require('cross-spawn');
17+
const debug = require('debug')('Metro:DebuggerShell');
1718
const path = require('path');
1819

1920
// The 'prebuilt' flavor will use the prebuilt shell binary (and the JavaScript embedded in it).
@@ -31,6 +32,7 @@ async function unstable_spawnDebuggerShellWithArgs(
3132
mode = 'detached',
3233
flavor = process.env.RNDT_DEV === '1' ? 'dev' : 'prebuilt',
3334
prebuiltBinaryPath,
35+
silent = process.env.NODE_ENV === 'test',
3436
}: Readonly<{
3537
// In 'syncAndExit' mode, the current process will block until the spawned process exits, and then it will exit
3638
// with the same exit code as the spawned process.
@@ -39,6 +41,7 @@ async function unstable_spawnDebuggerShellWithArgs(
3941
mode?: 'syncThenExit' | 'detached',
4042
flavor?: DebuggerShellFlavor,
4143
prebuiltBinaryPath?: ?string,
44+
silent?: boolean,
4245
}> = {},
4346
): Promise<void> {
4447
const [binaryPath, baseArgs] = getShellBinaryAndArgs(
@@ -55,36 +58,63 @@ async function unstable_spawnDebuggerShellWithArgs(
5558
...env
5659
} = process.env;
5760
const child = spawn(binaryPath, [...baseArgs, ...args], {
58-
stdio: 'inherit',
61+
stdio: ['ignore', 'pipe', 'pipe'],
5962
windowsHide: true,
6063
detached: mode === 'detached',
6164
env,
6265
});
6366
if (mode === 'detached') {
6467
child.on('spawn', () => {
68+
debug('Debugger spawned in detached mode');
6569
resolve();
6670
});
67-
child.on('close', (code: number) => {
71+
child.on('close', (code: number, signal: string) => {
72+
debug('Debugger closed with code %s and signal %s', code, signal);
6873
if (code !== 0) {
74+
if (!silent) {
75+
console.error(
76+
'Debugger exited with non-zero code (code: %s, signal: %s)',
77+
code,
78+
signal,
79+
);
80+
}
6981
reject(
7082
new Error(
7183
`Failed to open debugger shell: exited with code ${code}`,
7284
),
7385
);
7486
}
7587
});
88+
child.on('error', error => {
89+
debug('Debugger shell encountered error: %s', error);
90+
reject(error);
91+
});
92+
if (!silent) {
93+
child.stdout.on('data', data =>
94+
console.log('[debugger-shell stdout] ' + String(data)),
95+
);
96+
child.stderr.on('data', data =>
97+
console.warn('[debugger-shell stderr] ' + String(data)),
98+
);
99+
}
76100
child.unref();
77101
} else if (mode === 'syncThenExit') {
78102
child.on('close', function (code, signal) {
79-
if (code === null) {
80-
console.error('Debugger shell exited with signal', signal);
103+
debug('Debugger exited with code %s and signal %s', code, signal);
104+
if (code === null && !silent) {
105+
console.error(
106+
'Debugger exited with code %s and signal %s',
107+
code,
108+
signal,
109+
);
81110
process.exit(1);
82111
}
83112
process.exit(code);
84113
});
85114

86115
const handleTerminationSignal = function (signal: string) {
87116
process.on(signal, function signalHandler() {
117+
debug('Received signal %s. Killing debugger shell.', signal);
88118
if (!child.killed) {
89119
child.kill(signal);
90120
}

0 commit comments

Comments
 (0)