Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): satisfy partial caret and hyphen semver ranges (#10184)

Cindy Zhang8b836a3fcc55
diff --git a/.changeset/cli-semver-partial-caret-hyphen.md b/.changeset/cli-semver-partial-caret-hyphen.md
new file mode 100644
index 0000000000..62f4252d01
--- /dev/null
+++ b/.changeset/cli-semver-partial-caret-hyphen.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] The CLI's semver range check reads `^0` as any 0.x.y and `^0.0` as any 0.0.y, and reads a hyphen range (`1.0.0 - 2.0.0`) as inclusive bounds, with a partial upper bound (`1.0.0 - 2`) covering its whole series as npm does. Before, `^0` and `^0.0` were locked to a single patch version and a hyphen range could never match, so `astryx doctor` and the peer-version checks reported a false mismatch for projects that declare those ranges.
+
+@thecodebasedot
diff --git a/packages/cli/foundation/env/semver.mjs b/packages/cli/foundation/env/semver.mjs
index 39c0e9f208..83c604bc3b 100644
--- a/packages/cli/foundation/env/semver.mjs
+++ b/packages/cli/foundation/env/semver.mjs
@@ -80,7 +80,10 @@ export function semverGt(a, b) {
* @returns {{major: number, minor: number, patch: number, count: number}}
*/
function parseVersionParts(value) {
- const core = String(value).trim().replace(/^[v=]+/, '').split(/[-+]/)[0];
+ const core = String(value)
+ .trim()
+ .replace(/^[v=]+/, '')
+ .split(/[-+]/)[0];
const nums = core.split('.').map(n => parseInt(n, 10));
return {
major: Number.isFinite(nums[0]) ? nums[0] : 0,
@@ -105,7 +108,8 @@ function satisfiesComparator(version, token) {
if (!m) return true;
const op = m[1] || '=';
const target = m[2].trim();
- if (target === '' || target === '*' || target.toLowerCase() === 'x') return true;
+ if (target === '' || target === '*' || target.toLowerCase() === 'x')
+ return true;
const t = parseVersionParts(target);
// No numeric component (e.g. `workspace:*`, a git/url spec) — can't model it,
// so treat as unconstrained rather than emit a false mismatch.
@@ -123,16 +127,22 @@ function satisfiesComparator(version, token) {
case '<':
return cmp < 0;
case '^':
- if (t.major > 0) return below(`${t.major + 1}.0.0`);
- if (t.minor > 0) return below(`0.${t.minor + 1}.0`);
+ // A partial target widens the range to the parts given: `^0` is any
+ // 0.x.y, `^0.0` is any 0.0.y, while `^0.0.3` is locked to the patch.
+ if (t.major > 0 || t.count === 1) return below(`${t.major + 1}.0.0`);
+ if (t.minor > 0 || t.count === 2) return below(`0.${t.minor + 1}.0`);
return below(`0.0.${t.patch + 1}`);
case '~':
- return below(t.count >= 2 ? `${t.major}.${t.minor + 1}.0` : `${t.major + 1}.0.0`);
+ return below(
+ t.count >= 2 ? `${t.major}.${t.minor + 1}.0` : `${t.major + 1}.0.0`,
+ );
case '=':
default:
// A full version is exact; a partial (`1`, `1.2`) behaves like a range.
if (t.count >= 3) return cmp === 0;
- return below(t.count === 2 ? `${t.major}.${t.minor + 1}.0` : `${t.major + 1}.0.0`);
+ return below(
+ t.count === 2 ? `${t.major}.${t.minor + 1}.0` : `${t.major + 1}.0.0`,
+ );
}
}
@@ -154,11 +164,33 @@ function satisfiesComparator(version, token) {
export function satisfiesRange(version, range) {
if (!isValidSemver(version)) return true;
const r = String(range ?? '').trim();
- if (r === '' || r === '*' || r.toLowerCase() === 'x' || r.toLowerCase() === 'latest') {
+ if (
+ r === '' ||
+ r === '*' ||
+ r.toLowerCase() === 'x' ||
+ r.toLowerCase() === 'latest'
+ ) {
return true;
}
return r.split('||').some(union => {
- const tokens = union.trim().split(/\s+/).filter(Boolean);
- return tokens.length === 0 || tokens.every(tok => satisfiesComparator(version, tok));
+ // A hyphen range `A - B` is the inclusive intersection `>=A <=B`; left
+ // as-is its two bare versions would each read as an exact match that
+ // can never both hold. A partial upper bound covers its whole series, as
+ // npm reads it: `1.0.0 - 2` is `>=1.0.0 <3.0.0`.
+ const normalized = union
+ .trim()
+ .replace(/^(\S+)\s+-\s+(\S+)$/, (_, low, high) => {
+ const h = parseVersionParts(high);
+ if (h.count === 0) return `>=${low}`;
+ if (h.count >= 3) return `>=${low} <=${high}`;
+ const next =
+ h.count === 2 ? `${h.major}.${h.minor + 1}.0` : `${h.major + 1}.0.0`;
+ return `>=${low} <${next}`;
+ });
+ const tokens = normalized.split(/\s+/).filter(Boolean);
+ return (
+ tokens.length === 0 ||
+ tokens.every(tok => satisfiesComparator(version, tok))
+ );
});
}
diff --git a/packages/cli/foundation/env/semver.test.mjs b/packages/cli/foundation/env/semver.test.mjs
index 8a2ad15129..3c21d634dc 100644
--- a/packages/cli/foundation/env/semver.test.mjs
+++ b/packages/cli/foundation/env/semver.test.mjs
@@ -92,6 +92,34 @@ describe('satisfiesRange', () => {
expect(satisfiesRange('0.11.0', '^0.10')).toBe(false);
});
+ it('widens a partial caret target to the parts given', () => {
+ expect(satisfiesRange('0.5.0', '^0')).toBe(true);
+ expect(satisfiesRange('0.0.3', '^0')).toBe(true);
+ expect(satisfiesRange('1.0.0', '^0')).toBe(false);
+ expect(satisfiesRange('0.0.3', '^0.0')).toBe(true);
+ expect(satisfiesRange('0.1.0', '^0.0')).toBe(false);
+ // A full 0.0.x target stays locked to that patch.
+ expect(satisfiesRange('0.0.4', '^0.0.3')).toBe(false);
+ });
+
+ it('handles hyphen ranges as inclusive bounds', () => {
+ expect(satisfiesRange('1.5.0', '1.0.0 - 2.0.0')).toBe(true);
+ expect(satisfiesRange('1.0.0', '1.0.0 - 2.0.0')).toBe(true);
+ expect(satisfiesRange('2.0.0', '1.0.0 - 2.0.0')).toBe(true);
+ expect(satisfiesRange('2.0.1', '1.0.0 - 2.0.0')).toBe(false);
+ expect(satisfiesRange('0.9.9', '1.0.0 - 2.0.0')).toBe(false);
+ expect(satisfiesRange('3.0.0', '1.0.0 - 2.0.0 || >=3')).toBe(true);
+ });
+
+ it('reads a partial upper bound in a hyphen range as its whole series', () => {
+ expect(satisfiesRange('2.9.9', '1.0.0 - 2')).toBe(true);
+ expect(satisfiesRange('3.0.0', '1.0.0 - 2')).toBe(false);
+ expect(satisfiesRange('2.3.9', '1.2.3 - 2.3')).toBe(true);
+ expect(satisfiesRange('2.4.0', '1.2.3 - 2.3')).toBe(false);
+ expect(satisfiesRange('0.8.3', '0.7.0 - 0.8')).toBe(true);
+ expect(satisfiesRange('22.14.0', '20 - 22')).toBe(true);
+ });
+
it('handles exact, tilde, unions, and wildcards', () => {
expect(satisfiesRange('1.2.3', '1.2.3')).toBe(true);
expect(satisfiesRange('1.2.4', '1.2.3')).toBe(false);