Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 54 additions & 15 deletions src/semver/SemverQuery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@
impl Flags {
pub const PRE: usize = 1;
pub const BUILD: usize = 0;
/// Set when an explicit `||` union contains a branch that is entirely the
/// ANY comparator (`*`, `x`, `>=0.0.0`, ...). node-semver collapses such a
/// range to `*` before applying the prerelease rule.
pub const MATCH_ALL_BRANCH: usize = 2;
}

pub struct Group {
Expand Down Expand Up @@ -462,6 +466,7 @@
let range = &self.head.head.range;
if self.head.next.is_none()
&& self.head.head.next.is_none()
&& !self.flags.is_set(Flags::MATCH_ALL_BRANCH)
Comment thread
robobun marked this conversation as resolved.
&& range.has_left()
&& range.left.op == RangeOp::Eql
&& !range.has_right()
Expand Down Expand Up @@ -498,6 +503,7 @@
pub fn is_exact(&self) -> bool {
self.head.next.is_none()
&& self.head.head.next.is_none()
&& !self.flags.is_set(Flags::MATCH_ALL_BRANCH)
&& !self.head.head.range.has_right()
&& self.head.head.range.left.op == RangeOp::Eql
}
Expand All @@ -514,7 +520,8 @@

#[inline]
pub fn eql(&self, rhs: &Group) -> bool {
self.head.eql(&rhs.head)
self.flags.is_set(Flags::MATCH_ALL_BRANCH) == rhs.flags.is_set(Flags::MATCH_ALL_BRANCH)
&& self.head.eql(&rhs.head)
}

pub fn to_version(&self) -> Version {
Expand Down Expand Up @@ -578,6 +585,11 @@
#[inline]
pub fn satisfies(&self, version: Version, group_buf: &[u8], version_buf: &[u8]) -> bool {
if version.tag.has_pre() {
// node-semver collapses a `||`-union containing a match-all branch
// to `*`, which then rejects every prerelease (see range.js L57-67).
if self.flags.is_set(Flags::MATCH_ALL_BRANCH) {
return false;
Comment thread
robobun marked this conversation as resolved.
}
Comment thread
robobun marked this conversation as resolved.
self.head.satisfies_pre(version, group_buf, version_buf)
} else {
self.head.satisfies(version, group_buf, version_buf)
Expand Down Expand Up @@ -855,6 +867,8 @@
let mut count: u32 = 0;
let mut skip_round;
let mut is_or = false;
let mut saw_or_separator = false;
let mut branch_has_non_any = false;

while i < input.len() {
skip_round = false;
Expand Down Expand Up @@ -926,6 +940,11 @@
while i < input.len() && input[i] == b' ' {
i += 1;
}
if !branch_has_non_any {
list.flags.set_value(Flags::MATCH_ALL_BRANCH, true);
}
Comment thread
robobun marked this conversation as resolved.
saw_or_separator = true;
branch_has_non_any = false;
Comment thread
robobun marked this conversation as resolved.
Outdated
Comment thread
robobun marked this conversation as resolved.
Outdated
is_or = true;
token.tag = TokenTag::None;
skip_round = true;
Expand Down Expand Up @@ -953,11 +972,15 @@
while i < input.len() && input[i] != b' ' && input[i] != b'|' {
i += 1;
}
// node-semver loose mode drops these tokens via `.filter(c => c.length)`
// before the collapse-to-`*` step, so a garbage-only branch is not ANY.
Comment thread
robobun marked this conversation as resolved.
branch_has_non_any = true;
Comment thread
robobun marked this conversation as resolved.
skip_round = true;
}
}

if !skip_round {
let has_real_operand = matches!(input.get(i), Some(b'0'..=b'9' | b'x' | b'X' | b'*'));

Check warning on line 983 in src/semver/SemverQuery.rs

View check run for this annotation

Claude / Claude Code Review

has_real_operand first-byte check misclassifies x/X/*-prefixed garbage as ANY

Same class as the `vv`/`==` sibling that 6663913e fixed, but a gap in that fix itself: `has_real_operand` checks only the first byte, so an `x`/`X`/`*`-prefixed garbage token (`"xx"`, `"x."`, `"*."`, `"xy"`, `"*a"`, `"^xy"`) passes the check, `Version::parse` yields `wildcard=Major`, and `to_range` produces `{Gte 0.0.0}` / `Range::default()` which `is_match_all()` accepts — `branch_has_non_any` stays false and `MATCH_ALL_BRANCH` is set. `Bun.semver.satisfies("1.2.3-alpha.1", "xx || 1.2.3-alpha.1
let parse_result = Version::parse(sliced.sub(&input[i..]));
let version = parse_result.version.min();
if version.tag.has_build() {
Expand Down Expand Up @@ -1074,6 +1097,9 @@
},
};

if !range.is_match_all() {
branch_has_non_any = true;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
if is_or {
list.or_range(&range)?;
} else {
Expand All @@ -1088,23 +1114,32 @@
// covers a leading "--foo" (treat "--foo" the same as "-foo", example:
// foo/bar@1.2.3@--canary.24) as well as a dangling "-" after a skipped
// tag, like "1 || - foo".
branch_has_non_any = true;
token.wildcard = Wildcard::None;
continue;
} else if count == 0 && token.tag == TokenTag::Version {
match parse_result.wildcard {
Wildcard::None => {
list.or_version(version)?;
}
_ => {
list.or_range(&token.to_range(&parse_result.version))?;
}
}
} else if count == 0 {
list.and_range(&token.to_range(&parse_result.version))?;
} else if is_or {
list.or_range(&token.to_range(&parse_result.version))?;
} else if count == 0
&& token.tag == TokenTag::Version
&& parse_result.wildcard == Wildcard::None
{
branch_has_non_any = true;
list.or_version(version)?;
} else {
list.and_range(&token.to_range(&parse_result.version))?;
let range = token.to_range(&parse_result.version);
// An operator whose operand is not a real version or wildcard
// char (`^`, `~`, `>=`, `^v`, `vv`, ...) is dropped by
// node-semver loose mode before the collapse step.
if !range.is_match_all() || !has_real_operand {
branch_has_non_any = true;
}
if count == 0 && token.tag == TokenTag::Version {
list.or_range(&range)?;
} else if count == 0 {
list.and_range(&range)?;
} else if is_or {
Comment thread
robobun marked this conversation as resolved.
list.or_range(&range)?;
} else {
list.and_range(&range)?;
}
}

is_or = false;
Expand All @@ -1113,5 +1148,9 @@
}
}

Comment thread
robobun marked this conversation as resolved.
if saw_or_separator && !branch_has_non_any {
list.flags.set_value(Flags::MATCH_ALL_BRANCH, true);
}

Ok(list)
}
13 changes: 13 additions & 0 deletions src/semver/SemverRange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ impl Range {
self.right.op != Op::Unset
}

/// True when this range is node-semver's ANY comparator (`*` / `x` /
/// `>=0.0.0`): it accepts every release version and, under the default
/// prerelease rule, no prerelease.
pub fn is_match_all(self) -> bool {
if !self.has_left() {
return true;
}
self.left.op == Op::Gte
&& self.left.version.is_zero()
&& !self.left.version.tag.has_pre()
&& !self.has_right()
}
Comment thread
robobun marked this conversation as resolved.

Comment thread
coderabbitai[bot] marked this conversation as resolved.
/// Is the Range equal to another Range
/// This does not evaluate the range.
#[inline]
Expand Down
58 changes: 58 additions & 0 deletions test/cli/install/semver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,64 @@ describe("Bun.semver.satisfies()", () => {

testSatisfies("^3.0.0-next.0||^3.0.0", "3.0.0-next.2", true);

// node-semver collapses a `||`-union containing a match-all branch to `*`
// before applying the prerelease rule, so the prerelease is rejected even
// though a sibling branch names it explicitly.
for (const any of ["*", "x", "X", "x.x", "x.x.x", ">=0.0.0", ">=0.0.0+build", "* *", "0.0.0 - x", "^*", "~*", ""]) {
testSatisfies(`${any} || 1.2.3-alpha.1`, "1.2.3-alpha.1", false);
testSatisfies(`1.2.3-alpha.1 || ${any}`, "1.2.3-alpha.1", false);
testSatisfies(`${any} || ^1.2.3-alpha`, "1.2.3-alpha.1", false);
testSatisfies(`${any} || 1.2.3-alpha.1 || ${any}`, "1.2.3-alpha.1", false);
}
for (const any of ["*", "x", "X", "x.x", "x.x.x", ">=0.0.0", ">=0.0.0+build", "* *", "0.0.0 - x"]) {
// A release version still satisfies the collapsed `*`.
testSatisfies(`${any} || 1.2.3-alpha.1`, "1.2.4", true);
}
testSatisfies("|| 1.2.3-alpha.1", "1.2.3-alpha.1", false);
testSatisfies("1.2.3-alpha.1 ||", "1.2.3-alpha.1", false);
// A non-match-all first branch keeps union semantics.
testSatisfies("1.x || 1.2.3-alpha.1", "1.2.3-alpha.1", true);
testSatisfies(">0.0.0 || 1.2.3-alpha.1", "1.2.3-alpha.1", true);
testSatisfies(">=1.0.0 || 1.2.3-alpha.1", "1.2.3-alpha.1", true);
// `>=0.0.0-0` has a prerelease tag so it is not the ANY comparator.
testSatisfies(">=0.0.0-0 || 1.2.3-alpha.1", "1.2.3-alpha.1", true);
// `*` alongside a narrower comparator in the same `||`-branch is not a
// match-all branch: node-semver drops the `*` and keeps the narrower one.
testSatisfies("* 2.0.0 || 1.2.3-alpha.1", "1.2.3-alpha.1", true);
testSatisfies("2.0.0 * || 1.2.3-alpha.1", "1.2.3-alpha.1", true);
testSatisfies("2.0.0 || * 1.2.3-alpha.1", "1.2.3-alpha.1", true);
testSatisfies("* 1.2.3-alpha.1", "1.2.3-alpha.1", true);
// A dist-tag/garbage branch is dropped by node-semver loose mode before
// the collapse-to-`*` step, so it is not a match-all branch.
testSatisfies("1.2.3-alpha.1 || boop", "1.2.3-alpha.1", true);
testSatisfies("boop || 1.2.3-alpha.1", "1.2.3-alpha.1", true);
testSatisfies("1.2.3-alpha.1 || latest", "1.2.3-alpha.1", true);
testSatisfies("latest || 1.2.3-alpha.1", "1.2.3-alpha.1", true);
testSatisfies("boop || * || 1.2.3-alpha.1", "1.2.3-alpha.1", false);
// A bare operator with no operand is likewise dropped, not treated as `*`.
for (const op of [
"^",
"~",
">=",
">",
"<",
"<=",
"=",
"v",
"-",
"- -",
"- boop",
"vv",
"==",
"^v",
"~=",
">=v",
"^\t",
]) {
testSatisfies(`1.2.3-alpha.1 || ${op}`, "1.2.3-alpha.1", true);
testSatisfies(`${op} || 1.2.3-alpha.1`, "1.2.3-alpha.1", true);
}

const notPassing = [
"0.1.0",
"0.10.0",
Expand Down
Loading