Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
133 changes: 45 additions & 88 deletions src/install/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::Error;
use crate::bun_json as JSON;
use crate::bun_schema::api;
use bun_alloc::AllocError;
use bun_collections::{HashMap, IdentityContext, StringSet};
use bun_collections::{HashMap, IdentityContext};
use bun_core::{Global, Output, fmt as bun_fmt};
use bun_core::{MutableString, strings};
use bun_dotenv::Loader as DotEnv;
Expand Down Expand Up @@ -928,8 +928,10 @@ pub mod package_manifest {
// - v0.0.5: added bundled dependencies
// - v0.0.6: changed semver major/minor/patch to each use u64 instead of u32
// - v0.0.7: added version publish times and extended manifest flag for minimum release age
// - v0.0.8: bundled_deps_buf holds every name a version lists in bundleDependencies; it used
// to hold only the names matched in the last dependency group parsed
Comment thread
robobun marked this conversation as resolved.
Resolved
const HEADER_BYTES: &'static str =
concat!("#!/usr/bin/env bun\n", "bun-npm-manifest-cache-v0.0.7\n");
concat!("#!/usr/bin/env bun\n", "bun-npm-manifest-cache-v0.0.8\n");

// Field order is hardcoded (descending alignment). Re-verify if the
// layout changes.
Expand Down Expand Up @@ -1963,6 +1965,16 @@ const DEPENDENCY_GROUPS: [DependencyGroup; 3] = [
DependencyGroup::PEER,
];

/// npm accepts both spellings; `bundleDependencies` is the documented one.
fn bundled_dependencies_field(
version_obj: Option<&JSON::E::ObjectJSON>,
) -> Option<&JSON::E::JsonValue> {
let version_obj = version_obj?;
version_obj
.get(b"bundleDependencies")
.or_else(|| version_obj.get(b"bundledDependencies"))
}

impl PackageManifest {
/// This parses [Abbreviated metadata](https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md#abbreviated-metadata-format)
pub fn parse(
Expand Down Expand Up @@ -2016,9 +2028,6 @@ impl PackageManifest {
let mut version_extern_strings_dedupe_map = ExternalStringMapDeduper::default();
let mut optional_peer_dep_names: Vec<u64> = Vec::new();

let mut bundled_deps_set = StringSet::init();
let mut bundle_all_deps: bool;

let mut bundled_deps_count: usize = 0;

let mut string_builder = Semver::semver_string::Builder {
Expand Down Expand Up @@ -2151,26 +2160,15 @@ impl PackageManifest {
}
}

bundled_deps_set.map.clear_retaining_capacity();
bundle_all_deps = false;
if let Some(bundled_deps_value) = version_obj
.and_then(|o| o.get(b"bundleDependencies"))
.or_else(|| version_obj.and_then(|o| o.get(b"bundledDependencies")))
if let Some(JSON::E::JsonValue::Array(arr)) =
bundled_dependencies_field(version_obj)
{
match bundled_deps_value {
JSON::E::JsonValue::Boolean(boolean) => {
bundle_all_deps = *boolean;
}
JSON::E::JsonValue::Array(arr) => {
for bundled_dep in arr.get().items() {
let Some(s) = bundled_dep.as_str() else {
continue;
};
bundled_deps_set.insert(s)?;
}
}
_ => {}
}
bundled_deps_count += arr
.get()
.items()
.iter()
.filter(|bundled_dep| bundled_dep.as_str().is_some())
.count();
}

for pair in &DEPENDENCY_GROUPS {
Expand All @@ -2182,11 +2180,6 @@ impl PackageManifest {
dependency_sum += properties.len();
for property in properties {
let key = property.key.slice();
if !bundle_all_deps && bundled_deps_set.swap_remove(key) {
// swap remove the dependency name because it could exist in
// multiple behavior groups.
bundled_deps_count += 1;
}
string_builder.count(key);
string_builder.count(property.value.as_str().unwrap_or(b""));
}
Expand Down Expand Up @@ -2375,29 +2368,31 @@ impl PackageManifest {

let version_obj = prop.value.as_object();

bundled_deps_set.map.clear_retaining_capacity();
bundle_all_deps = false;
if let Some(bundled_deps_value) = version_obj
.and_then(|o| o.get(b"bundleDependencies"))
.or_else(|| version_obj.and_then(|o| o.get(b"bundledDependencies")))
let mut package_version: PackageVersion = empty_version;

// The list holds the names as published. `Package::from_npm`
// matches it against the dependencies of every group, so a
// name that no group declares is simply never matched.
Comment thread
robobun marked this conversation as resolved.
Resolved
package_version.bundled_dependencies = match bundled_dependencies_field(version_obj)
{
match bundled_deps_value {
JSON::E::JsonValue::Boolean(boolean) => {
bundle_all_deps = *boolean;
}
JSON::E::JsonValue::Array(arr) => {
for bundled_dep in arr.get().items() {
let Some(s) = bundled_dep.as_str() else {
continue;
};
bundled_deps_set.insert(s)?;
}
Some(JSON::E::JsonValue::Boolean(true)) => ExternalPackageNameHashList::INVALID,
Some(JSON::E::JsonValue::Array(arr)) => {
let bundled_deps_begin = bundled_deps_offset;
for bundled_dep in arr.get().items() {
let Some(name) = bundled_dep.as_str() else {
continue;
};
bundled_deps_buf[bundled_deps_offset] =
Semver::semver_string::Builder::string_hash(name);
bundled_deps_offset += 1;
}
_ => {}
ExternalPackageNameHashList::init(
&bundled_deps_buf,
&bundled_deps_buf[bundled_deps_begin..bundled_deps_offset],
)
}
}

let mut package_version: PackageVersion = empty_version;
_ => ExternalPackageNameHashList::default(),
};

if let Some(cpu) = version_obj.and_then(|o| o.get(b"cpu")) {
package_version.cpu = negatable_from_json_value::<Architecture>(cpu);
Expand Down Expand Up @@ -2620,13 +2615,7 @@ impl PackageManifest {
// For peer deps, fall through with an empty `items`
// slice when `peerDependencies` is absent so that
// `peerDependenciesMeta`-only entries (synthesised
// below) still get a build pass. The fallthrough must
// stay scoped to packages that actually have a
// `peerDependenciesMeta`: the body sets
// `package_version.bundled_dependencies` from this
// iteration's slice of `bundled_deps_buf`, so an
// unconditional empty pass would clobber the value the
// `dependencies` iteration just produced.
// below) still get a build pass.
let items: &[JSON::E::PropertyJSON] = version_obj
.and_then(|o| o.get(pair.prop))
.and_then(|deps| deps.as_object())
Expand Down Expand Up @@ -2675,8 +2664,6 @@ impl PackageManifest {
}
}

let bundled_deps_begin = bundled_deps_offset;

let mut i: usize = 0;

for item in items {
Expand All @@ -2697,15 +2684,6 @@ impl PackageManifest {
version_extern_strings[values_base + i] =
string_builder.append::<ExternalString>(version_str);

if !bundle_all_deps && bundled_deps_set.swap_remove(name_str) {
// SAFETY: bundled_deps_buf sized in counting pass
unsafe {
*bundled_deps_buf.as_mut_ptr().add(bundled_deps_offset) =
all_extern_strings[names_base + i].hash;
}
bundled_deps_offset += 1;
}

if is_peer {
if optional_peer_dep_names
.iter()
Expand Down Expand Up @@ -2805,27 +2783,6 @@ impl PackageManifest {
let this_versions =
&version_extern_strings[values_base..values_base + count];

// Bundled deps are matched against the
// `dependencies`/`optionalDependencies` groups
// only; the peer pass never adds to
// `bundled_deps_buf`. With the meta-only
// synthesis above the peer body now runs even
// when `peerDependencies` is absent, so writing
// here would clobber the value the dependencies
// pass already produced with an empty slice.
if !is_peer {
if bundle_all_deps {
package_version.bundled_dependencies =
ExternalPackageNameHashList::INVALID;
} else {
package_version.bundled_dependencies =
ExternalPackageNameHashList::init(
&bundled_deps_buf,
&bundled_deps_buf[bundled_deps_begin..bundled_deps_offset],
);
}
}

let mut name_list =
ExternalStringList::init(&all_extern_strings, this_names);
let mut version_list =
Expand Down
133 changes: 133 additions & 0 deletions test/cli/install/bun-install-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,139 @@ describe("bundledDependencies", () => {
await check();
});

test(`(${textLockfile ? "bun.lock" : "bun.lockb"}) bundled names from dependencies and optionalDependencies`, async () => {
// bundled-with-optional@1.0.0 has dependencies { no-deps }, optionalDependencies { a-dep, basic-1 }
// and bundleDependencies [no-deps, a-dep, not-a-dependency]. Its tarball ships no-deps and a-dep.
// Only basic-1 should be installed from the registry; not-a-dependency is declared in no group
// (and does not exist in the registry), so listing it must have no effect.
await write(
packageJson,
JSON.stringify({
name: "bundled-mixed-groups",
dependencies: {
"bundled-with-optional": "1.0.0",
},
}),
);

await runBunInstall(env, packageDir, { saveTextLockfile: textLockfile });

async function check() {
expect(await readdirSorted(join(packageDir, "node_modules"))).toEqual(["basic-1", "bundled-with-optional"]);
const bundledNodeModules = join(packageDir, "node_modules", "bundled-with-optional", "node_modules");
expect(
await Promise.all([
file(join(bundledNodeModules, "no-deps", "package.json")).json(),
file(join(bundledNodeModules, "a-dep", "package.json")).json(),
]),
).toEqual([
{ name: "no-deps", version: "1.0.0" },
{ name: "a-dep", version: "1.0.1" },
]);
}

await check();

let lockfile: string | undefined;
if (textLockfile) {
lockfile = await file(join(packageDir, "bun.lock")).text();
expect(lockfile.replaceAll(/localhost:\d+/g, "localhost:1234")).toMatchInlineSnapshot(`
"{
"lockfileVersion": 2,
"configVersion": 1,
"workspaces": {
"": {
"name": "bundled-mixed-groups",
"dependencies": {
"bundled-with-optional": "1.0.0",
},
},
},
"packages": {
"basic-1": ["basic-1@1.0.0", "http://localhost:1234/basic-1/-/basic-1-1.0.0.tgz", {}, "sha512-NW5qBU1Kn7DzCjfVfnAbBBRGuQ7krbBtrnezZwOXutA9NvrCT4SI4EJMog3AGsNeK/1OygErysF8RN/FqDYunA=="],

"bundled-with-optional": ["bundled-with-optional@1.0.0", "http://localhost:1234/bundled-with-optional/-/bundled-with-optional-1.0.0.tgz", { "dependencies": { "no-deps": "1.0.0" }, "optionalDependencies": { "a-dep": "1.0.1", "basic-1": "1.0.0" } }, "sha512-rq4Jtdsk53QE4T00/KaOP5lyv3d8Gnt4VABtara3PFos+8POKMkRIQPYCXVRRBey7K+ttzkiA5MkHhe+T19eYA=="],

"bundled-with-optional/a-dep": ["a-dep@1.0.1", "http://localhost:1234/a-dep/-/a-dep-1.0.1.tgz", { "bundled": true }, "sha512-6nmTaPgO2U/uOODqOhbjbnaB4xHuZ+UB7AjKUA3g2dT4WRWeNxgp0dC8Db4swXSnO5/uLLUdFmUJKINNBO/3wg=="],

"bundled-with-optional/no-deps": ["no-deps@1.0.0", "http://localhost:1234/no-deps/-/no-deps-1.0.0.tgz", { "bundled": true }, "sha512-v4w12JRjUGvfHDUP8vFDwu0gUWu04j0cv9hLb1Abf9VdaXu4XcrddYFTMVBVvmldKViGWH7jrb6xPJRF0wq6gw=="],
}
}
"
`);
}

// The lockfile must record the bundled edges too: a cold install from it
// must not pull no-deps or a-dep from the registry either.
await rm(join(packageDir, "node_modules"), { recursive: true, force: true });
await runBunInstall(env, packageDir, { frozenLockfile: true });

await check();
if (textLockfile) {
expect(await file(join(packageDir, "bun.lock")).text()).toBe(lockfile!);
}
});

test(`(${textLockfile ? "bun.lock" : "bun.lockb"}) bundled name declared in peerDependencies`, async () => {
// bundled-peer@1.0.0 has peerDependencies { no-deps } and bundleDependencies [no-deps], and its
// tarball ships no-deps. The peer is satisfied by the bundled copy, so nothing else is installed.
await write(
packageJson,
JSON.stringify({
name: "bundled-peer-root",
dependencies: {
"bundled-peer": "1.0.0",
},
}),
);

await runBunInstall(env, packageDir, { saveTextLockfile: textLockfile });

async function check() {
expect(await readdirSorted(join(packageDir, "node_modules"))).toEqual(["bundled-peer"]);
expect(
await file(
join(packageDir, "node_modules", "bundled-peer", "node_modules", "no-deps", "package.json"),
).json(),
).toEqual({ name: "no-deps", version: "1.0.0" });
}

await check();

let lockfile: string | undefined;
if (textLockfile) {
lockfile = await file(join(packageDir, "bun.lock")).text();
expect(lockfile.replaceAll(/localhost:\d+/g, "localhost:1234")).toMatchInlineSnapshot(`
"{
"lockfileVersion": 2,
"configVersion": 1,
"workspaces": {
"": {
"name": "bundled-peer-root",
"dependencies": {
"bundled-peer": "1.0.0",
},
},
},
"packages": {
"bundled-peer": ["bundled-peer@1.0.0", "http://localhost:1234/bundled-peer/-/bundled-peer-1.0.0.tgz", { "peerDependencies": { "no-deps": "1.0.0" } }, "sha512-rmaahOTDrcPuhmBskBLcKEEfTotY+xH6bDPnQopcNlmdePtEvAbAehSpK8JQAegpBir4omjiFqBy1LFqXQBfgA=="],

"bundled-peer/no-deps": ["no-deps@1.0.0", "http://localhost:1234/no-deps/-/no-deps-1.0.0.tgz", { "bundled": true }, "sha512-v4w12JRjUGvfHDUP8vFDwu0gUWu04j0cv9hLb1Abf9VdaXu4XcrddYFTMVBVvmldKViGWH7jrb6xPJRF0wq6gw=="],
}
}
"
`);
}

await rm(join(packageDir, "node_modules"), { recursive: true, force: true });
await runBunInstall(env, packageDir, { frozenLockfile: true });

await check();
if (textLockfile) {
expect(await file(join(packageDir, "bun.lock")).text()).toBe(lockfile!);
}
});

test(`(${textLockfile ? "bun.lock" : "bun.lockb"}) git dependencies`, async () => {
await Promise.all([
write(
Expand Down
Binary file not shown.
44 changes: 44 additions & 0 deletions test/cli/install/registry/packages/bundled-peer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "bundled-peer",
"versions": {
"1.0.0": {
"name": "bundled-peer",
"version": "1.0.0",
"peerDependencies": {
"no-deps": "1.0.0"
},
"bundleDependencies": [
"no-deps"
],
"_id": "bundled-peer@1.0.0",
"integrity": "sha512-rmaahOTDrcPuhmBskBLcKEEfTotY+xH6bDPnQopcNlmdePtEvAbAehSpK8JQAegpBir4omjiFqBy1LFqXQBfgA==",
"shasum": "32b15c8ab065db1e6b9383043f0dd002077cb65d",
"dist": {
"integrity": "sha512-rmaahOTDrcPuhmBskBLcKEEfTotY+xH6bDPnQopcNlmdePtEvAbAehSpK8JQAegpBir4omjiFqBy1LFqXQBfgA==",
"shasum": "32b15c8ab065db1e6b9383043f0dd002077cb65d",
"tarball": "http://localhost:4873/bundled-peer/-/bundled-peer-1.0.0.tgz"
},
"contributors": []
}
},
"time": {
"modified": "2024-12-30T18:14:22.143Z",
"created": "2024-12-30T18:14:22.143Z",
"1.0.0": "2024-12-30T18:14:22.143Z"
},
"users": {},
"dist-tags": {
"latest": "1.0.0"
},
"_uplinks": {},
"_distfiles": {},
"_attachments": {
"bundled-peer-1.0.0.tgz": {
"shasum": "32b15c8ab065db1e6b9383043f0dd002077cb65d",
"version": "1.0.0"
}
},
"_rev": "",
"_id": "bundled-peer",
"readme": ""
}
Binary file not shown.
Loading