Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
64 changes: 50 additions & 14 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 Down Expand Up @@ -578,6 +584,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.
}

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

View check run for this annotation

Claude / Claude Code Review

npm.rs find_best_version Eql fast path bypasses MATCH_ALL_BRANCH

🟡 Same predicate-sync class as the `is_exact()`/`get_exact_version()` guards applied in 0d85b615 and the open `Group::eql()` note, but at a different site: `find_best_version`/`find_best_version_with_filter` in `src/install/npm.rs` (~1823/~1893) open-code `group.head.head.range.left.op == Eql` without checking `MATCH_ALL_BRANCH`. For `"1.2.3-alpha.1 || *"` the fast path returns `1.2.3-alpha.1`, but `Group::satisfies(1.2.3-alpha.1)` now returns `false` via the new early return here — the resolver
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 +866,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 +939,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,6 +971,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 +1095,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 @@ -1090,21 +1114,29 @@
// tag, like "1 || - foo".
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;
}

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

View check run for this annotation

Claude / Claude Code Review

parse_result.len == 0 guard defeated by Version::parse leading v/=/whitespace skip

The `parse_result.len == 0` guard added in 5ae87f1f only covers a bare operator at end-of-input; `Version::parse` also treats input made entirely of `v`/`=`/whitespace as "no operand" but returns `len > 0` (Version.rs:486-511 counts skipped chars into `result.len`). So `Bun.semver.satisfies("1.2.3-alpha.1", "1.2.3-alpha.1 || vv")` (also `"|| =="`, `"|| ^v"`, `"|| ^\t"`) leaves `branch_has_non_any` false, sets `MATCH_ALL_BRANCH`, and returns `false` — a regression from pre-PR Bun and npm-loose (b
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 +1145,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"]) {
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