@@ -629,76 +629,12 @@ export async function getCodeQLSource(
629629 `URL: ${ url ?? "unspecified" } .` ,
630630 ) ;
631631
632- let codeqlFolder : string | undefined ;
633-
634- if ( cliVersion ) {
635- // If we find the specified CLI version, we always use that.
636- codeqlFolder = toolcache . find ( "CodeQL" , cliVersion ) ;
637-
638- // Fall back to a single `x.y.z-*` entry, since older toolcaches store bundles as
639- // `x.y.z-<bundle version>`.
640- if ( ! codeqlFolder ) {
641- logger . debug (
642- "Didn't find a version of the CodeQL tools in the toolcache with a version number " +
643- `exactly matching ${ cliVersion } .` ,
644- ) ;
645- const allVersions = toolcache . findAllVersions ( "CodeQL" ) ;
646- logger . debug (
647- `Found the following versions of the CodeQL tools in the toolcache: ${ JSON . stringify (
648- allVersions ,
649- ) } .`,
650- ) ;
651- const candidateVersions = allVersions . filter ( ( version ) =>
652- version . startsWith ( `${ cliVersion } -` ) ,
653- ) ;
654- if ( candidateVersions . length === 1 ) {
655- logger . debug (
656- `Exactly one version of the CodeQL tools starting with ${ cliVersion } found in the ` +
657- "toolcache, using that." ,
658- ) ;
659- codeqlFolder = toolcache . find ( "CodeQL" , candidateVersions [ 0 ] ) ;
660- } else if ( candidateVersions . length === 0 ) {
661- logger . debug (
662- `Didn't find any versions of the CodeQL tools starting with ${ cliVersion } ` +
663- `in the toolcache. Trying next fallback method.` ,
664- ) ;
665- } else {
666- logger . warning (
667- `Found ${ candidateVersions . length } versions of the CodeQL tools starting with ` +
668- `${ cliVersion } in the toolcache, but at most one was expected.` ,
669- ) ;
670- logger . debug ( "Trying next fallback method." ) ;
671- }
672- }
673- }
674-
675- // Fall back to matching `0.0.0-<bundleVersion>`.
676- if ( ! codeqlFolder && tagName ) {
677- const fallbackVersion = await tryGetFallbackToolcacheVersion (
678- cliVersion ,
679- tagName ,
680- logger ,
681- ) ;
682- if ( fallbackVersion ) {
683- codeqlFolder = toolcache . find ( "CodeQL" , fallbackVersion ) ;
684- } else {
685- logger . debug (
686- "Could not determine a fallback toolcache version number for CodeQL tools version " +
687- `${ humanReadableVersion } .` ,
688- ) ;
689- }
690- }
691-
692- if ( codeqlFolder ) {
693- logger . info (
694- `Found CodeQL tools version ${ humanReadableVersion } in the toolcache.` ,
695- ) ;
696- } else {
697- logger . info (
698- `Did not find CodeQL tools version ${ humanReadableVersion } in the toolcache.` ,
699- ) ;
700- }
701-
632+ const codeqlFolder = await findCodeQLInToolcache (
633+ cliVersion ,
634+ tagName ,
635+ humanReadableVersion ,
636+ logger ,
637+ ) ;
702638 if ( codeqlFolder ) {
703639 if ( cliVersion ) {
704640 logger . info (
@@ -790,6 +726,90 @@ export async function getCodeQLSource(
790726 } ;
791727}
792728
729+ /**
730+ * Looks for the requested version of the CodeQL tools in the toolcache, allowing for the different
731+ * version numbers that toolcaches may use for the same bundle. We try the exact CLI version, then a
732+ * single `x.y.z-*` entry for that version, then `0.0.0-<bundle version>`. The `x.y.z-*` entry can
733+ * be any prerelease of the CLI version.
734+ */
735+ async function findCodeQLInToolcache (
736+ cliVersion : string | undefined ,
737+ tagName : string | undefined ,
738+ humanReadableVersion : string ,
739+ logger : Logger ,
740+ ) : Promise < string | undefined > {
741+ let codeqlFolder : string | undefined ;
742+
743+ if ( cliVersion ) {
744+ // If we find the specified CLI version, we always use that.
745+ codeqlFolder = toolcache . find ( "CodeQL" , cliVersion ) ;
746+
747+ // Fall back to a single `x.y.z-*` entry, since older toolcaches store bundles as
748+ // `x.y.z-<bundle version>`.
749+ if ( ! codeqlFolder ) {
750+ logger . debug (
751+ "Didn't find a version of the CodeQL tools in the toolcache with a version number " +
752+ `exactly matching ${ cliVersion } .` ,
753+ ) ;
754+ const allVersions = toolcache . findAllVersions ( "CodeQL" ) ;
755+ logger . debug (
756+ `Found the following versions of the CodeQL tools in the toolcache: ${ JSON . stringify (
757+ allVersions ,
758+ ) } .`,
759+ ) ;
760+ const candidateVersions = allVersions . filter ( ( version ) =>
761+ version . startsWith ( `${ cliVersion } -` ) ,
762+ ) ;
763+ if ( candidateVersions . length === 1 ) {
764+ logger . debug (
765+ `Exactly one version of the CodeQL tools starting with ${ cliVersion } found in the ` +
766+ "toolcache, using that." ,
767+ ) ;
768+ codeqlFolder = toolcache . find ( "CodeQL" , candidateVersions [ 0 ] ) ;
769+ } else if ( candidateVersions . length === 0 ) {
770+ logger . debug (
771+ `Didn't find any versions of the CodeQL tools starting with ${ cliVersion } ` +
772+ `in the toolcache. Trying next fallback method.` ,
773+ ) ;
774+ } else {
775+ logger . warning (
776+ `Found ${ candidateVersions . length } versions of the CodeQL tools starting with ` +
777+ `${ cliVersion } in the toolcache, but at most one was expected.` ,
778+ ) ;
779+ logger . debug ( "Trying next fallback method." ) ;
780+ }
781+ }
782+ }
783+
784+ // Fall back to matching `0.0.0-<bundleVersion>`.
785+ if ( ! codeqlFolder && tagName ) {
786+ const fallbackVersion = await tryGetFallbackToolcacheVersion (
787+ cliVersion ,
788+ tagName ,
789+ logger ,
790+ ) ;
791+ if ( fallbackVersion ) {
792+ codeqlFolder = toolcache . find ( "CodeQL" , fallbackVersion ) ;
793+ } else {
794+ logger . debug (
795+ "Could not determine a fallback toolcache version number for CodeQL tools version " +
796+ `${ humanReadableVersion } .` ,
797+ ) ;
798+ }
799+ }
800+
801+ if ( codeqlFolder ) {
802+ logger . info (
803+ `Found CodeQL tools version ${ humanReadableVersion } in the toolcache.` ,
804+ ) ;
805+ } else {
806+ logger . info (
807+ `Did not find CodeQL tools version ${ humanReadableVersion } in the toolcache.` ,
808+ ) ;
809+ }
810+ return codeqlFolder ;
811+ }
812+
793813/**
794814 * Gets a fallback version number to use when looking for CodeQL in the toolcache if we didn't find
795815 * the `x.y.z` version. This is to support old versions of the toolcache.
0 commit comments