Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
57 changes: 43 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 @@ -578,6 +583,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 +865,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 +938,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 @@ -1074,6 +1091,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 +1110,26 @@
// 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);
if !range.is_match_all() {
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)?;
}

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

View check run for this annotation

Claude / Claude Code Review

Dangling-operator branch (bare ^/~/>=) misclassified as match-all

Related edge case the `emitted && !branch_has_non_any` fix above won't cover: a `||`-branch that is a **bare operator with no operand** (`^`, `~`, `>=`) *does* emit a comparator — `Version::parse("")` returns `major=None`, so `to_range` yields `Range::default()` (Caret/Tilda) or `{Gte 0.0.0}` (Gte), both of which `is_match_all()` accepts, and `MATCH_ALL_BRANCH` is set. `Bun.semver.satisfies("1.2.3-alpha.1", "1.2.3-alpha.1 || ^")` now returns `false`; pre-PR Bun and npm-loose (which drops the bar
}

is_or = false;
Expand All @@ -1113,5 +1138,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
28 changes: 28 additions & 0 deletions test/cli/install/semver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,34 @@ 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);

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