Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
68 changes: 53 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;

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

View check run for this annotation

Claude / Claude Code Review

Lone '|' (single pipe) misclassified as '||' separator, sets MATCH_ALL_BRANCH

Same class as the `boop`/`^`/`-` siblings, but at the separator itself: the `b'|'` arm doesn't distinguish a lone `|` from `||` (the inner `while input[i] == b'|'` runs zero times), so `"| 1.2.3-alpha.1"` sets `MATCH_ALL_BRANCH` at line 944 and `Bun.semver.satisfies("1.2.3-alpha.1", "| 1.2.3-alpha.1")` now returns `false` — pre-PR Bun and npm-loose (which only splits on `/\s*\|\|\s*/` and drops a lone `|` via `COMPARATORLOOSE`) return `true`. Trailing form `"1.2.3-alpha.1 |"` hits the same via t
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,6 +972,9 @@
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;
}
}
Expand Down Expand Up @@ -1074,6 +1096,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 +1113,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);
// A bare operator with no operand (`^`, `~`, `>=` at end of
// branch) parses len==0 and node-semver loose mode drops it
// before the collapse step, so it is not the ANY comparator.
if !range.is_match_all() || parse_result.len == 0 {
Comment thread
robobun marked this conversation as resolved.
Outdated
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 +1147,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
40 changes: 40 additions & 0 deletions test/cli/install/semver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,46 @@ 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"]) {
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