Skip to content

install: match npm's version resolution for prerelease ranges, || groups, and * - #32843

Closed
robobun wants to merge 8 commits into
mainfrom
farm/fcae3838/npm-version-resolution
Closed

robobun wants to merge 8 commits into
mainfrom
farm/fcae3838/npm-version-resolution

Conversation

@robobun

@robobun robobun commented Jun 27, 2026 •

Copy link
Copy Markdown
Collaborator

bun install resolves three classes of npm ranges to different versions than npm, and fails an install npm completes. All of them come from PackageManifest::find_best_version / find_best_version_with_filter in src/install/npm.rs.

Repro

Against a registry with the listed versions (latest as shown), bun install --lockfile-only picks:

versions (latest) range npm 11 bun
1.1.1, 1.2.0-alpha.1, 2.0.0 (2.0.0) <1.2.0-alpha.2 1.2.0-alpha.1 1.1.1
1.2.0, 2.0.0-rc.1, 3.0.0 (3.0.0) <=2.0.0-rc.1 2.0.0-rc.1 1.2.0
1.0.0, 2.0.0, 2.1.0 (2.1.0) 1.0.0 || ^2.0.0 2.1.0 1.0.0
1.0.0-beta.1, 1.0.0-beta.2 (1.0.0-beta.2) * 1.0.0-beta.2 error: No version matching "*" found for specifier ... (but package exists)
1.0.0, 2.0.0-beta.1 (2.0.0-beta.1) * 2.0.0-beta.1 1.0.0

The fourth one is the worst: a package whose published versions are all prereleases (nightly/canary-only packages) aborts the whole install with error: No version matching "*" found for specifier "<name>" (but package exists).

Cause

  • The exact-version fast path checked group.head.head.range.left.op == Op::Eql, which is true for the first || alternative of 1.0.0 || ^2.0.0, so the rest of the group was never evaluated (order dependent: ^2.0.0 || 1.0.0 resolved to 2.1.0).
  • The stable list was scanned first and prereleases only if no stable matched, so a satisfying prerelease above the best satisfying stable was never chosen. For prerelease ranges the latest dist-tag was also only honored when it compared equal to the range's left version, instead of npm's "prefer latest whenever it satisfies".
  • * required latest to satisfy the group, which a prerelease never does without a prerelease comparator, so prerelease-only packages errored. npm treats the literal * as the latest dist-tag unconditionally.

Fix

In both find_best_version and find_best_version_with_filter:

  • gate the exact fast path on Group::get_exact_version() (single exact comparator, no ||/&&/right bound)
  • accept the latest dist-tag when it satisfies the range, matching npm-pick-manifest (and pnpm)
  • accept the latest dist-tag unconditionally for the literal *. npm only does this for the literal * (not >=0.0.0 or x, which parse to the same structure), so the parser records the literal in a new Flags::STAR bit (Group::is_star_literal())
  • under minimumReleaseAge, route * through the same dist-tag fallback latest uses, so a too recent latest falls back to an older version of the same release channel instead of failing
  • search both the release and prerelease lists when the range opts into prereleases and take the higher satisfying version

Verification

New suite test/cli/install/bun-install-version-resolution.test.ts (local registry, packuments only): 10 of 13 tests fail on bun before the fix, 13/13 after. Every expectation was produced by running npm 11 against the same packuments, not by assumption (npm is not pure maxSatisfying: it prefers a satisfying latest, which the tests also pin).

Cross-checked with the same harness that the existing prerelease matrix in test/cli/install/bun-install-registry.test.ts (prereleases-1 through prereleases-4, 34 expectations) is unchanged, and minimum-release-age.test.ts, bun-install.test.ts, bun-add.test.ts, bun-update.test.ts show no new failures.

Supersedes #30076, which fixes only the || fast path part of this.

