Files
setup-uv/src/version/resolve.ts
T
Kevin StillhammerandAmp 36300bab3f 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>
2026-12-02 11:40:15 +01:00

255 lines
7.1 KiB
TypeScript

import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import * as pep440 from "@renovatebot/pep440";
import * as semver from "semver";
import { getLatestKnownVersion } from "../download/checksum/known-version";
import {
getAllVersions,
getFirstMatchingVersion,
getFirstVersion,
} from "../download/manifest";
import type { ManifestSource, ResolutionStrategy } from "../utils/inputs";
import * as log from "../utils/logging";
import {
type ParsedVersionSpecifier,
parseVersionSpecifier,
} from "./specifier";
import type { ResolveUvVersionOptions } from "./types";
import { resolveVersionRequest } from "./version-request-resolver";
interface ConcreteVersionResolutionContext {
source: ManifestSource;
parsedSpecifier: ParsedVersionSpecifier;
resolutionStrategy: ResolutionStrategy;
}
interface ConcreteVersionResolver {
resolve(
context: ConcreteVersionResolutionContext,
): Promise<string | undefined>;
}
class ExactVersionResolver implements ConcreteVersionResolver {
async resolve(
context: ConcreteVersionResolutionContext,
): Promise<string | undefined> {
if (context.parsedSpecifier.kind !== "exact") {
return undefined;
}
core.debug(
`Version ${context.parsedSpecifier.normalized} is an explicit version.`,
);
return context.parsedSpecifier.normalized;
}
}
class LatestVersionResolver implements ConcreteVersionResolver {
async resolve(
context: ConcreteVersionResolutionContext,
): Promise<string | undefined> {
const shouldUseLatestVersion =
context.parsedSpecifier.kind === "latest" ||
(context.parsedSpecifier.kind === "range" &&
context.parsedSpecifier.isSimpleMinimumVersionSpecifier &&
context.resolutionStrategy === "highest");
if (!shouldUseLatestVersion) {
return undefined;
}
if (
context.parsedSpecifier.kind === "range" &&
context.parsedSpecifier.isSimpleMinimumVersionSpecifier
) {
log.info("Found minimum version specifier, using latest version");
}
const latestVersion = await getFirstVersion(context.source);
if (
context.parsedSpecifier.kind === "range" &&
context.parsedSpecifier.isSimpleMinimumVersionSpecifier &&
!pep440.satisfies(latestVersion, context.parsedSpecifier.raw)
) {
throw new Error(`No version found for ${context.parsedSpecifier.raw}`);
}
return latestVersion;
}
}
class RangeVersionResolver implements ConcreteVersionResolver {
async resolve(
context: ConcreteVersionResolutionContext,
): Promise<string | undefined> {
if (context.parsedSpecifier.kind !== "range") {
return undefined;
}
let resolvedVersion: string | undefined;
if (
context.resolutionStrategy === "highest" &&
context.source.newestFirst
) {
resolvedVersion = await findHighestSatisfyingVersion(
context.parsedSpecifier.normalized,
context.source,
);
} else {
const availableVersions = await getAllVersions(context.source);
core.debug(`Available versions: ${availableVersions}`);
resolvedVersion =
context.resolutionStrategy === "lowest"
? minSatisfying(availableVersions, context.parsedSpecifier.normalized)
: maxSatisfying(
availableVersions,
context.parsedSpecifier.normalized,
);
}
if (resolvedVersion === undefined) {
throw new Error(`No version found for ${context.parsedSpecifier.raw}`);
}
return resolvedVersion;
}
}
const CONCRETE_VERSION_RESOLVERS: ConcreteVersionResolver[] = [
new ExactVersionResolver(),
new LatestVersionResolver(),
new RangeVersionResolver(),
];
export async function resolveUvVersion(
options: ResolveUvVersionOptions,
source: ManifestSource,
): Promise<string> {
const request = resolveVersionRequest(options);
const resolutionStrategy = options.resolutionStrategy ?? "highest";
const version = await resolveVersion(
request.specifier,
source,
resolutionStrategy,
);
return version;
}
export async function resolveVersion(
versionInput: string,
source: ManifestSource,
resolutionStrategy: ResolutionStrategy = "highest",
): Promise<string> {
core.debug(`Resolving version: ${versionInput}`);
if (versionInput.trim() === "latest-known") {
return getLatestKnownVersion();
}
const context: ConcreteVersionResolutionContext = {
parsedSpecifier: parseVersionSpecifier(versionInput),
resolutionStrategy,
source,
};
for (const resolver of CONCRETE_VERSION_RESOLVERS) {
const version = await resolver.resolve(context);
if (version !== undefined) {
return version;
}
}
throw new Error(`No version found for ${versionInput}`);
}
async function findHighestSatisfyingVersion(
versionSpecifier: string,
source: ManifestSource,
): Promise<string | undefined> {
// The built-in manifest sources list uv releases in descending order.
const semverRange = isPep440Specifier(versionSpecifier)
? null
: semver.validRange(versionSpecifier);
if (semverRange !== null) {
const semverMatch = await getFirstMatchingVersion(
(version) => semver.satisfies(version, semverRange),
source,
);
if (semverMatch !== undefined) {
core.debug(
`Found a version that satisfies the semver range: ${semverMatch}`,
);
return semverMatch;
}
}
const pep440Match = await getFirstMatchingVersion(
(version) => pep440.satisfies(version, versionSpecifier),
source,
);
if (pep440Match !== undefined) {
core.debug(
`Found a version that satisfies the pep440 specifier: ${pep440Match}`,
);
}
return pep440Match;
}
/**
* Specifiers that are valid PEP 440 must be evaluated as PEP 440. semver
* accepts several of them with different semantics, e.g. `~=0.5` as `<0.6.0`
* instead of `<1.0`, `<=0.5` as `<0.6.0` instead of `<=0.5.0` and `>0.5` as
* `>=0.6.0` instead of `>0.5.0`.
*/
function isPep440Specifier(versionSpecifier: string): boolean {
return pep440.validRange(versionSpecifier);
}
function maxSatisfying(
versions: string[],
version: string,
): string | undefined {
const maxSemver = isPep440Specifier(version)
? ""
: tc.evaluateVersions(versions, version);
if (maxSemver !== "") {
core.debug(`Found a version that satisfies the semver range: ${maxSemver}`);
return maxSemver;
}
const maxPep440 = pep440.maxSatisfying(versions, version);
if (maxPep440 !== null) {
core.debug(
`Found a version that satisfies the pep440 specifier: ${maxPep440}`,
);
return maxPep440;
}
return undefined;
}
function minSatisfying(
versions: string[],
version: string,
): string | undefined {
const minSemver = isPep440Specifier(version)
? null
: semver.minSatisfying(versions, version);
if (minSemver !== null) {
core.debug(`Found a version that satisfies the semver range: ${minSemver}`);
return minSemver;
}
const minPep440 = pep440.minSatisfying(versions, version);
if (minPep440 !== null) {
core.debug(
`Found a version that satisfies the pep440 specifier: ${minPep440}`,
);
return minPep440;
}
return undefined;
}