Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): collect all logs from forked processes #27778

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions packages/nx/src/tasks-runner/forked-process-task-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,29 +339,53 @@ export class ForkedProcessTaskRunner {
}

let outWithErr = [];
let exitCode;
let stdoutHasEnded = false;
let stderrHasEnded = false;
let processHasExited = false;

const handleProcessEnd = () => {
// ensure process has exited and both stdout and stderr have ended before we pass along the logs
// if we only wait for the process to exit, we might miss some logs as stdout and stderr might still be streaming
if (stdoutHasEnded && stderrHasEnded && processHasExited) {
// we didn't print any output as we were running the command
// print all the collected output|
const terminalOutput = outWithErr.join('');
const code = exitCode;

if (!streamOutput) {
this.options.lifeCycle.printTaskTerminalOutput(
task,
code === 0 ? 'success' : 'failure',
terminalOutput
);
}
this.writeTerminalOutput(temporaryOutputPath, terminalOutput);
res({ code, terminalOutput });
}
};

p.stdout.on('data', (chunk) => {
outWithErr.push(chunk.toString());
});
p.stdout.on('end', () => {
stdoutHasEnded = true;
handleProcessEnd();
});
p.stderr.on('data', (chunk) => {
outWithErr.push(chunk.toString());
});
p.stderr.on('end', () => {
stderrHasEnded = true;
handleProcessEnd();
});

p.on('exit', (code, signal) => {
this.processes.delete(p);
if (code === null) code = signalToCode(signal);
// we didn't print any output as we were running the command
// print all the collected output|
const terminalOutput = outWithErr.join('');

if (!streamOutput) {
this.options.lifeCycle.printTaskTerminalOutput(
task,
code === 0 ? 'success' : 'failure',
terminalOutput
);
}
this.writeTerminalOutput(temporaryOutputPath, terminalOutput);
res({ code, terminalOutput });
exitCode = code;
processHasExited = true;
handleProcessEnd();
});
} catch (e) {
console.error(e);
Expand Down