npm 11 / npm-pick-manifest / node-semver / bun comparison after the fix
case                      range             latest          npm                  pickManifest    maxSatisfying   bun
pre-above-stable          <1.2.0-alpha.2    2.0.0           1.2.0-alpha.1        1.2.0-alpha.1   1.2.0-alpha.1   1.2.0-alpha.1
range-pre-upper           <=2.0.0-rc.1      3.0.0           2.0.0-rc.1           2.0.0-rc.1      2.0.0-rc.1      2.0.0-rc.1
vp                        <1.2.0-alpha.2    1.2.0           1.2.0-alpha.1        1.2.0-alpha.1   1.2.0-alpha.1   1.2.0-alpha.1
vp2                       <=2.0.0-rc.1      1.2.0           1.2.0                1.2.0           2.0.0-rc.1      1.2.0
or-exact                  1.0.0 || ^2.0.0   2.1.0           2.1.0                2.1.0           2.1.0           2.1.0
or-exact-rev              ^2.0.0 || 1.0.0   2.1.0           2.1.0                2.1.0           2.1.0           2.1.0
or-scan                   1.0.0 || ^2.0.0   3.0.0           2.1.0                2.1.0           2.1.0           2.1.0
or-only-exact             1.0.0 || ^3.0.0   2.0.0           1.0.0                1.0.0           1.0.0           1.0.0
only-pre                  *                 1.0.0-beta.2    1.0.0-beta.2         1.0.0-beta.2    null            1.0.0-beta.2
only-pre-no-latest        *                 (none)          ETARGET              ETARGET         null            error (same)
latest-held-back          ^1.0.0            1.1.0           1.1.0                1.1.0           1.2.0           1.1.0
exact                     1.0.0             2.0.0           1.0.0                1.0.0           1.0.0           1.0.0
star-stable               *                 2.0.0           2.0.0                2.0.0           2.0.0           2.0.0
star-pre-latest           *                 2.0.0-beta.1    2.0.0-beta.1         2.0.0-beta.1    1.0.0           2.0.0-beta.1
caret-pre-latest          ^1.0.0-0          1.0.0-0         1.0.0-0              1.0.0-0         1.0.0-8         1.0.0-0
pre-range-latest-stable   ^1.2.3-alpha.1    1.2.4           1.2.4                1.2.4           1.3.0           1.2.4
pre-above-latest-tag      ^1.0.0-future.5   1.0.0-future.4  1.0.0-future.7       1.0.0-future.7  1.0.0-future.7  1.0.0-future.7
pre-eq-latest-tag         ^1.0.0-future.4   1.0.0-future.4  1.0.0-future.4       1.0.0-future.4  1.0.0-future.7  1.0.0-future.4
gte-zero-pre-latest       >=0.0.0           2.0.0-beta.1    1.0.0                1.0.0           1.0.0           1.0.0
gte-zero-short            >=0               2.0.0-beta.1    1.0.0                1.0.0           1.0.0           1.0.0
x-pre-latest              x                 2.0.0-beta.1    1.0.0                1.0.0           1.0.0           1.0.0
gte-zero-only-pre         >=0.0.0           1.0.0-beta.2    ETARGET              ETARGET         null            error (same)

Before the fix, bun disagreed with npm on pre-above-stable (1.1.1), range-pre-upper (1.2.0), vp (1.1.1), or-exact (1.0.0), or-scan (1.0.0), only-pre (install error), star-pre-latest (1.0.0), and pre-range-latest-stable (1.3.0).

…ups, and *

find_best_version (and the minimum-release-age variant) diverged from npm in
three ways:

- The exact-version fast path fired on the first comparator of the first ||
  alternative, so the rest of the group was never evaluated.
- The stable list was scanned first and prereleases only as a fallback, so a
  satisfying prerelease higher than the best satisfying stable was never
  chosen, and a latest dist-tag that satisfies a prerelease range was only
  honored if it equaled the range's left version.
- '*' never considered the latest dist-tag, so packages with only prerelease
  versions failed to resolve.

npm (npm-pick-manifest) prefers a satisfying latest dist-tag, treats '*' as
latest, and otherwise picks the highest satisfying version overall.
@robobun

robobun commented Jun 27, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 11:53 AM PT - Jun 27th, 2026

❌ @robobun, your commit d5bf62b has 1 failures in Build #65510 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 32843

That installs a local version of the PR into your bun-32843 executable, so you can run:

bun-32843 --bun

@robobun

robobun commented Jun 27, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: all three npm resolution divergences are fixed in find_best_version / find_best_version_with_filter, plus the review-caught follow-ups (literal-* parse flag in Group::eql, * dist-tag fallback under minimumReleaseAge, cross-list age-filter diagnostics). Tests: test/cli/install/bun-install-version-resolution.test.ts (15 cases, npm-verified; 10 fail on unfixed bun). Existing prerelease matrices and minimum-release-age.test.ts unchanged. All automated review threads resolved.

