Skip to content

Commit

Permalink
Surface autobuild errors from stderr stream
Browse files Browse the repository at this point in the history
  • Loading branch information
henrymercer committed Mar 14, 2024
1 parent f055b5e commit 88b28eb
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 24 deletions.
38 changes: 28 additions & 10 deletions lib/cli-errors.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/cli-errors.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions lib/codeql.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/codeql.test.js.map

Large diffs are not rendered by default.

43 changes: 31 additions & 12 deletions src/cli-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,31 @@ export class CommandInvocationError extends Error {
.join(" ");

const fatalErrors = extractFatalErrors(stderr);
const lastLine = stderr.trim().split("\n").pop()?.trim();
let error = fatalErrors
? ` and error was: ${fatalErrors.trim()}`
: lastLine
? ` and last log line was: ${lastLine}`
: "";
if (error[error.length - 1] !== ".") {
error += ".";
const autobuildErrors = extractAutobuildErrors(stderr);
let message: string;

if (fatalErrors) {
message =
`Encountered a fatal error while running "${prettyCommand}". ` +
`Exit code was ${exitCode} and error was: ${fatalErrors.trim()} See the logs for more details.`;
} else if (autobuildErrors) {
const autobuildHelpLink =
"https://docs.github.com/en/code-security/code-scanning/troubleshooting-code-scanning/automatic-build-failed";
message =
"We were unable to automatically build your code. Please provide manual build steps. " +
`For more information, see ${autobuildHelpLink}. ` +
`Encountered the following error: ${autobuildErrors}`;
} else {
let lastLine = stderr.trim().split("\n").pop()?.trim() || "";
if (lastLine[lastLine.length - 1] !== ".") {
lastLine += ".";
}
message =
`Encountered a fatal error while running "${prettyCommand}". ` +
`Exit code was ${exitCode} and last log line was: ${lastLine} See the logs for more details.`;
}

super(
`Encountered a fatal error while running "${prettyCommand}". ` +
`Exit code was ${exitCode}${error} See the logs for more details.`,
);
super(message);
}
}

Expand Down Expand Up @@ -96,6 +107,14 @@ function extractFatalErrors(error: string): string | undefined {
return undefined;
}

function extractAutobuildErrors(error: string): string | undefined {
const pattern = /.*\[autobuild\] \[ERROR\] (.*)/gi;
return (
[...error.matchAll(pattern)].map((match) => match[1]).join("\n") ||
undefined
);
}

function ensureEndsInPeriod(text: string): string {
return text[text.length - 1] === "." ? text : `${text}.`;
}
Expand Down
35 changes: 35 additions & 0 deletions src/codeql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as sinon from "sinon";

import * as actionsUtil from "./actions-util";
import { GitHubApiDetails } from "./api-client";
import { CommandInvocationError } from "./cli-errors";
import * as codeql from "./codeql";
import { AugmentationProperties, Config } from "./config-utils";
import * as defaults from "./defaults.json";
Expand Down Expand Up @@ -967,6 +968,40 @@ test("runTool summarizes several fatal errors", async (t) => {
);
});

test("runTool summarizes autobuilder errors", async (t) => {
const stderr = `
[2019-09-18 12:00:00] [autobuild] A non-error message
[2019-09-18 12:00:00] Untagged message
[2019-09-18 12:00:00] [autobuild] [ERROR] Start of the error message
[2019-09-18 12:00:00] [autobuild] An interspersed non-error message
[2019-09-18 12:00:01] [autobuild] [ERROR] Some more context about the error message
[2019-09-18 12:00:01] [autobuild] [ERROR] continued
[2019-09-18 12:00:01] [autobuild] [ERROR] and finished here.
[2019-09-18 12:00:01] [autobuild] A non-error message
`;
stubToolRunnerConstructor(1, stderr);
const codeqlObject = await codeql.getCodeQLForTesting();
sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.12.4"));
sinon.stub(codeqlObject, "resolveExtractor").resolves("/path/to/extractor");
// safeWhich throws because of the test CodeQL object.
sinon.stub(safeWhich, "safeWhich").resolves("");

await t.throwsAsync(
async () => await codeqlObject.runAutobuild(Language.java, false),
{
instanceOf: CommandInvocationError,
message:
"We were unable to automatically build your code. Please provide manual build steps. " +
"For more information, see " +
"https://docs.github.com/en/code-security/code-scanning/troubleshooting-code-scanning/automatic-build-failed. " +
"Encountered the following error: Start of the error message\n" +
" Some more context about the error message\n" +
" continued\n" +
" and finished here.",
},
);
});

test("runTool outputs last line of stderr if fatal error could not be found", async (t) => {
const cliStderr = "line1\nline2\nline3\nline4\nline5";
stubToolRunnerConstructor(32, cliStderr);
Expand Down

0 comments on commit 88b28eb

Please sign in to comment.