From 94f4bcf3aeda485d7f02f795101ffcb416566871 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 28 Jun 2026 04:55:13 +0000 Subject: [PATCH 1/4] semver: parse space-separated comparators as an intersection In npm's range grammar, whitespace between comparators is an AND and only "||" starts a new alternative. The range tokenizer set `is_or` for any comparator starting with a digit, `x`, `X`, `*`, `=`, or `v`, so a space-followed comparator beginning with one of those characters started a new "||" alternative instead of extending the current AND chain. Bun.semver.satisfies("2.5.0", "1.x 2.x") // true, should be false Bun.semver.satisfies("1.0.1", "<1.0.0 1.x") // true, should be false Only the "||" arm sets `is_or` now. This is the single range parser behind Bun.semver, `bun install` dependency parsing, `bun why`, and `bun pm view`. Verified against node-semver 7 over an 86,685-case differential of node-valid ranges: 12,608 mismatches before, 2,864 after, with no case that matched before mismatching after. The remaining mismatches are unrelated families this change does not touch. --- src/semver/SemverQuery.rs | 4 ++-- test/cli/install/semver.test.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/semver/SemverQuery.rs b/src/semver/SemverQuery.rs index 65a52e8825cd..9c593b9f4cfa 100644 --- a/src/semver/SemverQuery.rs +++ b/src/semver/SemverQuery.rs @@ -888,7 +888,6 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result { } b'=' | b'v' => { token.tag = TokenTag::Version; - is_or = true; i += 1; while i < input.len() && input[i] == b' ' { i += 1; @@ -915,7 +914,6 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result { } b'0'..=b'9' | b'X' | b'x' | b'*' => { token.tag = TokenTag::Version; - is_or = true; } b'|' => { i += 1; @@ -926,6 +924,8 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result { while i < input.len() && input[i] == b' ' { i += 1; } + // "||" is the only token that starts a new OR alternative; whitespace + // between comparators is an intersection (node-semver's range grammar). is_or = true; token.tag = TokenTag::None; skip_round = true; diff --git a/test/cli/install/semver.test.ts b/test/cli/install/semver.test.ts index 63aa787a222b..f4630845cf2b 100644 --- a/test/cli/install/semver.test.ts +++ b/test/cli/install/semver.test.ts @@ -735,6 +735,39 @@ describe("Bun.semver.satisfies()", () => { } }); + test("space-separated comparators are an intersection, not alternatives", () => { + // In npm's range grammar only "||" starts a new alternative; whitespace between + // comparators is an intersection. Comparators starting with a digit, "x", "*", + // "=", or "v" used to begin a new "||" group. Every expectation matches node-semver. + const cases: [version: string, range: string, expected: boolean][] = [ + ["2.5.0", "1.x 2.x", false], + ["1.0.1", "<1.0.0 1.x", false], + ["1.2.3", ">=2.0.0 1.2.3", false], + ["1.2.3", "1.2.3 4.5.6", false], + ["4.5.6", "1.2.3 4.5.6", false], + ["1.2.3", ">=2.0.0 =1.2.3", false], + ["1.2.3", ">=2.0.0 v1.2.3", false], + ["1.0.0", ">=2.0.0 *", false], + ["1.0.0", ">=2.0.0 x", false], + ["1.0.0", ">=2.0.0 x.x.x", false], + ["1.5.0", ">=1.0.0 1.x <1.4.0", false], + ["4.5.6", "4.5.6 7.8.9 || 1.2.3", false], + // the intersection holds, or an "||" alternative holds + ["1.3.0", ">=1.0.0 1.x <1.4.0", true], + ["2.5.0", "1.x || 2.x", true], + ["1.0.1", "<1.0.0 || 1.x", true], + ["1.2.3", ">=2.0.0 || 1.2.3", true], + ["1.2.3", "1.2.3 4.5.6 || 1.2.3", true], + ["1.2.3", "~1.2.1 1.2.3", true], + ["1.2.3", "~1.2.1 =1.2.3", true], + ["1.2.3", ">=1.2.1 1.2.3", true], + ["1.2.3", "1.2.3 >=1.2.1", true], + ]; + expect(cases.map(([version, range]) => ({ version, range, satisfies: satisfies(version, range) }))).toEqual( + cases.map(([version, range, expected]) => ({ version, range, satisfies: expected })), + ); + }); + test("pre-release snapshot", () => { expect(unsortedPrereleases.sort(Bun.semver.order)).toMatchSnapshot(); }); From a0f79c82bded4a43f3c9114bad13e17fd2111e7d Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 28 Jun 2026 10:17:37 +0000 Subject: [PATCH 2/4] ci: retrigger The only failed job in the previous run was the darwin-26-aarch64 test lane, which hit a buildkite-agent artifact download timeout before running any tests. No test failed anywhere in the build. From c59facdcd2f39475c3ebba32907dfd9dd1d1be60 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 1 Oct 2026 15:58:20 +0000 Subject: [PATCH 3/4] semver: cover the fstream range and bun install in the space-AND tests Remove the comment above `is_or = true` in the `||` arm. The code says the same thing. Add rows for `>=0.5 0`, the range that fstream@1.0.12 declares for mkdirp. npm reads it as `>=0.5.0 <1.0.0` and installs mkdirp 0.5.6. Add a `bun install --lockfile-only` test against a local registry that pins the same resolution. --- src/semver/SemverQuery.rs | 2 -- test/cli/install/semver.test.ts | 48 ++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/semver/SemverQuery.rs b/src/semver/SemverQuery.rs index 0c567985e2cc..94ddcf52cc30 100644 --- a/src/semver/SemverQuery.rs +++ b/src/semver/SemverQuery.rs @@ -910,8 +910,6 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result { while i < input.len() && input[i] == b' ' { i += 1; } - // "||" is the only token that starts a new OR alternative; whitespace - // between comparators is an intersection (node-semver's range grammar). is_or = true; token.tag = TokenTag::None; skip_round = true; diff --git a/test/cli/install/semver.test.ts b/test/cli/install/semver.test.ts index 70fd3bfa22ec..f439298d8285 100644 --- a/test/cli/install/semver.test.ts +++ b/test/cli/install/semver.test.ts @@ -14,7 +14,8 @@ // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR // IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -import { bunEnv, bunExe } from "harness"; +import { bunEnv, bunExe, tempDir } from "harness"; +import { join } from "path"; import { unsortedPrereleases } from "./semver-fixture.js"; const { satisfies, order } = Bun.semver; @@ -807,9 +808,7 @@ describe("Bun.semver.satisfies()", () => { }); test("space-separated comparators are an intersection, not alternatives", () => { - // In npm's range grammar only "||" starts a new alternative; whitespace between - // comparators is an intersection. Comparators starting with a digit, "x", "*", - // "=", or "v" used to begin a new "||" group. Every expectation matches node-semver. + // Every expectation is node-semver 7's answer for the same input. const cases: [version: string, range: string, expected: boolean][] = [ ["2.5.0", "1.x 2.x", false], ["1.0.1", "<1.0.0 1.x", false], @@ -823,6 +822,11 @@ describe("Bun.semver.satisfies()", () => { ["1.0.0", ">=2.0.0 x.x.x", false], ["1.5.0", ">=1.0.0 1.x <1.4.0", false], ["4.5.6", "4.5.6 7.8.9 || 1.2.3", false], + // fstream@1.0.12 declares "mkdirp": ">=0.5 0" + ["0.4.2", ">=0.5 0", false], + ["1.0.0", ">=0.5 0", false], + ["3.0.1", ">=0.5 0", false], + ["0.5.6", ">=0.5 0", true], // the intersection holds, or an "||" alternative holds ["1.3.0", ">=1.0.0 1.x <1.4.0", true], ["2.5.0", "1.x || 2.x", true], @@ -844,6 +848,42 @@ describe("Bun.semver.satisfies()", () => { }); }); +test("bun install resolves a space-separated range as an intersection", async () => { + await using registry = Bun.serve({ + port: 0, + fetch(req) { + const { origin } = new URL(req.url); + return Response.json({ + name: "dep", + "dist-tags": { latest: "3.0.1" }, + versions: Object.fromEntries( + ["0.4.2", "0.5.6", "1.0.0", "3.0.1"].map(version => [ + version, + { name: "dep", version, dist: { tarball: `${origin}/dep-${version}.tgz` } }, + ]), + ), + }); + }, + }); + using dir = tempDir("semver-space-range", { + // fstream@1.0.12 declares "mkdirp": ">=0.5 0", and npm installs mkdirp 0.5.6 for it. + "package.json": JSON.stringify({ name: "root", version: "1.0.0", dependencies: { dep: ">=0.5 0" } }), + "bunfig.toml": `[install]\ncache = false\nregistry = "${registry.url.href}"\n`, + }); + await using proc = Bun.spawn({ + cmd: [bunExe(), "install", "--lockfile-only"], + cwd: String(dir), + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + if (exitCode !== 0) expect(stderr).toBe(""); + const lockfile = await Bun.file(join(String(dir), "bun.lock")).text(); + expect(lockfile.match(/"dep@([^"]+)"/)?.[1]).toBe("0.5.6"); + expect(exitCode).toBe(0); +}); + test("a version range with >=256 || comparators does not abort", async () => { const range = Array(300).fill("1.0.0").join(" || "); await using proc = Bun.spawn({ From d3ee0e727eabeb3cb36e33ec406628702d16f9f0 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 2 Oct 2026 18:02:32 +0000 Subject: [PATCH 4/4] semver: intersect space-separated comparators only when both are valid A comparator with no operator joins the comparator before it when both parse as valid and only spaces separate them. Any other text starts a new alternative, as on main. The two `is_or = true` assignments stay deleted. Without the new check, malformed ranges that packages publish changed their reading too. The peer range `=>16.8.0` of adaptableblotter-react-aggrid@4.2.0 matched every version on main and only 16.8.0 with the deletions alone. `^6.17.2.1` matched no version. Tests: rows for match-all tails, `=` and `v` prefixes and runs of spaces. A table that pins the reading of malformed text. The install test covers a root edge and a transitive edge. --- src/semver/SemverQuery.rs | 13 +++++++ test/cli/install/semver.test.ts | 66 ++++++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/src/semver/SemverQuery.rs b/src/semver/SemverQuery.rs index 94ddcf52cc30..7bff2e280d6f 100644 --- a/src/semver/SemverQuery.rs +++ b/src/semver/SemverQuery.rs @@ -841,9 +841,12 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result { let mut count: u32 = 0; let mut skip_round; let mut is_or = false; + // Only spaces lie between the cursor and the last comparator, and that comparator is valid. + let mut after_comparator = false; while i < input.len() { skip_round = false; + let mut no_operator = false; match input[i] { b'>' => { @@ -874,6 +877,7 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result { } b'=' | b'v' => { token.tag = TokenTag::Version; + no_operator = true; i += 1; while i < input.len() && input[i] == b' ' { i += 1; @@ -900,6 +904,7 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result { } b'0'..=b'9' | b'X' | b'x' | b'*' => { token.tag = TokenTag::Version; + no_operator = true; } b'|' => { i += 1; @@ -930,6 +935,7 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result { _ => { i += 1; token.tag = TokenTag::None; + after_comparator = false; // skip tagged versions // we are assuming this is the beginning of a tagged version like "boop" @@ -993,6 +999,12 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result { } i += (!hyphenate) as usize; + let valid = parse_result.valid && !hyphenate; + // Malformed text ("=>16.0.0", "1.2.3.4") starts an alternative so that it cannot narrow the range. + if no_operator && !(after_comparator && valid) { + is_or = true; + } + if hyphenate { let second_parsed = Version::parse(sliced.sub(&input[i..])); let mut second_version = second_parsed.version.min(); @@ -1109,6 +1121,7 @@ pub fn parse(input: &[u8], sliced: SlicedString) -> Result { is_or = false; count += 1; token.wildcard = Wildcard::None; + after_comparator = valid && input.get(rollback) == Some(&b' '); } } diff --git a/test/cli/install/semver.test.ts b/test/cli/install/semver.test.ts index f439298d8285..0952a9dfe5cb 100644 --- a/test/cli/install/semver.test.ts +++ b/test/cli/install/semver.test.ts @@ -41,6 +41,12 @@ function testSatisfies(right: any, left: any, expected: boolean) { expect(satisfies(left, rightBuffer)).toBe(expected); } +function testSatisfiesTable(cases: [version: string, range: string, expected: boolean][]) { + expect(cases.map(([version, range]) => ({ version, range, satisfies: satisfies(version, range) }))).toEqual( + cases.map(([version, range, expected]) => ({ version, range, satisfies: expected })), + ); +} + describe("Bun.semver.order()", () => { test("whitespace bug fix", () => { expect( @@ -809,7 +815,7 @@ describe("Bun.semver.satisfies()", () => { test("space-separated comparators are an intersection, not alternatives", () => { // Every expectation is node-semver 7's answer for the same input. - const cases: [version: string, range: string, expected: boolean][] = [ + testSatisfiesTable([ ["2.5.0", "1.x 2.x", false], ["1.0.1", "<1.0.0 1.x", false], ["1.2.3", ">=2.0.0 1.2.3", false], @@ -827,6 +833,13 @@ describe("Bun.semver.satisfies()", () => { ["1.0.0", ">=0.5 0", false], ["3.0.1", ">=0.5 0", false], ["0.5.6", ">=0.5 0", true], + ["3.0.1", ">=0.5 v0", false], + ["3.0.1", ">=0.5 = 0", false], + ["3.0.1", " >=0.5 0 ", false], + ["0.5.6", " >=0.5 0 ", true], + ["9.9.9", "~1.0.0 x", false], + ["1.0.5", "~1.0.0 x", true], + ["0.1.0", ">=1.0.0 <2.0.0 x", false], // the intersection holds, or an "||" alternative holds ["1.3.0", ">=1.0.0 1.x <1.4.0", true], ["2.5.0", "1.x || 2.x", true], @@ -837,10 +850,24 @@ describe("Bun.semver.satisfies()", () => { ["1.2.3", "~1.2.1 =1.2.3", true], ["1.2.3", ">=1.2.1 1.2.3", true], ["1.2.3", "1.2.3 >=1.2.1", true], - ]; - expect(cases.map(([version, range]) => ({ version, range, satisfies: satisfies(version, range) }))).toEqual( - cases.map(([version, range, expected]) => ({ version, range, satisfies: expected })), - ); + ["1.2.3-beta", "1.2.3-beta 1.x", true], + ["1.5.0", "1.2.3-beta 1.x", false], + ]); + }); + + test("malformed text next to a comparator still starts an alternative", () => { + // Packages publish ranges like these: adaptableblotter-react-aggrid@4.2.0 has the peer range "=>16.8.0". + testSatisfiesTable([ + ["18.2.0", "=>16.8.0", true], + ["18.2.0", "=> 16.0.0", true], + ["18.2.0", ">=18.0.0 - <19.0.0", true], + ["5.0.0", ">=1.0.0 1.2.3.4", true], + ["1.5.0", "^2 1.2.3 - 2.3.4", true], + ["4.5.6", "1.2.3 - 2.3.4 4.5.6", true], + ["4.5.6", "1.2.3 foo 4.5.6", true], + ["4.5.6", "1.2.3-beta|4.5.6", true], + ["1.0.0", "*1.0.0", true], + ]); }); test("pre-release snapshot", () => { @@ -849,25 +876,35 @@ describe("Bun.semver.satisfies()", () => { }); test("bun install resolves a space-separated range as an intersection", async () => { + // fstream@1.0.12 declares "mkdirp": ">=0.5 0", and npm installs mkdirp 0.5.6 for it. + const range = ">=0.5 0"; await using registry = Bun.serve({ port: 0, fetch(req) { - const { origin } = new URL(req.url); + const { origin, pathname } = new URL(req.url); + const name = pathname.slice(1); + const versions: Record = + name === "parent" + ? { "1.0.0": { dependencies: { transitive: range } } } + : { "0.4.2": {}, "0.5.6": {}, "1.0.0": {}, "3.0.1": {} }; return Response.json({ - name: "dep", - "dist-tags": { latest: "3.0.1" }, + name, + "dist-tags": { latest: Object.keys(versions).at(-1) }, versions: Object.fromEntries( - ["0.4.2", "0.5.6", "1.0.0", "3.0.1"].map(version => [ + Object.entries(versions).map(([version, manifest]) => [ version, - { name: "dep", version, dist: { tarball: `${origin}/dep-${version}.tgz` } }, + { name, version, ...manifest, dist: { tarball: `${origin}/${name}-${version}.tgz` } }, ]), ), }); }, }); using dir = tempDir("semver-space-range", { - // fstream@1.0.12 declares "mkdirp": ">=0.5 0", and npm installs mkdirp 0.5.6 for it. - "package.json": JSON.stringify({ name: "root", version: "1.0.0", dependencies: { dep: ">=0.5 0" } }), + "package.json": JSON.stringify({ + name: "root", + version: "1.0.0", + dependencies: { direct: range, parent: "1.0.0" }, + }), "bunfig.toml": `[install]\ncache = false\nregistry = "${registry.url.href}"\n`, }); await using proc = Bun.spawn({ @@ -880,7 +917,10 @@ test("bun install resolves a space-separated range as an intersection", async () const [, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); if (exitCode !== 0) expect(stderr).toBe(""); const lockfile = await Bun.file(join(String(dir), "bun.lock")).text(); - expect(lockfile.match(/"dep@([^"]+)"/)?.[1]).toBe("0.5.6"); + expect({ + direct: lockfile.match(/"direct@([^"]+)"/)?.[1], + transitive: lockfile.match(/"transitive@([^"]+)"/)?.[1], + }).toEqual({ direct: "0.5.6", transitive: "0.5.6" }); expect(exitCode).toBe(0); });