CI: the diff is green on every lane that ran tests. Build 65475 had 72 lanes pass, red only on a darwin-14-aarch64 job that expired without an agent and a darwin-26-aarch64 artifact-download timeout that passed on retry. Build 65510 (retrigger) had 71 lanes pass, red only on the same darwin-14-aarch64 agent expiry plus a darwin-14-x64 flake in test/js/bun/terminal/terminal.test.ts (PTY spawn 90s timeout; that lane passed on 65475 with the same code). No failure in either build touches install, semver, or the new test file. Needs a maintainer to merge or retry the darwin lanes. Supersedes #30076.

@github-actions

Copy link
Copy Markdown
Contributor

Found 3 issues this PR may fix:

  1. error: No version matching ">=0.34.0-next.12 <1.0.0" found for specifier "svelte-eslint-parser" (but package exists) #9525 - PR fixes prerelease vs stable ordering so ranges like >=0.34.0-next.12 <1.0.0 correctly find satisfying prerelease versions
  2. No version matching "" found for specifier "" (but package exists) #19065 - PR fixes * range with prerelease-only packages (nightly/canary) that fail with "No version matching" error
  3. bun add with specific version does not always install the one specified. #16108 - PR fixes the || group exact-version fast path that could short-circuit and pick the wrong version

If this is helpful, copy the block below into the PR description to auto-close these issues on merge.

Fixes #9525
Fixes #19065
Fixes #16108

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: fe2974cb-2107-4478-91b5-cea63048cfe2

📥 Commits

Reviewing files that changed from the base of the PR and between 72e3c52 and a906cdb.

📒 Files selected for processing (3)
  • src/install/npm.rs
  • src/semver/SemverQuery.rs
  • test/cli/install/bun-install-version-resolution.test.ts

Walkthrough

Refactors npm version resolution in src/install/npm.rs, adds literal * parsing support in src/semver/SemverQuery.rs, and expands install tests for prerelease, ||, *, latest-tag, minimum age, and reinstall cases.

Changes

npm version resolution

Layer / File(s) Summary
Star literal support
src/semver/SemverQuery.rs
SemverQuery marks the literal * during parsing, exposes a predicate for detecting it, and includes the star-literal flag in query equality.
Result helpers and exact match path
src/install/npm.rs
FindVersionResult::unwrap() delegates to found(), the exact-range fast path uses group.get_exact_version(), and the age-filtered scan updates the highest skipped version seen so far.
Filtered selection flow
src/install/npm.rs
find_best_version_with_filter() now handles * through latest, scans releases and prereleases with age filtering, compares prerelease and release candidates by npm ordering, and returns FoundWithFilter, AllVersionsTooRecent, or NotFound from the accumulated filtered result.
Highest-satisfying scan
src/install/npm.rs
find_best_version() now uses group.get_exact_version(), prefers a satisfying latest dist-tag, scans the version map backwards for the highest satisfying release, and applies prerelease-vs-release ordering when PRE is set.
Install version-resolution tests
test/cli/install/bun-install-version-resolution.test.ts
The new bun install --lockfile-only suite sets up an in-process registry and covers prerelease, `

Suggested reviewers

  • Jarred-Sumner
  • dylan-conway
  • alii
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: npm-compatible version resolution for prerelease ranges, || groups, and *.
Description check ✅ Passed The description covers what changed and how it was verified, though it uses different headings than the required template.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/install/npm.rs`:
- Around line 1824-1873: The `find_version`/version-selection path in
`src/install/npm.rs` drops the dist-tag `latest` candidate when it is filtered
out for age and then falls back without considering prereleases unless
`Flags::PRE` is set. Update the logic around the `group.is_star()` check and the
subsequent `search_version_list` calls so `*` continues to use the `latest`
fallback behavior even when `latest` is too recent, and ensure an older
acceptable prerelease can still be selected instead of returning
`AllVersionsTooRecent`. Keep the fix localized to the
`search_version_list`/`prereleases` selection flow and preserve the existing
`prerelease_is_higher` ordering rule.

In `@test/cli/install/bun-install-version-resolution.test.ts`:
- Around line 48-58: The install test is reading and returning lockfile-derived
resolution data even when bun install fails, which hides the real diagnostics.
In the test around the resolved map setup, guard on the exit code before any
bun.lock parsing or Object.fromEntries work, and fail the test with
stdout/stderr when the command does not succeed. Keep the early exit-code check
in the install CLI test flow so failures surface instead of producing an empty
resolved result.
- Around line 1-4: The install CLI test file is missing the standard
long-running test timeout setup, which can cause failures in slow debug/ASAN
runs. Add the established install-test default timeout at file startup in this
test module, alongside the existing imports and before the test cases, using the
same pattern used by other files under test/cli/install/ so the suite inherits
the 5-minute limit.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: af47b379-16ce-48f9-bdfe-98d7d1f570cc

