Stream CLI server command stderr instead of buffering
This commit is contained in:
@@ -362,6 +362,8 @@ export class CodeQLCliServer implements Disposable {
|
||||
silent?: boolean,
|
||||
): Promise<string> {
|
||||
const stderrBuffers: Buffer[] = [];
|
||||
// The array of fragments of stderr of this line. To be used for logging.
|
||||
let currentLineStderrBuffers: Buffer[] = [];
|
||||
if (this.commandInProcess) {
|
||||
throw new Error("runCodeQlCliInternal called while cli was running");
|
||||
}
|
||||
@@ -419,6 +421,32 @@ export class CodeQLCliServer implements Disposable {
|
||||
// Listen to stderr
|
||||
process.stderr.addListener("data", (newData: Buffer) => {
|
||||
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.
|
||||
process.addListener("close", (code) =>
|
||||
@@ -433,6 +461,10 @@ export class CodeQLCliServer implements Disposable {
|
||||
// Make sure we remove the terminator;
|
||||
const data = fullBuffer.toString("utf8", 0, fullBuffer.length - 1);
|
||||
if (!silent) {
|
||||
void this.logger.log(
|
||||
Buffer.concat(currentLineStderrBuffers).toString("utf8"),
|
||||
);
|
||||
currentLineStderrBuffers = [];
|
||||
void this.logger.log("CLI command succeeded.");
|
||||
}
|
||||
return data;
|
||||
@@ -452,8 +484,10 @@ export class CodeQLCliServer implements Disposable {
|
||||
cliError.stack += getErrorStack(err);
|
||||
throw cliError;
|
||||
} finally {
|
||||
if (!silent) {
|
||||
void this.logger.log(Buffer.concat(stderrBuffers).toString("utf8"));
|
||||
if (!silent && currentLineStderrBuffers.length > 0) {
|
||||
void this.logger.log(
|
||||
Buffer.concat(currentLineStderrBuffers).toString("utf8"),
|
||||
);
|
||||
}
|
||||
// Remove the listeners we set up.
|
||||
process.stdout.removeAllListeners("data");
|
||||
|
||||
Reference in New Issue
Block a user