Skip to content

Commit 5387546

Browse files
authored
Don't log the resolve queries CLI command (#2454)
1 parent bc34202 commit 5387546

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

extensions/ql-vscode/src/codeql-cli/cli.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export class CodeQLCliServer implements Disposable {
218218
private readonly app: App,
219219
private distributionProvider: DistributionProvider,
220220
private cliConfig: CliConfig,
221-
private logger: Logger,
221+
public readonly logger: Logger,
222222
) {
223223
this.commandQueue = [];
224224
this.commandInProcess = false;
@@ -330,6 +330,7 @@ export class CodeQLCliServer implements Disposable {
330330
commandArgs: string[],
331331
description: string,
332332
onLine?: OnLineCallback,
333+
silent?: boolean,
333334
): Promise<string> {
334335
const stderrBuffers: Buffer[] = [];
335336
if (this.commandInProcess) {
@@ -349,7 +350,12 @@ export class CodeQLCliServer implements Disposable {
349350
// Compute the full args array
350351
const args = command.concat(LOGGING_FLAGS).concat(commandArgs);
351352
const argsString = args.join(" ");
352-
void this.logger.log(`${description} using CodeQL CLI: ${argsString}...`);
353+
// If we are running silently, we don't want to print anything to the console.
354+
if (!silent) {
355+
void this.logger.log(
356+
`${description} using CodeQL CLI: ${argsString}...`,
357+
);
358+
}
353359
try {
354360
await new Promise<void>((resolve, reject) => {
355361
// Start listening to stdout
@@ -395,24 +401,30 @@ export class CodeQLCliServer implements Disposable {
395401
const fullBuffer = Buffer.concat(stdoutBuffers);
396402
// Make sure we remove the terminator;
397403
const data = fullBuffer.toString("utf8", 0, fullBuffer.length - 1);
398-
void this.logger.log("CLI command succeeded.");
404+
if (!silent) {
405+
void this.logger.log("CLI command succeeded.");
406+
}
399407
return data;
400408
} catch (err) {
401409
// Kill the process if it isn't already dead.
402410
this.killProcessIfRunning();
403-
// Report the error (if there is a stderr then use that otherwise just report the error cod or nodejs error)
411+
// Report the error (if there is a stderr then use that otherwise just report the error code or nodejs error)
404412
const newError =
405413
stderrBuffers.length === 0
406-
? new Error(`${description} failed: ${err}`)
414+
? new Error(
415+
`${description} failed with args:${EOL} ${argsString}${EOL}${err}`,
416+
)
407417
: new Error(
408-
`${description} failed: ${Buffer.concat(stderrBuffers).toString(
409-
"utf8",
410-
)}`,
418+
`${description} failed with args:${EOL} ${argsString}${EOL}${Buffer.concat(
419+
stderrBuffers,
420+
).toString("utf8")}`,
411421
);
412422
newError.stack += getErrorStack(err);
413423
throw newError;
414424
} finally {
415-
void this.logger.log(Buffer.concat(stderrBuffers).toString("utf8"));
425+
if (!silent) {
426+
void this.logger.log(Buffer.concat(stderrBuffers).toString("utf8"));
427+
}
416428
// Remove the listeners we set up.
417429
process.stdout.removeAllListeners("data");
418430
process.stderr.removeAllListeners("data");
@@ -549,9 +561,11 @@ export class CodeQLCliServer implements Disposable {
549561
{
550562
progressReporter,
551563
onLine,
564+
silent = false,
552565
}: {
553566
progressReporter?: ProgressReporter;
554567
onLine?: OnLineCallback;
568+
silent?: boolean;
555569
} = {},
556570
): Promise<string> {
557571
if (progressReporter) {
@@ -567,6 +581,7 @@ export class CodeQLCliServer implements Disposable {
567581
commandArgs,
568582
description,
569583
onLine,
584+
silent,
570585
).then(resolve, reject);
571586
} catch (err) {
572587
reject(err);
@@ -600,10 +615,12 @@ export class CodeQLCliServer implements Disposable {
600615
addFormat = true,
601616
progressReporter,
602617
onLine,
618+
silent = false,
603619
}: {
604620
addFormat?: boolean;
605621
progressReporter?: ProgressReporter;
606622
onLine?: OnLineCallback;
623+
silent?: boolean;
607624
} = {},
608625
): Promise<OutputType> {
609626
let args: string[] = [];
@@ -614,6 +631,7 @@ export class CodeQLCliServer implements Disposable {
614631
const result = await this.runCodeQlCliCommand(command, args, description, {
615632
progressReporter,
616633
onLine,
634+
silent,
617635
});
618636
try {
619637
return JSON.parse(result) as OutputType;
@@ -739,14 +757,19 @@ export class CodeQLCliServer implements Disposable {
739757
/**
740758
* Finds all available queries in a given directory.
741759
* @param queryDir Root of directory tree to search for queries.
760+
* @param silent If true, don't print logs to the CodeQL extension log.
742761
* @returns The list of queries that were found.
743762
*/
744-
public async resolveQueries(queryDir: string): Promise<ResolvedQueries> {
763+
public async resolveQueries(
764+
queryDir: string,
765+
silent?: boolean,
766+
): Promise<ResolvedQueries> {
745767
const subcommandArgs = [queryDir];
746768
return await this.runJsonCodeQlCliCommand<ResolvedQueries>(
747769
["resolve", "queries"],
748770
subcommandArgs,
749771
"Resolving queries",
772+
{ silent },
750773
);
751774
}
752775

extensions/ql-vscode/src/queries-panel/query-discovery.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ export class QueryDiscovery
114114
const fullPath = workspaceFolder.uri.fsPath;
115115
const name = workspaceFolder.name;
116116

117-
const resolvedQueries = await this.cliServer.resolveQueries(fullPath);
117+
// We don't want to log each invocation of resolveQueries, since it clutters up the log.
118+
const silent = true;
119+
const resolvedQueries = await this.cliServer.resolveQueries(
120+
fullPath,
121+
silent,
122+
);
118123
if (resolvedQueries.length === 0) {
119124
return undefined;
120125
}

extensions/ql-vscode/test/vscode-tests/cli-integration/run-cli.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { KeyType, resolveQueries } from "../../../src/language-support";
1616
import { faker } from "@faker-js/faker";
1717
import { getActivatedExtension } from "../global.helper";
18+
import { BaseLogger } from "../../../src/common";
1819

1920
/**
2021
* Perform proper integration tests by running the CLI
@@ -23,10 +24,14 @@ describe("Use cli", () => {
2324
let cli: CodeQLCliServer;
2425
let supportedLanguages: string[];
2526

27+
let logSpy: jest.SpiedFunction<BaseLogger["log"]>;
28+
2629
beforeEach(async () => {
2730
const extension = await getActivatedExtension();
2831
cli = extension.cliServer;
2932
supportedLanguages = await cli.getSupportedLanguages();
33+
34+
logSpy = jest.spyOn(cli.logger, "log");
3035
});
3136

3237
if (process.env.CLI_VERSION && process.env.CLI_VERSION !== "nightly") {
@@ -42,6 +47,23 @@ describe("Use cli", () => {
4247
expect(result).toEqual(["-J-Xmx4096M", "--off-heap-ram=4096"]);
4348
});
4449

50+
describe("silent logging", () => {
51+
it("should log command output", async () => {
52+
const queryDir = getOnDiskWorkspaceFolders()[0];
53+
await cli.resolveQueries(queryDir);
54+
55+
expect(logSpy).toHaveBeenCalled();
56+
});
57+
58+
it("shouldn't log command output if the `silent` flag is set", async () => {
59+
const queryDir = getOnDiskWorkspaceFolders()[0];
60+
const silent = true;
61+
await cli.resolveQueries(queryDir, silent);
62+
63+
expect(logSpy).not.toHaveBeenCalled();
64+
});
65+
});
66+
4567
itWithCodeQL()("should resolve query packs", async () => {
4668
const qlpacks = await cli.resolveQlpacks(getOnDiskWorkspaceFolders());
4769
// Depending on the version of the CLI, the qlpacks may have different names

0 commit comments

Comments
 (0)