From 89606bbad18e5ed1b53cbf2167003a5006156e99 Mon Sep 17 00:00:00 2001 From: Henry Mercer Date: Wed, 16 Sep 2026 19:43:05 +0100 Subject: [PATCH] Return toolcache rejection reasons with Result Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- lib/entry-points.js | 37 ++++++++++++++++------------ src/setup-codeql.test.ts | 17 ++++++++++++- src/setup-codeql.ts | 52 +++++++++++++++++++++------------------- 3 files changed, 66 insertions(+), 40 deletions(-) diff --git a/lib/entry-points.js b/lib/entry-points.js index 0a0941fe9..fc50bae4f 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -152541,8 +152541,8 @@ var downloadCodeQL = async function(source, apiDetails, tarVersion, tempDir, log codeqlURL ); } - const toolcacheDestination = getToolcacheDestination(source, logger); - const extractedBundlePath = toolcacheDestination ?? getTempExtractionDir(tempDir); + const toolcacheDestination = getToolcacheDestination({ logger }, source); + const extractedBundlePath = toolcacheDestination.isSuccess() ? toolcacheDestination.value : getTempExtractionDir(tempDir); const statusReport = await downloadAndExtract( codeqlURL, compressionMethod, @@ -152552,27 +152552,34 @@ var downloadCodeQL = async function(source, apiDetails, tarVersion, tempDir, log tarVersion, logger ); - if (toolcacheDestination) { - writeToolcacheMarkerFile(toolcacheDestination, logger); + if (toolcacheDestination.isSuccess()) { + writeToolcacheMarkerFile(toolcacheDestination.value, logger); } else { - logger.debug( - bundle.kind === "per-language" ? "Not caching the CodeQL tools because they came from a bundle that contains only a single language." : `Could not cache CodeQL tools because we could not determine the bundle version from the URL ${codeqlURL}.` - ); + logger.debug(toolcacheDestination.value); } return { codeqlFolder: extractedBundlePath, statusReport: bundle.kind === "per-language" ? { ...statusReport, bundleLanguage: bundle.language } : statusReport }; }; -function getToolcacheDestination(source, logger) { - if (source.bundle.kind !== "combined" || !source.bundleVersion) { - return void 0; +function getToolcacheDestination({ logger }, source) { + if (source.bundle.kind !== "combined") { + return new Failure( + "Not caching the CodeQL tools because they came from a bundle that contains only a single language." + ); } - return getToolcacheDirectory( - getCanonicalToolcacheVersion( - source.cliVersion, - source.bundleVersion, - logger + if (!source.bundleVersion) { + return new Failure( + `Could not cache CodeQL tools because we could not determine the bundle version from the URL ${source.bundle.url}.` + ); + } + return new Success( + getToolcacheDirectory( + getCanonicalToolcacheVersion( + source.cliVersion, + source.bundleVersion, + logger + ) ) ); } diff --git a/src/setup-codeql.test.ts b/src/setup-codeql.test.ts index 3f440894d..c32c22b64 100644 --- a/src/setup-codeql.test.ts +++ b/src/setup-codeql.test.ts @@ -25,6 +25,7 @@ import { SAMPLE_DEFAULT_CLI_VERSION, SAMPLE_DOTCOM_API_DETAILS, checkExpectedLogMessages, + checkUnexpectedLogMessages, createFeatures, createTestConfig, getRecordingLogger, @@ -331,6 +332,10 @@ test.serial( downloadDurationMs: 200, totalDurationMs: 300, }); + checkUnexpectedLogMessages(t, loggedMessages, [ + "Not caching the CodeQL tools", + "Could not cache CodeQL tools", + ]); // Ensure message logging CodeQL CLI version was present in user logs. const expected_message: string = `Using CodeQL CLI version ${LINKED_CLI_VERSION.cliVersion}`; @@ -563,6 +568,9 @@ for (const bundlePath of [ t.deepEqual(toolcache.findAllVersions("CodeQL"), []); checkExpectedLogMessages(t, messages, [ `Using CodeQL CLI sourced from ${url}`, + bundlePath === "codeql-bundle-ruby-linux64.tar.zst" + ? "Not caching the CodeQL tools because they came from a bundle that contains only a single language." + : `Could not cache CodeQL tools because we could not determine the bundle version from the URL ${url}.`, ]); }); }, @@ -1473,6 +1481,7 @@ for (const asset of [ `setupCodeQLBundle keeps explicitly requested ${asset} out of the toolcache`, async (t) => { const extractStub = stubDownloadAndExtract(); + const messages: LoggedMessage[] = []; const url = `https://github.com/github/codeql-action/releases/download/codeql-bundle-v9.9.9/${asset}`; process.env[ActionsEnvVars.RUNNER_ENVIRONMENT] = "self-hosted"; @@ -1487,7 +1496,7 @@ for (const asset of [ undefined, // rawLanguages false, // useOverlayAwareDefaultCliVersion createFeatures([]), - getRunnerLogger(true), + getRecordingLogger(messages), ); t.true(extractStub.calledOnce); @@ -1500,6 +1509,12 @@ for (const asset of [ t.is(path.dirname(result.codeqlFolder), tmpDir); t.deepEqual(toolcache.findAllVersions("CodeQL"), []); t.false(fs.existsSync(`${result.codeqlFolder}.complete`)); + checkExpectedLogMessages(t, messages, [ + "Not caching the CodeQL tools because they came from a bundle that contains only a single language.", + ]); + checkUnexpectedLogMessages(t, messages, [ + "Could not cache CodeQL tools because we could not determine the bundle version", + ]); }); }, ); diff --git a/src/setup-codeql.ts b/src/setup-codeql.ts index ba51d0b36..4efb0dc2e 100644 --- a/src/setup-codeql.ts +++ b/src/setup-codeql.ts @@ -879,9 +879,10 @@ export const downloadCodeQL = async function ( ); } - const toolcacheDestination = getToolcacheDestination(source, logger); - const extractedBundlePath = - toolcacheDestination ?? getTempExtractionDir(tempDir); + const toolcacheDestination = getToolcacheDestination({ logger }, source); + const extractedBundlePath = toolcacheDestination.isSuccess() + ? toolcacheDestination.value + : getTempExtractionDir(tempDir); const statusReport = await downloadAndExtract( codeqlURL, @@ -893,16 +894,10 @@ export const downloadCodeQL = async function ( logger, ); - if (toolcacheDestination) { - writeToolcacheMarkerFile(toolcacheDestination, logger); + if (toolcacheDestination.isSuccess()) { + writeToolcacheMarkerFile(toolcacheDestination.value, logger); } else { - logger.debug( - bundle.kind === "per-language" - ? "Not caching the CodeQL tools because they came from a bundle that contains only a " + - "single language." - : "Could not cache CodeQL tools because we could not determine the bundle version from the " + - `URL ${codeqlURL}.`, - ); + logger.debug(toolcacheDestination.value); } return { @@ -915,23 +910,32 @@ export const downloadCodeQL = async function ( }; /** - * Returns the canonical toolcache directory for a combined bundle with a known version. - * Returns undefined for per-language bundles or unknown versions. + * Returns the canonical toolcache directory, or the reason the bundle cannot be cached. */ function getToolcacheDestination( + { logger }: ActionState<["Logger"]>, source: CodeQLDownloadSource, - logger: Logger, -): string | undefined { - // Per-language bundles must not be stored in the toolcache. - if (source.bundle.kind !== "combined" || !source.bundleVersion) { - return undefined; +): util.Result { + if (source.bundle.kind !== "combined") { + return new util.Failure( + "Not caching the CodeQL tools because they came from a bundle that contains only a " + + "single language.", + ); + } + if (!source.bundleVersion) { + return new util.Failure( + "Could not cache CodeQL tools because we could not determine the bundle version from the " + + `URL ${source.bundle.url}.`, + ); } - return getToolcacheDirectory( - getCanonicalToolcacheVersion( - source.cliVersion, - source.bundleVersion, - logger, + return new util.Success( + getToolcacheDirectory( + getCanonicalToolcacheVersion( + source.cliVersion, + source.bundleVersion, + logger, + ), ), ); }