📥 Commits

Reviewing files that changed from the base of the PR and between df92f8f and dbe6930.

📒 Files selected for processing (2)
  • src/install/npm.rs
  • test/cli/install/bun-install-version-resolution.test.ts

Comment thread src/install/npm.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

This PR may be a duplicate of:

  1. install: consider OR'd alternatives in find_best_version fast path #30076 - Also fixes the || group short-circuit bug in find_best_version fast path in src/install/npm.rs; PR install: match npm's version resolution for prerelease ranges, || groups, and * #32843 explicitly supersedes it with a more comprehensive fix

🤖 Generated with Claude Code

Comment thread src/install/npm.rs Resolved
Comment thread src/install/npm.rs Resolved
'>=0.0.0', '>=0', and 'x' parse to the same shape as '*' but npm does not
give them the latest dist-tag preference; track the literal in a parse flag
instead of using the structural is_star().
@robobun

robobun commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator Author

I checked each of the three suggested issues against npm 11 and this branch before adding any "Fixes" lines, and none of them is actually closed by this PR, so I am not linking them:

Comment thread src/install/npm.rs Resolved
Comment thread src/install/npm.rs Resolved
Comment thread src/install/npm.rs Resolved
…e-filtered

For '*' with minimumReleaseAge, an entirely too recent latest channel
previously returned AllVersionsTooRecent even when an old enough version
existed in the other list. Also surface the newest age-filtered version when
the two scans disagree so the minimum-release-age diagnostics stay accurate.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/install/npm.rs (1)

1860-1908: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Track the highest filtered candidate across both scans.

This block assumes newest_filtered already holds the newest filtered match overall. It doesn’t: the release scan runs first, so a filtered release can stick even when the prerelease scan later sees a higher filtered prerelease. For a range like <3.0.0-beta.2, that can leave best = 3.0.0-alpha.1 but newest_filtered = 2.5.0, so Lines 1893-1908 return Found instead of FoundWithFilter and drop the minimum-release-age signal for the actual newest satisfying version. Please merge filtered candidates by semver order instead of “first one wins.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/install/npm.rs` around lines 1860 - 1908, The version-selection logic in
the npm install search is not merging filtered candidates from both release and
prerelease scans, so `newest_filtered` can stay stuck on the first scan’s result
instead of the highest satisfying version overall. Update the
`search_version_list` / `best` handling in this block so `newest_filtered` is
compared and replaced by semver order across both scans, not “first one wins,”
and ensure the final `FoundWithFilter` decision uses the merged highest filtered
candidate from `FindVersionResult`/`found()` before returning.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/install/npm.rs`:
- Around line 1860-1908: The version-selection logic in the npm install search
is not merging filtered candidates from both release and prerelease scans, so
`newest_filtered` can stay stuck on the first scan’s result instead of the
highest satisfying version overall. Update the `search_version_list` / `best`
handling in this block so `newest_filtered` is compared and replaced by semver
order across both scans, not “first one wins,” and ensure the final
`FoundWithFilter` decision uses the merged highest filtered candidate from
`FindVersionResult`/`found()` before returning.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: b0289bb7-cfdb-4305-bc80-351ff6e8ce99

📥 Commits

Reviewing files that changed from the base of the PR and between dbe6930 and 72e3c52.

📒 Files selected for processing (3)
  • src/install/npm.rs
  • src/semver/SemverQuery.rs
  • test/cli/install/bun-install-version-resolution.test.ts

Comment thread src/semver/SemverQuery.rs Resolved
'*' and '>=0.0.0' parse to the same comparators but resolve differently, so
dependency change detection has to tell them apart.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All my earlier feedback has been addressed and this pass found nothing new, but this reworks the core bun install version-selection path (and semver query equality), so it should get a maintainer's eyes before merge.

Extended reasoning...

Overview

