Skip to content

Commit 4ab9237

Browse files
authored
Merge pull request #1927 from github/henrymercer/reduce-log-duplication
Reduce duplication in the logs when errors occur in CLI commands
2 parents 2125352 + 83d1db3 commit 4ab9237

File tree

10 files changed

+72
-29
lines changed

10 files changed

+72
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ See the [releases page](https://github.com/github/codeql-action/releases) for th
44

55
## [UNRELEASED]
66

7-
No user facing changes.
7+
- Improve the log output when an error occurs in an invocation of the CodeQL CLI. [#1927](https://github.com/github/codeql-action/pull/1927)
88

99
## 2.22.1 - 09 Oct 2023
1010

lib/analyze.js

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/analyze.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.js

Lines changed: 15 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.test.js

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/analyze.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,10 @@ export async function runQueries(
394394
await runPrintLinesOfCode(language);
395395
}
396396
} catch (e) {
397-
logger.info(String(e));
398-
if (e instanceof Error) {
399-
logger.info(e.stack!);
400-
}
401397
statusReport.analyze_failure_language = language;
402398
throw new CodeQLAnalysisError(
403399
statusReport,
404-
`Error running analysis for ${language}: ${e}`,
400+
`Error running analysis for ${language}: ${util.wrapError(e).message}`,
405401
);
406402
}
407403
}

src/codeql.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ test("database finalize does not override no code found error on CodeQL 2.12.4",
11331133
{
11341134
message:
11351135
'Encountered a fatal error while running "codeql-for-testing database finalize --finalize-dataset --threads=2 --ram=2048 db". ' +
1136-
`Exit code was 32 and error was: ${cliMessage}`,
1136+
`Exit code was 32 and last log line was: ${cliMessage} See the logs for more details.`,
11371137
},
11381138
);
11391139
});
@@ -1158,7 +1158,26 @@ test("runTool summarizes several fatal errors", async (t) => {
11581158
{
11591159
message:
11601160
'Encountered a fatal error while running "codeql-for-testing database finalize --finalize-dataset --threads=2 --ram=2048 db". ' +
1161-
`Exit code was 32 and error was: ${datasetImportError}. Context: ${heapError}.`,
1161+
`Exit code was 32 and error was: ${datasetImportError}. Context: ${heapError}. See the logs for more details.`,
1162+
},
1163+
);
1164+
});
1165+
1166+
test("runTool outputs last line of stderr if fatal error could not be found", async (t) => {
1167+
const cliStderr = "line1\nline2\nline3\nline4\nline5";
1168+
stubToolRunnerConstructor(32, cliStderr);
1169+
const codeqlObject = await codeql.getCodeQLForTesting();
1170+
sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.12.4"));
1171+
// safeWhich throws because of the test CodeQL object.
1172+
sinon.stub(safeWhich, "safeWhich").resolves("");
1173+
1174+
await t.throwsAsync(
1175+
async () =>
1176+
await codeqlObject.finalizeDatabase("db", "--threads=2", "--ram=2048"),
1177+
{
1178+
message:
1179+
'Encountered a fatal error while running "codeql-for-testing database finalize --finalize-dataset --threads=2 --ram=2048 db". ' +
1180+
"Exit code was 32 and last log line was: line5. See the logs for more details.",
11621181
},
11631182
);
11641183
});

src/codeql.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,27 @@ export class CommandInvocationError extends Error {
5454
cmd: string,
5555
args: string[],
5656
public exitCode: number,
57-
public error: string,
58-
public output: string,
57+
public stderr: string,
58+
public stdout: string,
5959
) {
6060
const prettyCommand = [cmd, ...args]
6161
.map((x) => (x.includes(" ") ? `'${x}'` : x))
6262
.join(" ");
63+
64+
const fatalErrors = extractFatalErrors(stderr);
65+
const lastLine = stderr.trim().split("\n").pop()?.trim();
66+
let error = fatalErrors
67+
? ` and error was: ${fatalErrors.trim()}`
68+
: lastLine
69+
? ` and last log line was: ${lastLine}`
70+
: "";
71+
if (error[error.length - 1] !== ".") {
72+
error += ".";
73+
}
74+
6375
super(
6476
`Encountered a fatal error while running "${prettyCommand}". ` +
65-
`Exit code was ${exitCode} and error was: ${error.trim()}`,
77+
`Exit code was ${exitCode}${error} See the logs for more details.`,
6678
);
6779
}
6880
}
@@ -1238,7 +1250,6 @@ async function runTool(
12381250
...(opts.stdin ? { input: Buffer.from(opts.stdin || "") } : {}),
12391251
}).exec();
12401252
if (exitCode !== 0) {
1241-
error = extractFatalErrors(error) || error;
12421253
throw new CommandInvocationError(cmd, args, exitCode, error, output);
12431254
}
12441255
return output;
@@ -1454,7 +1465,7 @@ function isNoCodeFoundError(e: CommandInvocationError): boolean {
14541465
*/
14551466
const javascriptNoCodeFoundWarning =
14561467
"No JavaScript or TypeScript code found.";
1457-
return e.exitCode === 32 || e.error.includes(javascriptNoCodeFoundWarning);
1468+
return e.exitCode === 32 || e.stderr.includes(javascriptNoCodeFoundWarning);
14581469
}
14591470

14601471
async function isDiagnosticsExportInvalidSarifFixed(

0 commit comments

Comments
 (0)