Stream CLI server command stderr instead of buffering

This commit is contained in:
Koen Vlaswinkel
2024-02-16 13:14:38 +01:00
parent d2f4cd12a1
commit 5ddc3c11d3

View File

@@ -362,6 +362,8 @@ export class CodeQLCliServer implements Disposable {
silent?: boolean, silent?: boolean,
): Promise<string> { ): Promise<string> {
const stderrBuffers: Buffer[] = []; const stderrBuffers: Buffer[] = [];
// The array of fragments of stderr of this line. To be used for logging.
let currentLineStderrBuffers: Buffer[] = [];
if (this.commandInProcess) { if (this.commandInProcess) {
throw new Error("runCodeQlCliInternal called while cli was running"); throw new Error("runCodeQlCliInternal called while cli was running");
} }
@@ -419,6 +421,32 @@ export class CodeQLCliServer implements Disposable {
// Listen to stderr // Listen to stderr
process.stderr.addListener("data", (newData: Buffer) => { process.stderr.addListener("data", (newData: Buffer) => {
stderrBuffers.push(newData); stderrBuffers.push(newData);
if (!silent) {
currentLineStderrBuffers.push(newData);
// Print the stderr to the logger as it comes in. We need to ensure that
// we don't split messages on the same line, so we buffer the stderr and
// split it on EOLs.
let currentLineBuffer = Buffer.concat(currentLineStderrBuffers);
const eolBuffer = Buffer.from(EOL);
if (currentLineBuffer.includes(eolBuffer)) {
while (currentLineBuffer.includes(eolBuffer)) {
const line = currentLineBuffer.subarray(
0,
currentLineBuffer.indexOf(eolBuffer),
);
void this.logger.log(line.toString("utf-8"));
currentLineBuffer = currentLineBuffer.subarray(
currentLineBuffer.indexOf(eolBuffer) + eolBuffer.length,
);
}
currentLineStderrBuffers = [currentLineBuffer];
}
}
}); });
// Listen for process exit. // Listen for process exit.
process.addListener("close", (code) => process.addListener("close", (code) =>
@@ -433,6 +461,10 @@ export class CodeQLCliServer implements Disposable {
// Make sure we remove the terminator; // Make sure we remove the terminator;
const data = fullBuffer.toString("utf8", 0, fullBuffer.length - 1); const data = fullBuffer.toString("utf8", 0, fullBuffer.length - 1);
if (!silent) { if (!silent) {
void this.logger.log(
Buffer.concat(currentLineStderrBuffers).toString("utf8"),
);
currentLineStderrBuffers = [];
void this.logger.log("CLI command succeeded."); void this.logger.log("CLI command succeeded.");
} }
return data; return data;
@@ -452,8 +484,10 @@ export class CodeQLCliServer implements Disposable {
cliError.stack += getErrorStack(err); cliError.stack += getErrorStack(err);
throw cliError; throw cliError;
} finally { } finally {
if (!silent) { if (!silent && currentLineStderrBuffers.length > 0) {
void this.logger.log(Buffer.concat(stderrBuffers).toString("utf8")); void this.logger.log(
Buffer.concat(currentLineStderrBuffers).toString("utf8"),
);
} }
// Remove the listeners we set up. // Remove the listeners we set up.
process.stdout.removeAllListeners("data"); process.stdout.removeAllListeners("data");