diff --git a/src/codegen/generate-js2native.ts b/src/codegen/generate-js2native.ts index 4158fb5bbeb7..8d8dde98fa23 100644 --- a/src/codegen/generate-js2native.ts +++ b/src/codegen/generate-js2native.ts @@ -52,6 +52,7 @@ const rustIdentifierPaths: Record = { "Listener.rs": "runtime/socket/Listener.rs", "MarkdownObject.rs": "runtime/api/MarkdownObject.rs", "SecureContext.rs": "runtime/api/bun/SecureContext.rs", + "SemverObject.rs": "semver_jsc/SemverObject.rs", "Stat.rs": "runtime/node/Stat.rs", "bindgen_test.rs": "jsc/bindgen_test.rs", "collections/linear_fifo.rs": "collections/linear_fifo.rs", diff --git a/src/js/internal-for-testing.ts b/src/js/internal-for-testing.ts index 203b2d3d7a20..fbcd0e601988 100644 --- a/src/js/internal-for-testing.ts +++ b/src/js/internal-for-testing.ts @@ -76,6 +76,11 @@ export const napiThreadsafeFunctionLiveCount = $newRustFunction("napi_body.rs", export const bundlerWorkerLiveCount: () => number = $newRustFunction("JSBundler.rs", "jsWorkerLiveCount", 0); +export const semverIntersects = $newRustFunction("SemverObject.rs", "intersectsForTesting", 2) as ( + left: string, + right: string, +) => boolean; + export const escapeRegExp = $newRustFunction("escapeRegExp.rs", "jsEscapeRegExp", 1); export const escapeRegExpForPackageNameMatching = $newRustFunction( "escapeRegExp.rs", diff --git a/src/runtime/dispatch_js2native.rs b/src/runtime/dispatch_js2native.rs index 6b38b97e2e04..5b566eaba2bb 100644 --- a/src/runtime/dispatch_js2native.rs +++ b/src/runtime/dispatch_js2native.rs @@ -43,6 +43,8 @@ pub use bun_jsc::bun_string_jsc::js_escape_reg_exp as string_escape_reg_exp_js_e pub use bun_jsc::bun_string_jsc::js_escape_reg_exp_for_package_name_matching as string_escape_reg_exp_js_escape_reg_exp_for_package_name_matching; pub use bun_jsc::bun_string_jsc::unicode_testing_apis::to_utf16_alloc_sentinel as bun_core_string_immutable_unicode_testing_ap_is_to_utf16_alloc_sentinel; +pub use bun_semver_jsc::SemverObject::intersects_for_testing as semver_jsc_semver_object_intersects_for_testing; + pub use bun_patch_jsc::testing::patch_apply as patch_patch_testing_ap_is_apply; pub use bun_patch_jsc::testing::patch_make_diff as patch_patch_testing_ap_is_make_diff; pub use bun_patch_jsc::testing::patch_parse as patch_patch_testing_ap_is_parse; diff --git a/src/semver/intersects.rs b/src/semver/intersects.rs index 736fe6905318..02fdccc90999 100644 --- a/src/semver/intersects.rs +++ b/src/semver/intersects.rs @@ -86,12 +86,56 @@ impl<'a> Interval<'a> { } } +impl Query { + /// The version when this AND chain is one `=version` comparator. + fn exact_version(&self) -> Option { + let range = &self.range; + if self.next.is_none() && range.has_left() && range.left.op == Op::Eql && !range.has_right() + { + return Some(range.left.version); + } + None + } + + /// Whether this AND chain is `*` (a lone `>=0.0.0`), which every version satisfies. `>=0.0.0-0` is not `*`. + fn admits_all(&self) -> bool { + let range = &self.range; + self.next.is_none() + && range.left.op == Op::Gte + && range.left.version.is_zero() + && !range.left.version.tag.has_pre() + && !range.has_right() + } + + /// `Group::satisfies` for one AND chain, with the same prerelease rule. `*` admits a prerelease too. + fn admits(&self, version: Version, query_buf: &[u8], version_buf: &[u8]) -> bool { + if self.admits_all() { + true + } else if version.tag.has_pre() { + let mut pre_matched = false; + self.satisfies_pre(version, query_buf, version_buf, &mut pre_matched) && pre_matched + } else { + self.satisfies(version, query_buf, version_buf) + } + } +} + impl Group { - /// Whether some version satisfies both groups; prerelease-exclusion rules are not modelled, comparators are compared directly. + /// Whether some version satisfies both groups; an exact version goes through `satisfies`, two ranges compare bounds only. pub fn intersects(&self, self_buf: &[u8], other: &Group, other_buf: &[u8]) -> bool { let mut a = Some(&self.head); while let Some(list_a) = a { a = list_a.next.as_deref(); + if let Some(version) = list_a.head.exact_version() { + let mut b = Some(&other.head); + while let Some(list_b) = b { + b = list_b.next.as_deref(); + if list_b.head.admits(version, other_buf, self_buf) { + return true; + } + } + continue; + } let mut base = Interval::default(); base.and_query(&list_a.head, self_buf); if !base.is_non_empty() { @@ -100,6 +144,12 @@ impl Group { let mut b = Some(&other.head); while let Some(list_b) = b { b = list_b.next.as_deref(); + if let Some(version) = list_b.head.exact_version() { + if list_a.head.admits(version, self_buf, other_buf) { + return true; + } + continue; + } let mut i = base; i.and_query(&list_b.head, other_buf); if i.is_non_empty() { diff --git a/src/semver_jsc/SemverObject.rs b/src/semver_jsc/SemverObject.rs index 3b966d024499..3e03dcc58aab 100644 --- a/src/semver_jsc/SemverObject.rs +++ b/src/semver_jsc/SemverObject.rs @@ -111,3 +111,37 @@ fn satisfies(global: &JSGlobalObject, frame: &CallFrame) -> JsResult { left.slice(), ))) } + +/// `bun:internal-for-testing`: `Group::intersects` on two range strings. +#[bun_jsc::host_fn] +pub fn intersects_for_testing(global: &JSGlobalObject, frame: &CallFrame) -> JsResult { + let arguments = frame.arguments(); + if arguments.len() < 2 { + return Err(global.throw(format_args!("Expected two arguments"))); + } + + let left_view = arguments[0].to_js_string_view(global)?; + let right_view = arguments[1].to_js_string_view(global)?; + + let left = left_view.to_utf8(); + let right = right_view.to_utf8(); + + let left_group = + match query::parse(left.slice(), SlicedString::init(left.slice(), left.slice())) { + Ok(g) => g, + Err(_) => return Err(global.throw_out_of_memory()), + }; + let right_group = match query::parse( + right.slice(), + SlicedString::init(right.slice(), right.slice()), + ) { + Ok(g) => g, + Err(_) => return Err(global.throw_out_of_memory()), + }; + + Ok(JSValue::js_boolean(left_group.intersects( + left.slice(), + &right_group, + right.slice(), + ))) +} diff --git a/test/cli/install/nested-overrides.test.ts b/test/cli/install/nested-overrides.test.ts index 9308eb302438..be5e0846e1bd 100644 --- a/test/cli/install/nested-overrides.test.ts +++ b/test/cli/install/nested-overrides.test.ts @@ -824,6 +824,43 @@ describe.concurrent("version-scoped targets", () => { `); }); + // #43138. An edge pinned at an exact prerelease matches a ranged selector only when the + // prerelease satisfies it, which needs a comparator with a prerelease of the same + // major.minor.patch. `<1.0.0` has none, so the rule leaves the edge alone, as in npm. + // `>=1.0.0-0 <1.0.0` applies because `Bun.semver.satisfies` admits the prerelease (npm + // tests each comparator alone and does not). `*` matches every edge, as in npm, and + // `>=0.0.0` parses to the same range (npm reads it as a comparator and does not match). + describe.concurrent.each([ + { declared: "1.0.0-rc.1", selector: "<1.0.0", applies: false }, + { declared: "1.0.0-rc.1 || 1.0.0-rc.0", selector: "<1.0.0", applies: false }, + { declared: "1.0.0-rc.1", selector: ">=1.0.0-0 <1.0.0", applies: true }, + { declared: "1.0.0-rc.1", selector: "1.0.0-rc.1", applies: true }, + { declared: "1.0.0-rc.1", selector: "*", applies: true }, + { declared: "1.0.0-rc.1", selector: ">=0.0.0", applies: true }, + ])("an exact prerelease edge and a ranged selector %j", ({ declared, selector, applies }) => { + test(applies ? "the rule applies" : "the rule leaves the edge alone", async () => { + const dir = await project({ + dependencies: { "no-deps-tags": declared }, + overrides: { [`no-deps-tags@${selector}`]: "1.0.0" }, + }); + const { err } = await installOk(dir); + expect(err).not.toContain("warn:"); + expect(await versionSeenBy(dir, undefined, "no-deps-tags")).toBe(applies ? "1.0.0" : "1.0.0-rc.1"); + await installOk(dir, "--frozen-lockfile"); + }); + }); + + test("an exact prerelease selector matches a declared range only when the range admits it", async () => { + const dir = await project({ + dependencies: { "no-deps": "<2" }, + overrides: { "no-deps@2.0.0-alpha.0": "1.0.0" }, + }); + const { err } = await installOk(dir); + expect(err).not.toContain("warn:"); + expect(await versionSeenBy(dir, undefined, "no-deps")).toBe("1.1.0"); + await installOk(dir, "--frozen-lockfile"); + }); + test("two matching ranged rules: the range text that sorts first wins", async () => { const dir = await project({ dependencies: { "one-range-dep": "1.0.0" }, diff --git a/test/cli/install/semver.test.ts b/test/cli/install/semver.test.ts index f5de5d394b5e..3443f671cbc3 100644 --- a/test/cli/install/semver.test.ts +++ b/test/cli/install/semver.test.ts @@ -14,6 +14,7 @@ // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR // IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +import { semverIntersects } from "bun:internal-for-testing"; import { bunEnv, bunExe } from "harness"; import { unsortedPrereleases } from "./semver-fixture.js"; const { satisfies, order } = Bun.semver; @@ -811,6 +812,276 @@ describe("Bun.semver.satisfies()", () => { }); }); +// `Group::intersects` (src/semver/intersects.rs) decides whether a ranged `overrides` key applies to a dependency edge. +// `*` (which `>=0.0.0` also parses to) meets everything. Between two ranges it compares the bounds by semver +// order and does not model the prerelease rule, so the rows here avoid a prerelease bound on both sides. An +// exact version, alone or as one `||` branch, meets a range when it satisfies it. The table below this one +// checks that against `Bun.semver.satisfies` (#43138). +describe("range intersection", () => { + const cases: [string, string, boolean][] = [ + // exact versions + ["1.0.0", "1.0.0", true], + ["1.0.0", "1.0.1", false], + ["1.0.0", "^1.0.0", true], + ["1.0.0", "~1.0.0", true], + ["2.0.0", "^1.0.0", false], + ["1.0.0", ">=1.0.0", true], + ["1.0.0", ">1.0.0", false], + ["1.0.0", "<=1.0.0", true], + ["1.0.0", "<1.0.0", false], + ["1.0.0", "1.0.0 || 2.0.0", true], + ["3.0.0", "1.0.0 || 2.0.0", false], + ["1.2.3", "1", true], + ["1.2.3", "1.2", true], + ["1.2.3", "1.3", false], + ["1.2.3", "1.x", true], + ["1.2.3", "1.2.x", true], + ["1.2.3", "1.3.x", false], + ["1.2.3", "*", true], + ["1.2.3", "1.0.0 - 2.0.0", true], + ["2.0.0", "1.0.0 - 2.0.0", true], + ["2.0.1", "1.0.0 - 2.0.0", false], + ["0.9.9", "1.0.0 - 2.0.0", false], + + // caret + ["^1.0.0", "^1.2.0", true], + ["^1.0.0", "^2.0.0", false], + ["^1.0.0", "^1.0.0", true], + ["^0.1.0", "^0.2.0", false], + ["^0.1.0", "^0.1.5", true], + ["^0.0.1", "^0.0.2", false], + ["^0.0.1", "^0.0.1", true], + ["^1.2.3", "1.2.3", true], + ["^1.2.3", "1.2.2", false], + ["^1.2.3", "2.0.0", false], + ["^1.2.3", "1.9.9", true], + + // tilde + ["~1.2.0", "~1.3.0", false], + ["~1.2.0", "~1.2.5", true], + ["~1.2.0", "^1.0.0", true], + ["~1.2.0", "1.3.0", false], + ["~1.2.0", "1.2.9", true], + ["~1.2", "~1.3", false], + ["~1", "~1.9", true], + ["~1", "~2", false], + ["~0.1.0", "~0.2.0", false], + + // plain comparators + [">=1.0.0 <2.0.0", ">=2.0.0", false], + [">=1.0.0 <=2.0.0", ">=2.0.0", true], + [">=1.0.0 <2.0.0", ">=1.9.0", true], + [">1.0.0", "<1.0.0", false], + [">1.0.0", "<=1.0.0", false], + [">=1.0.0", "<=1.0.0", true], + [">=1.0.0", "<1.0.0", false], + ["<1.0.0", "<2.0.0", true], + [">1", ">2", true], + [">=1.0.0", ">=5.0.0", true], + ["<1.0.0", ">=0.0.1", true], + ["<1.0.0", ">=0.0.1 <0.5.0", true], + [">=1.0.0 <1.5.0", ">=1.5.0 <2.0.0", false], + [">=1.0.0 <=1.5.0", ">=1.5.0 <2.0.0", true], + [">=1.0.0 <1.5.0", ">1.5.0 <2.0.0", false], + [">1.0.0 <=1.0.1", "*", true], + [">1.0.0 <=1.0.1", "1.0.1", true], + [">1.0.0 <1.0.1", "1.0.0", false], + [">1.0.0 <1.0.1", "1.0.1", false], + [">=1.0.0 <1.0.0", "*", false], + [">=1.0.0 <1.0.0", "1.0.0", false], + ["<1.0.0 >=1.0.0", ">=0.0.0", false], + [">2.0.0 <1.0.0", "* || ^1.0.0", false], + + // partial versions after a comparator: `>3` is `>3.MAX.MAX`, `<=1` is `<=1.MAX.MAX` + [">3", "3.5.0", false], + [">3", "4.0.0", true], + [">3", "^4", true], + [">3.0", "^3.0", true], + [">3.0", "3.1.0", true], + [">3.0", "3.0.9", false], + [">3.0", "~3.1", true], + [">1 <3", "^2", true], + [">1 <=2", "*", true], + ["<=1", "^1", true], + ["<=1", "^2", false], + ["<=1", "2.0.0-0", false], + ["<1", "^1", false], + ["<1", "^0.9", true], + [">=1", "^1", true], + [">=2", "^1", false], + + // x-ranges and wildcards + ["1.x", "2.x", false], + ["1.x", "1.2.x", true], + ["1.2.x", "1.3.x", false], + ["1.2.x", "1.2.x", true], + ["1", "1.2.3", true], + ["1", "2.0.0", false], + ["1", "2", false], + ["1", "1.2", true], + ["*", "1.0.0", true], + ["*", "^3.0.0", true], + ["*", "*", true], + ["*", "2.0.0-0", true], + [">=0.0.0", "2.0.0-0", true], + [">=0.0.0-0", "1.0.0-rc.1", false], + [">=0.0.0-0", "0.0.0-1", true], + ["* || ^1.0.0", "2.0.0-beta.0", true], + [">=0.0.0 || 1.0.0", "2.0.0-beta.0", true], + ["1.0.0-rc.1 || *", "<1.0.0", true], + ["x", "2.x", true], + ["1.X", "1.2.3", true], + ["1.*", "2.0.0", false], + + // hyphen ranges + ["1.0.0 - 2.0.0", "2.0.0", true], + ["1.0.0 - 2.0.0", "2.0.1", false], + ["1.0.0 - 1.5.0", "1.6.0 - 2.0.0", false], + ["1.0.0 - 1.5.0", "1.5.0 - 2.0.0", true], + ["1.0.0 - 1.5.0", "^1.5.0", true], + ["1.0.0 - 1.5.0", "^1.5.1", false], + ["1.0.0 - 1.5.0", "~1.4.0", true], + ["1.0.0 - 1.5.0", "~1.6.0", false], + ["1.2 - 1.4", "1.4.9", true], + ["1.2 - 1.4", "1.5.0", false], + ["1.2 - 1.4", "1.1.9", false], + + // || lists + ["1.0.0 || 2.0.0", "2.0.0", true], + ["1.0.0 || 2.0.0", "3.0.0", false], + ["^1 || ^2", "^3", false], + ["^1 || ^3", "^2 || ^3", true], + ["^1 || ^3", "^2 || ^4", false], + ["<1 || >3", "^2", false], + ["<1 || >3", ">=3", true], + ["<1 || >3", "^1 || ^4", true], + ["1.0.0 || 1.0.1", "1.0.1 || 1.0.2", true], + ["1.0.0 || 1.0.1", "1.0.2 || 1.0.3", false], + ["^1.0.0 || ^2.0.0", ">=1.5.0 <2.5.0", true], + ["^1.0.0 || ^3.0.0", ">=2.0.0 <3.0.0", false], + + // exact prerelease versions + ["1.0.0-rc.1", "1.0.0-rc.1", true], + ["1.0.0-rc.1", "1.0.0-rc.2", false], + ["1.0.0-rc.1", "1.0.0", false], + ["1.0.0-rc.1", ">=1.0.0-rc.1", true], + ["1.0.0-rc.1", ">1.0.0-rc.1", false], + ["1.0.0-rc.1", ">=1.0.0-rc.0", true], + ["1.0.0-rc.1", "<1.0.0-rc.1", false], + ["1.0.0-rc.1", "<=1.0.0-rc.1", true], + ["1.0.0-rc.1", "<1.0.0", false], + ["1.0.0-rc.1", "^1.0.0", false], + ["1.0.0-rc.1", "^1.0.0-rc.0", true], + ["1.0.0-rc.1", "^1.0.0-rc.2", false], + ["1.0.0-rc.1", "^0.9.0", false], + ["1.0.0-rc.1", "*", true], + ["1.0.0-rc.1", ">=0.0.0", true], + ["2.0.0-beta", "^2.0.0", false], + ["2.0.0-beta", "^2.0.0-alpha", true], + ["2.0.0-beta", "^1.0.0 || ^2.0.0-alpha", true], + ["2.0.0-beta", "^1.0.0 || ^2.0.0", false], + ["1.0.0-alpha.1", "1.0.0-alpha.1 || 1.0.0-alpha.2", true], + ["1.0.0-alpha.3", "1.0.0-alpha.1 || 1.0.0-alpha.2", false], + ["1.0.0-alpha", "1.0.0-alpha.1", false], + ["1.0.0-alpha", ">=1.0.0-alpha <1.0.0-alpha.1", true], + + // an exact version inside a || list is held to the same rule as a lone one + ["9.0.0-beta.0 || 9.0.0-beta.1", "<9.0.0", false], + ["9.0.0-beta.0 || 9.0.0", "<9.0.1", true], + ["1.0.0-rc.1 || 1.0.0", ">=1.0.0-0 <1.0.0", true], + ["^1.0.0 || 2.0.0-beta", "<2.0.0", true], + ["^2.0.0 || 1.0.0-rc.1", ">=0.9.0 <1.0.0", false], + ["^2.0.0 || 1.0.0-rc.1", ">=1.0.0-rc.0 <1.0.0", true], + ["3.0.0-pre || ^1.0.0", "^3.0.0 || ^4.0.0", false], + ["3.0.0-pre || ^1.0.0", "^3.0.0-0 || ^4.0.0", true], + ]; + + test.each(cases)("%s and %s: %p", (left, right, expected) => { + expect(semverIntersects(left, right)).toBe(expected); + expect(semverIntersects(right, left)).toBe(expected); + }); + + // An exact version is a range with one member, so intersecting it with a range is `satisfies`. A prerelease + // version therefore satisfies `=x` only through a comparator that names a prerelease of its own + // major.minor.patch. node-semver tests an exact version against each comparator on its own, which differs + // for a key like `>=1.0.0-0 <1.0.0`: `satisfies` admits `1.0.0-rc.1`, node-semver's `intersects` does not. + // Bun follows `satisfies` so that an override key means what `Bun.semver.satisfies` says. `*` is not in + // this list: it meets every version, and `satisfies` rejects a prerelease against it. + const versions = [ + "0.0.0", + "0.0.1", + "0.1.0", + "0.9.9", + "1.0.0", + "1.0.1", + "1.2.3", + "1.9.9", + "2.0.0", + "2.0.1", + "3.0.0", + "1.0.0-0", + "1.0.0-alpha", + "1.0.0-alpha.1", + "1.0.0-beta", + "1.0.0-rc.1", + "1.2.3-beta.2", + "2.0.0-0", + "2.0.0-alpha.0", + "2.0.0-rc.1", + "3.0.0-pre", + ]; + const ranges = [ + "1", + "1.2", + "1.x", + "1.2.x", + "^1.0.0", + "^1.2.3", + "^0.1.0", + "^0.0.1", + "~1.2.3", + "~1.0.0", + "~0.1.0", + ">=1.0.0", + ">1.0.0", + "<2.0.0", + "<=2.0.0", + ">=1.0.0 <2.0.0", + ">1.0.0 <=2.0.0", + "1.0.0 - 2.0.0", + "1.0.0 || 2.0.0", + "^1 || ^3", + "<1 || >=2", + ">=1.0.0-0 <2", + ">=1.0.0-alpha <1.0.0", + ">=1.0.0-alpha.1 <1.0.0-beta", + ">=1.0.0-beta <=2.0.0-rc.1", + "^1.0.0-alpha", + "^1.0.0-rc.1", + "~1.2.3-beta.1", + "^2.0.0-0", + ">=2.0.0-alpha.0 <3", + ">=2.0.0-rc.1", + "<2.0.0-rc.1", + ">1", + ">1 <3", + "<=1", + ">0.9 <2.0.0-rc.1", + "1.0.0-alpha || 2.0.0-rc.1", + "2.0.0-0", + "1.0.0-alpha", + ]; + test.each(versions)("an exact version %s intersects a range when it satisfies it", version => { + const got = Object.fromEntries( + ranges.map(range => [range, [semverIntersects(version, range), semverIntersects(range, version)]]), + ); + const want = Object.fromEntries( + ranges.map(range => [range, [satisfies(version, range), satisfies(version, range)]]), + ); + expect(got).toEqual(want); + }); +}); + 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({