Skip to content

Commit 80fbe01

Browse files
soujasKclaude
andauthored
fix: resolve ~= specifiers with PEP 440 semantics (#1111)
## Summary `~=` is the PEP 440 compatible release operator, but the resolver tries `semver.validRange()` first, and semver accepts `~=` as a tilde range: | specifier | semver | PEP 440 | |---|---|---| | `~=0.9` | `>=0.9.0 <0.10.0` | `>=0.9, ==0.*` | So with the default `highest` strategy, `version: "~=0.9"` (or `required-version = "~=0.9"` picked up through `version-file`) installs the highest 0.9.x instead of the highest 0.x. The same happens with `manifest-file`, where `tc.evaluateVersions` is semver-based. This skips the semver evaluation for specifiers containing `~=`, so they are only evaluated with `@renovatebot/pep440`. No semver range legitimately uses `~=`. Notes: - Only a bare two-segment `~=X.Y` resolved incorrectly. `~=X.Y.Z` gives the same result in both, and compound specifiers like `~=0.9, !=0.9.3` were already rejected by semver and fell through to PEP 440. - `minSatisfying` gets the same check for consistency. The lower bound is the same under both interpretations, so `lowest` didn't produce wrong results before. - A single-segment `~=1` is invalid PEP 440. semver used to resolve it to 1.x; it now fails with `No version found for ~=1`, same as other invalid specifiers. ## Test plan - Added tests for `~=0.9` with the default manifest and with `manifest-file`. Both returned `0.9.26` instead of `0.10.2` before the change. - `npm run all` on Ubuntu (WSL) with Node 24.19.0: build, Biome (no fixes), packaging, and 161 tests across 15 suites pass. The rebuilt `dist/setup/index.cjs` only differs in the changed lines. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 36b7b57 commit 80fbe01

3 files changed

Lines changed: 44 additions & 6 deletions

File tree

‎__tests__/download/download-version.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ describe("download-version", () => {
146146
expect(mockGetAllVersions).not.toHaveBeenCalled();
147147
});
148148

149+
it("resolves ~= with PEP 440 compatible release semantics", async () => {
150+
mockGetFirstMatchingVersion.mockImplementation(
151+
(predicate: (version: string) => boolean) =>
152+
["1.0.0", "0.10.2", "0.9.26"].find(predicate),
153+
);
154+
155+
const version = await resolveVersion("~=0.9", undefined);
156+
157+
expect(version).toBe("0.10.2");
158+
});
159+
160+
it("resolves ~= with PEP 440 compatible release semantics from manifest-file", async () => {
161+
mockGetAllVersions.mockResolvedValue(["1.0.0", "0.10.2", "0.9.26"]);
162+
163+
const version = await resolveVersion(
164+
"~=0.9",
165+
"https://example.com/custom.ndjson",
166+
);
167+
168+
expect(version).toBe("0.10.2");
169+
});
170+
149171
it("treats == exact pins as explicit versions", async () => {
150172
const version = await resolveVersion("==0.9.26", undefined);
151173

‎dist/setup/index.cjs‎

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/version/resolve.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ async function findHighestSatisfyingVersion(
169169
): Promise<string | undefined> {
170170
// This fast path assumes uv releases are semver-monotonic: newer manifest
171171
// entries always have higher versions, with no lower-version backports.
172-
const semverRange = semver.validRange(versionSpecifier);
172+
const semverRange = isCompatibleReleaseSpecifier(versionSpecifier)
173+
? null
174+
: semver.validRange(versionSpecifier);
173175
if (semverRange !== null) {
174176
const semverMatch = await getFirstMatchingVersion((version) =>
175177
semver.satisfies(version, semverRange),
@@ -193,11 +195,20 @@ async function findHighestSatisfyingVersion(
193195
return pep440Match;
194196
}
195197

198+
// `~=` is the PEP 440 compatible release operator. semver accepts it as a
199+
// tilde range with a narrower upper bound (`~=0.5` as `<0.6.0` instead of
200+
// `<1.0`), so it must only be evaluated as PEP 440.
201+
function isCompatibleReleaseSpecifier(versionSpecifier: string): boolean {
202+
return versionSpecifier.includes("~=");
203+
}
204+
196205
function maxSatisfying(
197206
versions: string[],
198207
version: string,
199208
): string | undefined {
200-
const maxSemver = tc.evaluateVersions(versions, version);
209+
const maxSemver = isCompatibleReleaseSpecifier(version)
210+
? ""
211+
: tc.evaluateVersions(versions, version);
201212
if (maxSemver !== "") {
202213
core.debug(`Found a version that satisfies the semver range: ${maxSemver}`);
203214
return maxSemver;
@@ -218,7 +229,9 @@ function minSatisfying(
218229
versions: string[],
219230
version: string,
220231
): string | undefined {
221-
const minSemver = semver.minSatisfying(versions, version);
232+
const minSemver = isCompatibleReleaseSpecifier(version)
233+
? null
234+
: semver.minSatisfying(versions, version);
222235
if (minSemver !== null) {
223236
core.debug(`Found a version that satisfies the semver range: ${minSemver}`);
224237
return minSemver;

0 commit comments

Comments
 (0)