Skip to content

Commit db99327

Browse files
henrymercerCopilot
andcommitted
Document how the CodeQL tools are versioned in the toolcache
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 7f964a9 commit db99327

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

‎src/setup-codeql.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ export function tryGetTagNameFromUrl(
191191
return match[1];
192192
}
193193

194+
/**
195+
* Converts a bundle version to a semantic version, for example to use in the toolcache. Semantic
196+
* versions are normalized with `semver.clean`, which drops a leading `v` and any build metadata.
197+
* Anything else, such as a date, becomes a prerelease of `0.0.0`. Throws if the result isn't a
198+
* valid semantic version.
199+
*/
194200
export function convertToSemVer(version: string, logger: Logger): string {
195201
if (!semver.valid(version)) {
196202
logger.debug(
@@ -629,7 +635,8 @@ export async function getCodeQLSource(
629635
// If we find the specified CLI version, we always use that.
630636
codeqlFolder = toolcache.find("CodeQL", cliVersion);
631637

632-
// Fall back to matching `x.y.z-<tagName>`.
638+
// Fall back to a single `x.y.z-*` entry, since older toolcaches store bundles as
639+
// `x.y.z-<bundle version>`.
633640
if (!codeqlFolder) {
634641
logger.debug(
635642
"Didn't find a version of the CodeQL tools in the toolcache with a version number " +
@@ -641,8 +648,6 @@ export async function getCodeQLSource(
641648
allVersions,
642649
)}.`,
643650
);
644-
// If there is exactly one version of the CodeQL tools in the toolcache, and that version is
645-
// the form `x.y.z-<tagName>`, then use it.
646651
const candidateVersions = allVersions.filter((version) =>
647652
version.startsWith(`${cliVersion}-`),
648653
);
@@ -878,6 +883,9 @@ export const downloadCodeQL = async function (
878883

879884
/**
880885
* Returns the canonical toolcache directory, or the reason the bundle cannot be cached.
886+
*
887+
* The toolcache is keyed by version, so we don't cache bundles that would give a later request for
888+
* the same version the wrong tools, such as per-language bundles, which lack the other languages.
881889
*/
882890
function getToolcacheDestination(
883891
{ logger }: ActionState<["Logger"]>,
@@ -973,11 +981,11 @@ function getCanonicalToolcacheVersion(
973981
bundleVersion: string,
974982
logger: Logger,
975983
): string {
976-
// If the CLI version is a pre-release or contains build metadata, then cache the
977-
// bundle as `0.0.0-<bundleVersion>` to avoid the bundle being interpreted as containing a stable
978-
// CLI release. In principle, it should be enough to just check that the CLI version isn't a
979-
// pre-release, but the version numbers of CodeQL nightlies have the format `x.y.z+<timestamp>`,
980-
// and we don't want these nightlies to override stable CLI versions in the toolcache.
984+
// If the CLI version is unknown, as for nightlies, which are tagged by date, or isn't a plain
985+
// `x.y.z`, for example a prerelease, cache the bundle under its bundle version, such as
986+
// `0.0.0-<date>` or `x.y.z-rc.1`, so that it isn't cached as a stable release. However,
987+
// `convertToSemVer` drops build metadata, so a bundle URL tagged `codeql-bundle-vX.Y.Z+<build>`
988+
// is still cached as `X.Y.Z`.
981989
if (!cliVersion?.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)) {
982990
return convertToSemVer(bundleVersion, logger);
983991
}

0 commit comments

Comments
 (0)