This PR changes how bun install selects a version from a package manifest to match npm's behavior for three classes of ranges: prerelease-bearing comparators (<1.2.0-alpha.2, <=2.0.0-rc.1), || groups whose first alternative is an exact version, and the literal *. It touches:

  • src/install/npm.rs: rewrites find_best_version and find_best_version_with_filter — the exact-version fast path now uses Group::get_exact_version(), the latest dist-tag is preferred whenever it satisfies (or unconditionally for literal *), and both the release and prerelease lists are scanned with the higher result winning. Also reworks how newest_filtered is tracked across the two scans and how the * path interacts with minimumReleaseAge.
  • src/semver/SemverQuery.rs: adds Flags::STAR set only when the input is literally *, exposes Group::is_star_literal(), and folds the flag into Group::eql so * vs >=0.0.0/x are distinguishable for lockfile diffing.
  • test/cli/install/bun-install-version-resolution.test.ts: new 15-case suite with an in-process registry, expectations cross-checked against npm 11 / npm-pick-manifest.

Security risks

None identified. There is no auth, crypto, filesystem, or network surface change here; the registry response handling is unchanged. The change is purely which already-published version gets selected from a manifest. The new test file spins up a local Bun.serve registry on port 0 in the test process, which is the established pattern in sibling install tests.

Level of scrutiny

High. find_best_version / find_best_version_with_filter run on every npm dependency in every bun install, and a wrong pick silently locks a different version than npm would — exactly the class of subtle, hard-to-bisect bug this PR is fixing. Over the course of review this PR went through four rounds of feedback from me, each finding a real issue (the structural is_star() matching >=0.0.0/x; the AllVersionsTooRecent early-return swallowing the stable fallback under minimumReleaseAge; FoundWithFilter diagnostics being dropped across the two scans; Group::eql ignoring the new STAR flag). All have been addressed with targeted fixes and regression tests, and the current pass found nothing further — but the iteration history itself argues for a maintainer reviewing the final shape rather than auto-approving.

The Group::eql change also deserves a human look: it now distinguishes * from >=0.0.0/x, which is correct for the resolution semantics introduced here but is consumed by lockfile diffing, overrides, and catalog comparison via Dependency::eql, so the blast radius is wider than the two functions in npm.rs.

Other factors

  • CI on the most recently reported build is red (robobun reports failures on Build #65471 for commit 808de80); worth confirming whether those are related or pre-existing flakes before merge.
  • Test coverage is good: the new suite pins each behavior change against npm-verified expectations, and the PR description states the existing prereleases-1..4 matrix and minimum-release-age.test.ts are unchanged.
  • Supersedes #30076 (the || fast-path fix alone), so that PR should be closed if this lands.
  • All prior automated review comments (mine and CodeRabbit's) are resolved.

@robobun

robobun commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator Author

This came up again in a semver-parity fuzz round against npm 11.16.0 / npm-pick-manifest 10.0.0 (^1.0.0-0 and >=1.0.0-rc.0 <2 skipping a satisfying latest, ^1.0.0 || 2.1.0-beta.3 picking the release over the higher prerelease, * with a prerelease latest). I wrote the same fix before finding this PR, so I am not opening a second one. The branch is rebased on current main (this PR conflicts with it now) in case it helps to carry this forward:

main...robobun/c58f647d/npm-pick-parity

Differences from this PR:

  • Resolution::satisfies_dependency_version treats a bare * as satisfied by any version, prereleases included (arborist's dep-valid does the same). Without it, a * peer on a package whose latest is a prerelease prints warn: incorrect peer dependency after the * pick changes. npm installs that case silently.
  • Under minimumReleaseAge, * only changes whether latest counts as satisfying. A too recent latest falls through to the normal range scan instead of the dist-tag fallback.
  • Test: test/cli/install/bun-install-pick-version.test.ts (3 installs, 21 ranges over 5 version sets, expectations generated with npm-pick-manifest 10.0.0).

Verification on that branch: a differential run of bun info <pkg>@<range> version against npm-pick-manifest 10.0.0 over 85 ranges x 8 version sets agrees on 680/680 cells (bun 1.4.2: 46/680 differ). minimum-release-age.test.ts, the prereleases-* matrix in bun-install-registry.test.ts, and the prerelease cases in bun-update.test.ts / bun-update-transitive.test.ts pass.

@robobun

robobun commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as part of a cleanup of stale pull requests. This PR has had no new commits since 2026-06-27, it conflicts with main, and its last CI run failed. This is not a judgment on the fix itself. If the problem still reproduces on a current build, reopen this PR after a rebase or open a new one against main.

@robobun robobun closed this Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant