Skip to content
1 change: 1 addition & 0 deletions src/codegen/generate-js2native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const rustIdentifierPaths: Record<string, string> = {
"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",
Expand Down
5 changes: 5 additions & 0 deletions src/js/internal-for-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/dispatch_js2native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 51 additions & 1 deletion src/semver/intersects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Version> {
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()
}
Comment thread
robobun marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved

/// `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() {
Expand All @@ -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() {
Expand Down
34 changes: 34 additions & 0 deletions src/semver_jsc/SemverObject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,37 @@ fn satisfies(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> {
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<JSValue> {
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(),
)))
}
37 changes: 37 additions & 0 deletions test/cli/install/nested-overrides.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
Loading
Loading