diff --git a/lib/entry-points.js b/lib/entry-points.js index f78a951f0..c703349ed 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -152459,62 +152459,12 @@ async function getCodeQLSource(toolsInput, defaultCliVersion, rawLanguages, useO logger.debug( `Attempting to obtain CodeQL tools. CLI version: ${cliVersion2 ?? "unknown"}, bundle tag name: ${tagName ?? "unknown"}, URL: ${url2 ?? "unspecified"}.` ); - let codeqlFolder; - if (cliVersion2) { - codeqlFolder = toolcache3.find("CodeQL", cliVersion2); - if (!codeqlFolder) { - logger.debug( - `Didn't find a version of the CodeQL tools in the toolcache with a version number exactly matching ${cliVersion2}.` - ); - const allVersions = toolcache3.findAllVersions("CodeQL"); - logger.debug( - `Found the following versions of the CodeQL tools in the toolcache: ${JSON.stringify( - allVersions - )}.` - ); - const candidateVersions = allVersions.filter( - (version) => version.startsWith(`${cliVersion2}-`) - ); - if (candidateVersions.length === 1) { - logger.debug( - `Exactly one version of the CodeQL tools starting with ${cliVersion2} found in the toolcache, using that.` - ); - codeqlFolder = toolcache3.find("CodeQL", candidateVersions[0]); - } else if (candidateVersions.length === 0) { - logger.debug( - `Didn't find any versions of the CodeQL tools starting with ${cliVersion2} in the toolcache. Trying next fallback method.` - ); - } else { - logger.warning( - `Found ${candidateVersions.length} versions of the CodeQL tools starting with ${cliVersion2} in the toolcache, but at most one was expected.` - ); - logger.debug("Trying next fallback method."); - } - } - } - if (!codeqlFolder && tagName) { - const fallbackVersion = await tryGetFallbackToolcacheVersion( - cliVersion2, - tagName, - logger - ); - if (fallbackVersion) { - codeqlFolder = toolcache3.find("CodeQL", fallbackVersion); - } else { - logger.debug( - `Could not determine a fallback toolcache version number for CodeQL tools version ${humanReadableVersion}.` - ); - } - } - if (codeqlFolder) { - logger.info( - `Found CodeQL tools version ${humanReadableVersion} in the toolcache.` - ); - } else { - logger.info( - `Did not find CodeQL tools version ${humanReadableVersion} in the toolcache.` - ); - } + const codeqlFolder = await findCodeQLInToolcache( + cliVersion2, + tagName, + humanReadableVersion, + logger + ); if (codeqlFolder) { if (cliVersion2) { logger.info( @@ -152589,6 +152539,65 @@ async function getCodeQLSource(toolsInput, defaultCliVersion, rawLanguages, useO toolsVersion: resolvedVersion ?? "unknown" }; } +async function findCodeQLInToolcache(cliVersion2, tagName, humanReadableVersion, logger) { + let codeqlFolder; + if (cliVersion2) { + codeqlFolder = toolcache3.find("CodeQL", cliVersion2); + if (!codeqlFolder) { + logger.debug( + `Didn't find a version of the CodeQL tools in the toolcache with a version number exactly matching ${cliVersion2}.` + ); + const allVersions = toolcache3.findAllVersions("CodeQL"); + logger.debug( + `Found the following versions of the CodeQL tools in the toolcache: ${JSON.stringify( + allVersions + )}.` + ); + const candidateVersions = allVersions.filter( + (version) => version.startsWith(`${cliVersion2}-`) + ); + if (candidateVersions.length === 1) { + logger.debug( + `Exactly one version of the CodeQL tools starting with ${cliVersion2} found in the toolcache, using that.` + ); + codeqlFolder = toolcache3.find("CodeQL", candidateVersions[0]); + } else if (candidateVersions.length === 0) { + logger.debug( + `Didn't find any versions of the CodeQL tools starting with ${cliVersion2} in the toolcache. Trying next fallback method.` + ); + } else { + logger.warning( + `Found ${candidateVersions.length} versions of the CodeQL tools starting with ${cliVersion2} in the toolcache, but at most one was expected.` + ); + logger.debug("Trying next fallback method."); + } + } + } + if (!codeqlFolder && tagName) { + const fallbackVersion = await tryGetFallbackToolcacheVersion( + cliVersion2, + tagName, + logger + ); + if (fallbackVersion) { + codeqlFolder = toolcache3.find("CodeQL", fallbackVersion); + } else { + logger.debug( + `Could not determine a fallback toolcache version number for CodeQL tools version ${humanReadableVersion}.` + ); + } + } + if (codeqlFolder) { + logger.info( + `Found CodeQL tools version ${humanReadableVersion} in the toolcache.` + ); + } else { + logger.info( + `Did not find CodeQL tools version ${humanReadableVersion} in the toolcache.` + ); + } + return codeqlFolder; +} async function tryGetFallbackToolcacheVersion(cliVersion2, tagName, logger) { const bundleVersion2 = tryGetBundleVersionFromTagName(tagName, logger); if (bundleVersion2 === void 0) { diff --git a/src/setup-codeql.ts b/src/setup-codeql.ts index 3be9e5118..61b6e500a 100644 --- a/src/setup-codeql.ts +++ b/src/setup-codeql.ts @@ -629,76 +629,12 @@ export async function getCodeQLSource( `URL: ${url ?? "unspecified"}.`, ); - let codeqlFolder: string | undefined; - - if (cliVersion) { - // If we find the specified CLI version, we always use that. - codeqlFolder = toolcache.find("CodeQL", cliVersion); - - // Fall back to a single `x.y.z-*` entry, since older toolcaches store bundles as - // `x.y.z-`. - if (!codeqlFolder) { - logger.debug( - "Didn't find a version of the CodeQL tools in the toolcache with a version number " + - `exactly matching ${cliVersion}.`, - ); - const allVersions = toolcache.findAllVersions("CodeQL"); - logger.debug( - `Found the following versions of the CodeQL tools in the toolcache: ${JSON.stringify( - allVersions, - )}.`, - ); - const candidateVersions = allVersions.filter((version) => - version.startsWith(`${cliVersion}-`), - ); - if (candidateVersions.length === 1) { - logger.debug( - `Exactly one version of the CodeQL tools starting with ${cliVersion} found in the ` + - "toolcache, using that.", - ); - codeqlFolder = toolcache.find("CodeQL", candidateVersions[0]); - } else if (candidateVersions.length === 0) { - logger.debug( - `Didn't find any versions of the CodeQL tools starting with ${cliVersion} ` + - `in the toolcache. Trying next fallback method.`, - ); - } else { - logger.warning( - `Found ${candidateVersions.length} versions of the CodeQL tools starting with ` + - `${cliVersion} in the toolcache, but at most one was expected.`, - ); - logger.debug("Trying next fallback method."); - } - } - } - - // Fall back to matching `0.0.0-`. - if (!codeqlFolder && tagName) { - const fallbackVersion = await tryGetFallbackToolcacheVersion( - cliVersion, - tagName, - logger, - ); - if (fallbackVersion) { - codeqlFolder = toolcache.find("CodeQL", fallbackVersion); - } else { - logger.debug( - "Could not determine a fallback toolcache version number for CodeQL tools version " + - `${humanReadableVersion}.`, - ); - } - } - - if (codeqlFolder) { - logger.info( - `Found CodeQL tools version ${humanReadableVersion} in the toolcache.`, - ); - } else { - logger.info( - `Did not find CodeQL tools version ${humanReadableVersion} in the toolcache.`, - ); - } - + const codeqlFolder = await findCodeQLInToolcache( + cliVersion, + tagName, + humanReadableVersion, + logger, + ); if (codeqlFolder) { if (cliVersion) { logger.info( @@ -790,6 +726,90 @@ export async function getCodeQLSource( }; } +/** + * Looks for the requested version of the CodeQL tools in the toolcache, allowing for the different + * version numbers that toolcaches may use for the same bundle. We try the exact CLI version, then a + * single `x.y.z-*` entry for that version, then `0.0.0-`. The `x.y.z-*` entry can + * be any prerelease of the CLI version. + */ +async function findCodeQLInToolcache( + cliVersion: string | undefined, + tagName: string | undefined, + humanReadableVersion: string, + logger: Logger, +): Promise { + let codeqlFolder: string | undefined; + + if (cliVersion) { + // If we find the specified CLI version, we always use that. + codeqlFolder = toolcache.find("CodeQL", cliVersion); + + // Fall back to a single `x.y.z-*` entry, since older toolcaches store bundles as + // `x.y.z-`. + if (!codeqlFolder) { + logger.debug( + "Didn't find a version of the CodeQL tools in the toolcache with a version number " + + `exactly matching ${cliVersion}.`, + ); + const allVersions = toolcache.findAllVersions("CodeQL"); + logger.debug( + `Found the following versions of the CodeQL tools in the toolcache: ${JSON.stringify( + allVersions, + )}.`, + ); + const candidateVersions = allVersions.filter((version) => + version.startsWith(`${cliVersion}-`), + ); + if (candidateVersions.length === 1) { + logger.debug( + `Exactly one version of the CodeQL tools starting with ${cliVersion} found in the ` + + "toolcache, using that.", + ); + codeqlFolder = toolcache.find("CodeQL", candidateVersions[0]); + } else if (candidateVersions.length === 0) { + logger.debug( + `Didn't find any versions of the CodeQL tools starting with ${cliVersion} ` + + `in the toolcache. Trying next fallback method.`, + ); + } else { + logger.warning( + `Found ${candidateVersions.length} versions of the CodeQL tools starting with ` + + `${cliVersion} in the toolcache, but at most one was expected.`, + ); + logger.debug("Trying next fallback method."); + } + } + } + + // Fall back to matching `0.0.0-`. + if (!codeqlFolder && tagName) { + const fallbackVersion = await tryGetFallbackToolcacheVersion( + cliVersion, + tagName, + logger, + ); + if (fallbackVersion) { + codeqlFolder = toolcache.find("CodeQL", fallbackVersion); + } else { + logger.debug( + "Could not determine a fallback toolcache version number for CodeQL tools version " + + `${humanReadableVersion}.`, + ); + } + } + + if (codeqlFolder) { + logger.info( + `Found CodeQL tools version ${humanReadableVersion} in the toolcache.`, + ); + } else { + logger.info( + `Did not find CodeQL tools version ${humanReadableVersion} in the toolcache.`, + ); + } + return codeqlFolder; +} + /** * Gets a fallback version number to use when looking for CodeQL in the toolcache if we didn't find * the `x.y.z` version. This is to support old versions of the toolcache.