Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/codegen/generate-js2native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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 @@ -74,6 +74,11 @@ export const sslCtxLiveCount = $newRustFunction("SecureContext.rs", "jsLiveCount

export const napiThreadsafeFunctionLiveCount = $newRustFunction("napi_body.rs", "jsThreadsafeFunctionLiveCount", 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
24 changes: 12 additions & 12 deletions src/semver/SemverRange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,7 @@ impl Range {
return true;
}

// If left has prerelease check if major,minor,patch matches with left. If
// not, check the same with right if right exists and has prerelease.
*pre_matched = *pre_matched
|| (self.left.version.tag.has_pre()
&& version.patch == self.left.version.patch
&& version.minor == self.left.version.minor
&& version.major == self.left.version.major)
|| (has_right
&& self.right.version.tag.has_pre()
&& version.patch == self.right.version.patch
&& version.minor == self.right.version.minor
&& version.major == self.right.version.major);
*pre_matched = *pre_matched || self.admits_pre_of(version);

if !self.left.satisfies(version, range_buf, version_buf) {
return false;
Expand All @@ -150,6 +139,17 @@ impl Range {

true
}

/// Whether a comparator has a prerelease tag on `version`'s major.minor.patch.
pub(crate) fn admits_pre_of(self, version: Version) -> bool {
[self.left, self.right].into_iter().any(|c| {
c.op != Op::Unset
&& c.version.tag.has_pre()
&& c.version.major == version.major
&& c.version.minor == version.minor
&& c.version.patch == version.patch
})
}
}

pub struct Formatter<'a> {
Expand Down
70 changes: 68 additions & 2 deletions src/semver/intersects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,76 @@ impl<'a> Interval<'a> {
_ => true,
}
}

/// Whether a version that satisfies both queries (prereleases included, per `List::satisfies_pre`) lies inside.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
fn has_version(&self, a: &Query, b: &Query) -> bool {
if !self.is_non_empty() {
return false;
}
let (Some(l), Some(u)) = (self.lower, self.upper) else {
return true;
};

// The smallest version without a prerelease tag that the lower bound admits.
let release = if l.version.tag.has_pre() {
Version {
major: l.version.major,
minor: l.version.minor,
patch: l.version.patch,
..Default::default()
}
} else if l.inclusive {
return true;
} else {
let Some(release) = next_release(l.version) else {
return false;
};
release
};

let admitted = match release.order_without_build(u.version, &[], u.buf) {
Ordering::Less => true,
Ordering::Equal => u.inclusive,
Ordering::Greater => false,
};
if admitted {
return true;
}

allows_pre_of(a, release) && allows_pre_of(b, release)
}
}

/// The smallest release above `v`'s major.minor.patch, with a carry (`>3` desugars to `>3.MAX.MAX`).
fn next_release(v: Version) -> Option<Version> {
let release = |major, minor, patch| Version {
major,
minor,
patch,
..Default::default()
};
if let Some(patch) = v.patch.checked_add(1) {
return Some(release(v.major, v.minor, patch));
}
if let Some(minor) = v.minor.checked_add(1) {
return Some(release(v.major, minor, 0));
}
v.major.checked_add(1).map(|major| release(major, 0, 0))
}

fn allows_pre_of(query: &Query, release: Version) -> bool {
let mut cur = Some(query);
while let Some(q) = cur {
if q.range.admits_pre_of(release) {
return true;
}
cur = q.next.as_deref();
}
false
}

impl Group {
/// Whether some version satisfies both groups; prerelease-exclusion rules are not modelled, comparators are compared directly.
/// Whether some version satisfies both groups.
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 {
Expand All @@ -102,7 +168,7 @@ impl Group {
b = list_b.next.as_deref();
let mut i = base;
i.and_query(&list_b.head, other_buf);
if i.is_non_empty() {
if i.has_version(&list_a.head, &list_b.head) {
return true;
}
}
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(),
)))
}
25 changes: 25 additions & 0 deletions test/cli/install/nested-overrides.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,31 @@ describe.concurrent("version-scoped targets", () => {
await installOk(dir, "--frozen-lockfile");
});

// #41371: `^1.0.0` admits no 2.0.0 prerelease, so a selector whose lower bound is one does not intersect it.
describe.concurrent.each([
{ rule: { "no-deps@>=2.0.0-alpha.0 <3": "1.0.0" } },
{ rule: { "no-deps@>=2.0.0-0": "1.0.0" } },
{ rule: { "one-range-dep": { "no-deps@>=2.0.0-alpha.0 <3": "1.0.0" } } },
])("a prerelease lower bound above the declared range does not match %j", ({ rule }) => {
test("leaves the edge alone", async () => {
const dir = await project({ dependencies: { "one-range-dep": "1.0.0" }, overrides: rule });
const { err } = await installOk(dir);
expect(err).not.toContain("warn:");
expect(await versionSeenBy(dir, "one-range-dep", "no-deps")).toBe("1.1.0");
await installOk(dir, "--frozen-lockfile");
});
});

test("a prerelease selector still matches a declared range that admits that prerelease", async () => {
const dir = await project({
dependencies: { "no-deps": ">=1.0.0 <2.0.0-beta" },
overrides: { "no-deps@>=2.0.0-alpha.0 <3": "1.0.0" },
});
await installOk(dir);
expect(await versionSeenBy(dir, undefined, "no-deps")).toBe("1.0.0");
await installOk(dir, "--frozen-lockfile");
});

test("a non-intersecting range leaves the edge alone and is still recorded", async () => {
const dir = await project({
dependencies: { "one-range-dep": "1.0.0", "one-dep": "1.0.0" },
Expand Down
Loading
Loading