Skip to content

Commit db6e5ff

Browse files
committed
Extract separate function for warnIfGoInstalledAfterInit
1 parent cf7e9f2 commit db6e5ff

File tree

2 files changed

+57
-43
lines changed

2 files changed

+57
-43
lines changed

src/analyze-action.ts

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import path from "path";
33
import { performance } from "perf_hooks";
44

55
import * as core from "@actions/core";
6-
import { safeWhich } from "@chrisgavin/safe-which";
76

87
import * as actionsUtil from "./actions-util";
98
import {
@@ -13,13 +12,13 @@ import {
1312
runCleanup,
1413
runFinalize,
1514
runQueries,
15+
warnIfGoInstalledAfterInit,
1616
} from "./analyze";
1717
import { getApiDetails, getGitHubVersion } from "./api-client";
1818
import { runAutobuild } from "./autobuild";
1919
import { getCodeQL } from "./codeql";
2020
import { Config, getConfig } from "./config-utils";
2121
import { uploadDatabases } from "./database-upload";
22-
import { addDiagnostic, makeDiagnostic } from "./diagnostics";
2322
import { EnvVar } from "./environment";
2423
import { Features } from "./feature-flags";
2524
import { Language } from "./languages";
@@ -140,6 +139,12 @@ async function runAutobuildIfLegacyGoWorkflow(config: Config, logger: Logger) {
140139
if (!config.languages.includes(Language.go)) {
141140
return;
142141
}
142+
if (config.buildMode) {
143+
logger.debug(
144+
"Skipping legacy Go autobuild since a build mode has been specified.",
145+
);
146+
return;
147+
}
143148
if (process.env[EnvVar.DID_AUTOBUILD_GOLANG] === "true") {
144149
logger.debug("Won't run Go autobuild since it has already been run.");
145150
return;
@@ -234,46 +239,7 @@ async function run() {
234239
logger,
235240
);
236241

237-
// Check that `which go` still points at the same path it did when the `init` Action ran to ensure that no steps
238-
// in-between performed any setup. We encourage users to perform all setup tasks before initializing CodeQL so that
239-
// the setup tasks do not interfere with our analysis.
240-
// Furthermore, if we installed a wrapper script in the `init` Action, we need to ensure that there isn't a step
241-
// in the workflow after the `init` step which installs a different version of Go and takes precedence in the PATH,
242-
// thus potentially circumventing our workaround that allows tracing to work.
243-
const goInitPath = process.env[EnvVar.GO_BINARY_LOCATION];
244-
245-
if (
246-
process.env[EnvVar.DID_AUTOBUILD_GOLANG] !== "true" &&
247-
goInitPath !== undefined
248-
) {
249-
const goBinaryPath = await safeWhich("go");
250-
251-
if (goInitPath !== goBinaryPath) {
252-
core.warning(
253-
`Expected \`which go\` to return ${goInitPath}, but got ${goBinaryPath}: please ensure that the correct version of Go is installed before the \`codeql-action/init\` Action is used.`,
254-
);
255-
256-
addDiagnostic(
257-
config,
258-
Language.go,
259-
makeDiagnostic(
260-
"go/workflow/go-installed-after-codeql-init",
261-
"Go was installed after the `codeql-action/init` Action was run",
262-
{
263-
markdownMessage:
264-
"To avoid interfering with the CodeQL analysis, perform all installation steps before calling the `github/codeql-action/init` Action.",
265-
visibility: {
266-
statusPage: true,
267-
telemetry: true,
268-
cliSummaryTable: true,
269-
},
270-
severity: "warning",
271-
},
272-
),
273-
);
274-
}
275-
}
276-
242+
await warnIfGoInstalledAfterInit(config, logger);
277243
await runAutobuildIfLegacyGoWorkflow(config, logger);
278244

279245
dbCreationTimings = await runFinalize(
@@ -337,7 +303,7 @@ async function run() {
337303

338304
// We don't upload results in test mode, so don't wait for processing
339305
if (util.isInTestMode()) {
340-
core.debug("In test mode. Waiting for processing is disabled.");
306+
logger.debug("In test mode. Waiting for processing is disabled.");
341307
} else if (
342308
uploadResult !== undefined &&
343309
actionsUtil.getRequiredInput("wait-for-processing") === "true"

src/analyze.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from "path";
33
import { performance } from "perf_hooks";
44

55
import * as toolrunner from "@actions/exec/lib/toolrunner";
6+
import { safeWhich } from "@chrisgavin/safe-which";
67
import del from "del";
78
import * as yaml from "js-yaml";
89

@@ -12,6 +13,8 @@ import {
1213
getCodeQL,
1314
} from "./codeql";
1415
import * as configUtils from "./config-utils";
16+
import { addDiagnostic, makeDiagnostic } from "./diagnostics";
17+
import { EnvVar } from "./environment";
1518
import {
1619
FeatureEnablement,
1720
Feature,
@@ -419,6 +422,51 @@ export async function runFinalize(
419422
return timings;
420423
}
421424

425+
export async function warnIfGoInstalledAfterInit(
426+
config: configUtils.Config,
427+
logger: Logger,
428+
) {
429+
// Check that `which go` still points at the same path it did when the `init` Action ran to ensure that no steps
430+
// in-between performed any setup. We encourage users to perform all setup tasks before initializing CodeQL so that
431+
// the setup tasks do not interfere with our analysis.
432+
// Furthermore, if we installed a wrapper script in the `init` Action, we need to ensure that there isn't a step
433+
// in the workflow after the `init` step which installs a different version of Go and takes precedence in the PATH,
434+
// thus potentially circumventing our workaround that allows tracing to work.
435+
const goInitPath = process.env[EnvVar.GO_BINARY_LOCATION];
436+
437+
if (
438+
process.env[EnvVar.DID_AUTOBUILD_GOLANG] !== "true" &&
439+
goInitPath !== undefined
440+
) {
441+
const goBinaryPath = await safeWhich("go");
442+
443+
if (goInitPath !== goBinaryPath) {
444+
logger.warning(
445+
`Expected \`which go\` to return ${goInitPath}, but got ${goBinaryPath}: please ensure that the correct version of Go is installed before the \`codeql-action/init\` Action is used.`,
446+
);
447+
448+
addDiagnostic(
449+
config,
450+
Language.go,
451+
makeDiagnostic(
452+
"go/workflow/go-installed-after-codeql-init",
453+
"Go was installed after the `codeql-action/init` Action was run",
454+
{
455+
markdownMessage:
456+
"To avoid interfering with the CodeQL analysis, perform all installation steps before calling the `github/codeql-action/init` Action.",
457+
visibility: {
458+
statusPage: true,
459+
telemetry: true,
460+
cliSummaryTable: true,
461+
},
462+
severity: "warning",
463+
},
464+
),
465+
);
466+
}
467+
}
468+
}
469+
422470
export async function runCleanup(
423471
config: configUtils.Config,
424472
cleanupLevel: string,

0 commit comments

Comments
 (0)