Skip to content
Closed
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
43 changes: 27 additions & 16 deletions src/semver/SemverQuery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ pub enum Wildcard {
}

impl Token {
pub(crate) fn to_range(self, version: &version::Partial<u64>) -> Range {
pub(crate) fn to_range(self, parsed: &version::ParseResult<u64>) -> Range {
let version = &parsed.version;
match self.tag {
// Allows changes that do not modify the left-most non-zero element in the [major, minor, patch] tuple
TokenTag::Caret => {
Expand Down Expand Up @@ -693,18 +694,28 @@ impl Token {
}

match self.wildcard {
Wildcard::Major => Range {
left: Comparator {
op: RangeOp::Gte,
version: version.min(),
Wildcard::Major => match self.tag {
// https://github.com/npm/node-semver/blob/3a8a4309ae986c1967b3073ba88c9e69433d44cb/classes/range.js#L380-L387
// A wildcard major makes `<` and `>` allow nothing, whatever follows it. An operator
// before text that is not a version (`>latest`) also gets here, with no wildcard
// written, and keeps reading as anything.
TokenTag::Lt | TokenTag::Gt if parsed.wildcard_written => Range {
Comment thread
robobun marked this conversation as resolved.
Resolved
left: Comparator::null_set(),
..Default::default()
},
right: Comparator {
op: RangeOp::Lte,
version: Version {
major: u64::MAX,
minor: u64::MAX,
patch: u64::MAX,
..Default::default()
_ => Range {
left: Comparator {
op: RangeOp::Gte,
version: version.min(),
},
right: Comparator {
op: RangeOp::Lte,
version: Version {
major: u64::MAX,
minor: u64::MAX,
patch: u64::MAX,
..Default::default()
},
},
},
},
Expand Down Expand Up @@ -1097,15 +1108,15 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result<Group, AllocError> {
list.or_version(version)?;
}
_ => {
list.or_range(&token.to_range(&parse_result.version))?;
list.or_range(&token.to_range(&parse_result))?;
Comment thread
robobun marked this conversation as resolved.
Resolved
}
}
} else if count == 0 {
list.and_range(&token.to_range(&parse_result.version))?;
list.and_range(&token.to_range(&parse_result))?;
} else if is_or {
list.or_range(&token.to_range(&parse_result.version))?;
list.or_range(&token.to_range(&parse_result))?;
} else {
list.and_range(&token.to_range(&parse_result.version))?;
list.and_range(&token.to_range(&parse_result))?;
}

is_or = false;
Expand Down
18 changes: 17 additions & 1 deletion src/semver/SemverRange.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use core::cmp::Ordering;
use core::fmt;

use crate::Version;
use crate::query::token::Wildcard;
use crate::version::Tag;
use crate::{SlicedString, Version};

#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Eq, Default)]
Expand Down Expand Up @@ -183,6 +184,21 @@ pub struct Comparator {
}

impl Comparator {
/// `<0.0.0-0`: no version satisfies it. node-semver uses it for the empty set.
pub(crate) fn null_set() -> Comparator {
let zero: &[u8] = b"0";
Comparator {
op: Op::Lt,
version: Version {
tag: Tag {
pre: SlicedString::init(zero, zero).external(),
..Default::default()
},
..Default::default()
},
}
}

/// `< {major+1}.0.0`, or `<= u64::MAX.u64::MAX.u64::MAX` when `major+1`
/// would overflow so the desugared range stays non-empty at the ceiling.
pub(crate) fn lt_next_major(major: u64) -> Comparator {
Expand Down
5 changes: 5 additions & 0 deletions src/semver/Version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ impl<T: VersionInt> VersionType<T> {
}
_ => {}
}
result.wildcard_written = result.wildcard != Wildcard::None;
}
}
c => {
Expand Down Expand Up @@ -1191,6 +1192,9 @@ pub struct TagResult {
pub struct ParseResult<T: VersionInt> {
pub wildcard: Wildcard,
pub valid: bool,
/// The `wildcard` part is a written `x`, `X` or `*`. False when that part is only absent,
/// as in `1` or in text that is not a version (which also reads as `Wildcard::Major`).
Comment thread
robobun marked this conversation as resolved.
Resolved
pub(crate) wildcard_written: bool,
pub version: Partial<T>,
pub(crate) len: u32,
}
Expand All @@ -1200,6 +1204,7 @@ impl<T: VersionInt> Default for ParseResult<T> {
Self {
wildcard: Wildcard::None,
valid: true,
wildcard_written: false,
version: Partial::default(),
len: 0,
}
Expand Down
21 changes: 14 additions & 7 deletions src/semver/intersects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,20 @@ impl<'a> Interval<'a> {
}

fn is_non_empty(&self) -> bool {
match (self.lower, self.upper) {
(Some(l), Some(u)) => match l.version.order_without_build(u.version, l.buf, u.buf) {
Ordering::Less => true,
Ordering::Equal => l.inclusive && u.inclusive,
Ordering::Greater => false,
},
_ => true,
let Some(u) = self.upper else {
return true;
};
// `0.0.0-0` is the lowest version, so an interval with no lower bound starts there.
// That makes `<0.0.0-0` (`Comparator::null_set`) empty on its own.
Comment thread
robobun marked this conversation as resolved.
Resolved
let l = self.lower.unwrap_or_else(|| Bound {
version: Comparator::null_set().version,
buf: b"",
inclusive: true,
});
match l.version.order_without_build(u.version, l.buf, u.buf) {
Comment thread
robobun marked this conversation as resolved.
Resolved
Ordering::Less => true,
Ordering::Equal => l.inclusive && u.inclusive,
Ordering::Greater => false,
}
}
}
Comment thread
robobun marked this conversation as resolved.
Resolved
Expand Down
24 changes: 24 additions & 0 deletions test/cli/install/nested-overrides.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,30 @@ describe.concurrent("version-scoped targets", () => {
await installOk(dir, "--frozen-lockfile");
});

// node-semver reads `<x` and `>x` as `<0.0.0-0`, the empty set. `intersects` is what npm's semver 7.7.4
// answers, and npm applies a rule on that answer. Every rule sets 2.0.0. `untouched` is what the declared
// range resolves to without it.
describe.concurrent.each([
{ declared: "^1.0.0", selector: "<x", intersects: false, untouched: "1.1.0" },
{ declared: "^1.0.0", selector: ">*", intersects: false, untouched: "1.1.0" },
// neither side has a lower bound
{ declared: "<2", selector: "<x", intersects: false, untouched: "1.1.0" },
{ declared: "<=1.0.1", selector: ">x", intersects: false, untouched: "1.0.1" },
// the other alternative of the selector still matches
{ declared: "^1.0.0", selector: "<x || 1", intersects: true, untouched: "1.1.0" },
])("a selector with a wildcard major after < or > %j", ({ declared, selector, intersects, untouched }) => {
test(intersects ? "the rule applies" : "the rule leaves the edge alone", async () => {
const dir = await project({
dependencies: { "no-deps": declared },
overrides: { [`no-deps@${selector}`]: "2.0.0" },
});
const { err } = await installOk(dir);
expect(err).not.toContain("warn:");
expect(await versionSeenBy(dir, undefined, "no-deps")).toBe(intersects ? "2.0.0" : untouched);
await installOk(dir, "--frozen-lockfile");
});
});

test("edges declared with a dist-tag never match", async () => {
const dir = await project({
dependencies: { "no-deps": "latest", "one-range-dep": "1.0.0" },
Expand Down
71 changes: 71 additions & 0 deletions test/cli/install/semver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,77 @@ describe("Bun.semver.satisfies()", () => {
}
});

describe("a wildcard major after < or >", () => {
// node-semver's replaceXRange turns `<x` and `>x` into `<0.0.0-0`, which no version satisfies.
// Every expectation in the first three tests is the answer of npm's semver 7.7.4.
type Row = [range: string, version: string, expected: boolean];
const check = (rows: Row[]) =>
expect(rows.map(([range, version]) => [range, version, satisfies(version, range)])).toEqual(rows);

test("is the empty set", () => {
const ranges = [
...["<x", "<X", "<*", ">x", ">X", ">*"],
...["< x", "> *", "<\tx", "<vx", ">v*"],
...["<x.x", ">x.x", "<x.x.x", ">x.x.x", "<*.*", ">*.*.*"],
// a number or build metadata after the wildcard changes nothing
...["<x.1", ">x.1", "<x.1.2", ">x.1.2", "<x+build", ">x.x.x+build"],
];
const versions = ["0.0.0", "1.0.0", "1.0.0-beta.1", "0.0.0-0"];
check(ranges.flatMap(range => versions.map((version): Row => [range, version, false])));
});

test("empties an intersection and drops out of a union", () => {
check([
[">x <2", "1.2.3", false],
["<2 >x", "1.2.3", false],
[">=1 <x", "1.2.3", false],
["<x >=1", "1.2.3", false],
["<=x <x", "1.2.3", false],
["^1.0.0 >*", "1.2.3", false],
["1.0.0 <x", "1.0.0", false],
["<x || 1.0.0", "1.0.0", true],
["<x || 1.0.0", "2.0.0", false],
["1.0.0 || >x", "1.0.0", true],
["1.0.0 || >x", "2.0.0", false],
["<x||1.0.0", "1.0.0", true],
["<x||1.0.0", "2.0.0", false],
["<* || ^2", "2.1.0", true],
["<* || ^2", "1.1.0", false],
[">x || >=3", "3.0.0", true],
[">x || >=3", "2.0.0", false],
["<x || >x", "1.0.0", false],
]);
});

test("every other operator still allows any version", () => {
check([
...[">=x", "<=x", ">=*", "<=*", ">=x.x", "<=x.x.x", "<= x", ">= *"].map((range): Row => [range, "1.0.0", true]),
...["x", "*", "=x", "^x", "~x", "~>x"].map((range): Row => [range, "1.0.0", true]),
[">=x <2", "1.2.3", true],
[">=x <2", "2.0.0", false],
// a wildcard after a number is a bound, not the empty set
["<1.x", "0.9.0", true],
["<1.x", "1.0.0", false],
[">1.x", "2.0.0", true],
[">1.x", "1.9.0", false],
["<1.2.x", "1.1.9", true],
["<1.2.x", "1.2.0", false],
[">1.2.x", "1.3.0", true],
[">1.2.x", "1.2.9", false],
]);
});

test("an operator before text that is not a version still allows any version", () => {
// npm calls these ranges invalid. Bun reads the comparator as `*`, and the rows above do not change that.
check([
[">latest", "1.0.0", true],
["<foo", "1.0.0", true],
["< v", "1.0.0", true],
[">^1.0.0", "2.0.0", true],
]);
});
});

test("range includes", () => {
// https://github.com/npm/node-semver/blob/14d263faa156e408a033b9b12a2f87735c2df42c/test/fixtures/range-include.js#L3
var tests = [
Expand Down
Loading