Respect the mirror setting when selecting the version manifest (#1164)

Select the manifest source from the action inputs and keep version
resolution separate from archive downloads. Fetch the built-in manifest
directly from GitHub when the Astral mirror is disabled.

Fixes: #1140

Based on the work of @Rudra-G-23 in
https://github.com/astral-sh/setup-uv/pull/1152

Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Kevin StillhammerandAmp authored and GitHub committed 2026-12-02 11:40:15 +01:00
1 parent 521745f16c
commit 36300bab3f
19 files changed
+1637 -1647

No files matched your search

+3 -44
View File
@@ -6,15 +6,11 @@ import {
ASTRAL_MIRROR_PREFIX,
GITHUB_RELEASES_PREFIX,
TOOL_CACHE_NAME,
VERSIONS_MANIFEST_URL,
} from "../utils/constants";
import * as log from "../utils/logging";
import type { Architecture, Platform } from "../utils/platforms";
import { validateChecksum } from "./checksum/checksum";
import { KNOWN_CHECKSUMS } from "./checksum/known-checksums";
import { getArtifact } from "./manifest";
export { resolveVersion } from "../version/resolve";
export function getIntrinsicChecksum(
key: string,
@@ -52,27 +48,12 @@ export async function downloadVersion(
platform: Platform,
arch: Architecture,
version: string,
artifact: { downloadUrl: string; checksum: string },
checksum: string | undefined,
githubToken: string,
manifestUrl?: string,
downloadFromAstralMirror = true,
toolCacheName = TOOL_CACHE_NAME,
): Promise<{ version: string; cachedToolDir: string }> {
const artifact = await getArtifact(version, arch, platform, manifestUrl);
if (!artifact) {
throw new Error(
getMissingArtifactMessage(version, arch, platform, manifestUrl),
);
}
// Custom manifests are explicitly selected by the user, so their checksum
// takes precedence over the built-in table. For the default manifest, pass
// its checksum as a fallback after user input and KNOWN_CHECKSUMS.
const resolvedChecksum =
manifestUrl === undefined
? checksum
: resolveChecksum(checksum, artifact.checksum);
const manifestChecksum = artifact.checksum;
const mirrorUrl = downloadFromAstralMirror
@@ -87,7 +68,7 @@ export async function downloadVersion(
platform,
arch,
version,
resolvedChecksum,
checksum,
manifestChecksum,
githubTokenForUrl(downloadUrl, githubToken),
toolCacheName,
@@ -107,7 +88,7 @@ export async function downloadVersion(
platform,
arch,
version,
resolvedChecksum,
checksum,
manifestChecksum,
githubTokenForUrl(artifact.downloadUrl, githubToken),
toolCacheName,
@@ -191,28 +172,6 @@ async function downloadArtifact(
return { cachedToolDir, version };
}
function getMissingArtifactMessage(
version: string,
arch: Architecture,
platform: Platform,
manifestUrl?: string,
): string {
if (manifestUrl === undefined) {
return `Could not find artifact for version ${version}, arch ${arch}, platform ${platform} in ${VERSIONS_MANIFEST_URL} .`;
}
return `manifest-file does not contain version ${version}, arch ${arch}, platform ${platform}.`;
}
function resolveChecksum(
checksum: string | undefined,
manifestChecksum: string,
): string {
return checksum !== undefined && checksum !== ""
? checksum
: manifestChecksum;
}
function getExtension(platform: Platform): string {
return platform === "pc-windows-msvc" ? ".zip" : ".tar.gz";
}
+48 -72
View File
@@ -1,11 +1,8 @@
import { createInterface } from "node:readline";
import { Readable } from "node:stream";
import * as core from "@actions/core";
import {
GITHUB_VERSIONS_MANIFEST_URL,
VERSIONS_MANIFEST_URL,
} from "../utils/constants";
import { fetch } from "../utils/fetch";
import type { ManifestSource } from "../utils/inputs";
import * as log from "../utils/logging";
import { selectDefaultVariant } from "./variant-selection";
@@ -33,22 +30,23 @@ interface CachedManifest {
versions: ManifestVersion[];
}
const cachedManifestData = new Map<string, CachedManifest>();
// Share cached records for the same source object without keeping that object alive.
let cachedManifestData = new WeakMap<ManifestSource, CachedManifest>();
export const MANIFEST_FETCH_ATTEMPTS = 3;
export async function fetchManifest(
manifestUrl: string = VERSIONS_MANIFEST_URL,
source: ManifestSource,
): Promise<ManifestVersion[]> {
const cachedManifest = cachedManifestData.get(manifestUrl);
const cachedManifest = cachedManifestData.get(source);
if (cachedManifest?.complete === true) {
core.debug(`Using cached manifest data from ${manifestUrl}`);
core.debug(`Using cached manifest data from ${source.url}`);
return cachedManifest.versions;
}
const response = await fetchManifestResponse(manifestUrl);
const response = await fetchManifestResponse(source);
const body = await response.text();
const versions = parseManifest(body, manifestUrl);
cachedManifestData.set(manifestUrl, { complete: true, versions });
const versions = parseManifest(body, source.url);
cachedManifestData.set(source, { complete: true, versions });
return versions;
}
@@ -81,39 +79,32 @@ export function parseManifest(
return versions;
}
export async function getLatestVersion(
manifestUrl: string = VERSIONS_MANIFEST_URL,
): Promise<string> {
const latestVersion =
manifestUrl === VERSIONS_MANIFEST_URL
? (await findManifestVersion(() => true))?.version
: (await fetchManifest(manifestUrl))[0]?.version;
export async function getFirstVersion(source: ManifestSource): Promise<string> {
const version = (await findManifestVersion(source, () => true))?.version;
if (latestVersion === undefined) {
if (version === undefined) {
throw new Error("No versions found in manifest data");
}
core.debug(`Latest version from manifest: ${latestVersion}`);
return latestVersion;
return version;
}
// The default manifest is guaranteed to be ordered newest-first:
// https://github.com/astral-sh/versions#format
export async function getFirstMatchingVersion(
predicate: (version: string) => boolean,
source: ManifestSource,
): Promise<string | undefined> {
return (
await findManifestVersion((versionData) => predicate(versionData.version))
await findManifestVersion(source, (versionData) =>
predicate(versionData.version),
)
)?.version;
}
export async function getAllVersions(
manifestUrl: string = VERSIONS_MANIFEST_URL,
source: ManifestSource,
): Promise<string[]> {
log.info(
`Getting available versions from ${manifestSource(manifestUrl)} ...`,
);
const versions = await fetchManifest(manifestUrl);
log.info(`Getting available versions from ${source.url} ...`);
const versions = await fetchManifest(source);
return versions.map((versionData) => versionData.version);
}
@@ -121,16 +112,14 @@ export async function getArtifact(
version: string,
arch: string,
platform: string,
manifestUrl: string = VERSIONS_MANIFEST_URL,
source: ManifestSource,
): Promise<ArtifactResult | undefined> {
const versionData =
manifestUrl === VERSIONS_MANIFEST_URL
? await findManifestVersion((candidate) => candidate.version === version)
: (await fetchManifest(manifestUrl)).find(
(candidate) => candidate.version === version,
);
const versionData = await findManifestVersion(
source,
(candidate) => candidate.version === version,
);
if (!versionData) {
core.debug(`Version ${version} not found in manifest ${manifestUrl}`);
core.debug(`Version ${version} not found in manifest ${source.url}`);
return undefined;
}
@@ -160,26 +149,21 @@ export async function getArtifact(
};
}
export function clearManifestCache(manifestUrl?: string): void {
if (manifestUrl === undefined) {
cachedManifestData.clear();
return;
}
cachedManifestData.delete(manifestUrl);
export function clearManifestCache(): void {
cachedManifestData = new WeakMap();
}
async function fetchManifestResponse(manifestUrl: string) {
async function fetchManifestResponse(source: ManifestSource) {
try {
return await fetchManifestResponseFromUrl(manifestUrl);
return await fetchManifestResponseFromUrl(source.url);
} catch (error) {
if (manifestUrl !== VERSIONS_MANIFEST_URL) {
if (source.fallbackUrl === undefined) {
throw error;
}
log.info(
`Failed to fetch manifest from Astral mirror, falling back to GitHub: ${(error as Error).message}`,
`Failed to fetch manifest from ${source.url}, falling back to ${source.fallbackUrl}: ${(error as Error).message}`,
);
return await fetchManifestResponseFromUrl(GITHUB_VERSIONS_MANIFEST_URL);
return await fetchManifestResponseFromUrl(source.fallbackUrl);
}
}
@@ -214,21 +198,25 @@ async function fetchManifestResponseFromUrl(manifestUrl: string) {
}
async function findManifestVersion(
source: ManifestSource,
predicate: (versionData: ManifestVersion) => boolean,
): Promise<ManifestVersion | undefined> {
const cachedManifest = cachedManifestData.get(VERSIONS_MANIFEST_URL);
// The built-in manifests are ordered newest-first:
// https://github.com/astral-sh/versions#format
if (!source.newestFirst) {
return (await fetchManifest(source)).find(predicate);
}
const cachedManifest = cachedManifestData.get(source);
const cachedVersion = cachedManifest?.versions.find(predicate);
if (cachedVersion !== undefined || cachedManifest?.complete === true) {
return cachedVersion;
}
const response = await fetchManifestResponse(VERSIONS_MANIFEST_URL);
const response = await fetchManifestResponse(source);
if (response.body === null) {
const versions = parseManifest(
await response.text(),
VERSIONS_MANIFEST_URL,
);
cachedManifestData.set(VERSIONS_MANIFEST_URL, {
const versions = parseManifest(await response.text(), source.url);
cachedManifestData.set(source, {
complete: true,
versions,
});
@@ -251,14 +239,10 @@ async function findManifestVersion(
}
if (versions.length === 0) {
rejectLegacyManifest(record, VERSIONS_MANIFEST_URL);
rejectLegacyManifest(record, source.url);
}
const versionData = parseManifestRecord(
record,
VERSIONS_MANIFEST_URL,
lineNumber,
);
const versionData = parseManifestRecord(record, source.url, lineNumber);
versions.push(versionData);
if (predicate(versionData)) {
matchedVersion = versionData;
@@ -275,21 +259,13 @@ async function findManifestVersion(
}
if (versions.length === 0) {
throw new Error(`Manifest at ${VERSIONS_MANIFEST_URL} is empty.`);
throw new Error(`Manifest at ${source.url} is empty.`);
}
cachedManifestData.set(VERSIONS_MANIFEST_URL, { complete, versions });
cachedManifestData.set(source, { complete, versions });
return matchedVersion;
}
function manifestSource(manifestUrl: string): string {
if (manifestUrl === VERSIONS_MANIFEST_URL) {
return VERSIONS_MANIFEST_URL;
}
return `manifest-file ${manifestUrl}`;
}
function parseManifestRecord(
record: string,
sourceDescription: string,