From cfb5799bd26dead3230d0320b4a6ee67f5fe94ee Mon Sep 17 00:00:00 2001 From: RiskyMH Date: Mon, 13 Jan 2025 22:06:53 +0100 Subject: [PATCH 1/4] allow `bundledDependencies: true` in `bun pm pack` --- src/cli/pack_command.zig | 45 ++++++++++++++++++++++++------- test/cli/install/bun-pack.test.ts | 41 ++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/cli/pack_command.zig b/src/cli/pack_command.zig index 8918c2123314bc..3352476929f282 100644 --- a/src/cli/pack_command.zig +++ b/src/cli/pack_command.zig @@ -835,22 +835,47 @@ pub const PackCommand = struct { const bundled_deps = json.get(field) orelse return null; invalid_field: { - var iter = bundled_deps.asArray() orelse switch (bundled_deps.data) { - .e_array => return .{}, + switch (bundled_deps.data) { + .e_array => { + var iter = bundled_deps.asArray() orelse return .{}; + + while (iter.next()) |bundled_dep_item| { + const bundled_dep = try bundled_dep_item.asStringCloned(allocator) orelse break :invalid_field; + try deps.append(allocator, .{ + .name = bundled_dep, + .from_root_package_json = true, + }); + } + }, + .e_boolean => { + const b = bundled_deps.asBool() orelse return .{}; + if (!b == true) return .{}; + + if (json.get("dependencies")) |dependencies_expr| { + switch (dependencies_expr.data) { + .e_object => |dependencies| { + for (dependencies.properties.slice()) |*dependency| { + if (dependency.key == null) continue; + if (dependency.value == null) continue; + + const bundled_dep = try dependency.key.?.asStringCloned(allocator) orelse break :invalid_field; + try deps.append(allocator, .{ + .name = bundled_dep, + .from_root_package_json = true, + }); + } + }, + else => {}, + } + } + }, else => break :invalid_field, - }; - while (iter.next()) |bundled_dep_item| { - const bundled_dep = try bundled_dep_item.asStringCloned(allocator) orelse break :invalid_field; - try deps.append(allocator, .{ - .name = bundled_dep, - .from_root_package_json = true, - }); } return deps; } - Output.errGeneric("expected `{s}` to be an array of strings", .{field}); + Output.errGeneric("expected `{s}` to be an boolean or array of strings", .{field}); Global.crash(); } diff --git a/test/cli/install/bun-pack.test.ts b/test/cli/install/bun-pack.test.ts index 37b3d09563b820..a6916c579ee045 100644 --- a/test/cli/install/bun-pack.test.ts +++ b/test/cli/install/bun-pack.test.ts @@ -602,6 +602,47 @@ describe("bundledDependnecies", () => { }); } + test(`basic (bundledDependencies: true)`, async () => { + await Promise.all([ + write( + join(packageDir, "package.json"), + JSON.stringify({ + name: "pack-bundled", + version: "4.4.4", + dependencies: { + "dep1": "1.1.1", + }, + devDependencies: { + "dep2": "1.1.1", + }, + bundledDependencies: true, + }), + ), + write( + join(packageDir, "node_modules", "dep1", "package.json"), + JSON.stringify({ + name: "dep1", + version: "1.1.1", + }), + ), + write( + join(packageDir, "node_modules", "dep2", "package.json"), + JSON.stringify({ + name: "dep2", + version: "1.1.1", + }), + ), + ]); + + await pack(packageDir, bunEnv); + + const tarball = readTarball(join(packageDir, "pack-bundled-4.4.4.tgz")); + expect(tarball.entries).toMatchObject([ + { "pathname": "package/package.json" }, + { "pathname": "package/node_modules/dep1/package.json" }, + ]); + }); + test("resolve dep of bundled dep", async () => { // Test that a bundled dep can have it's dependencies resolved without // needing to add them to `bundledDependencies`. Also test that only From d39a490126455370bddd76224fc57ca45cccdcb1 Mon Sep 17 00:00:00 2001 From: Michael H Date: Tue, 14 Jan 2025 08:20:39 +1100 Subject: [PATCH 2/4] Update src/cli/pack_command.zig Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> --- src/cli/pack_command.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/pack_command.zig b/src/cli/pack_command.zig index 3352476929f282..2a2d971d0e627d 100644 --- a/src/cli/pack_command.zig +++ b/src/cli/pack_command.zig @@ -875,7 +875,7 @@ pub const PackCommand = struct { return deps; } - Output.errGeneric("expected `{s}` to be an boolean or array of strings", .{field}); + Output.errGeneric("expected `{s}` to be a boolean or an array of strings", .{field}); Global.crash(); } From 9f4d3031e0afd3b7121eb216d5a4bcdf54bf1237 Mon Sep 17 00:00:00 2001 From: RiskyMH Date: Mon, 13 Jan 2025 22:25:45 +0100 Subject: [PATCH 3/4] more tests --- test/cli/install/bun-pack.test.ts | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/cli/install/bun-pack.test.ts b/test/cli/install/bun-pack.test.ts index a6916c579ee045..5d35031a07b7bf 100644 --- a/test/cli/install/bun-pack.test.ts +++ b/test/cli/install/bun-pack.test.ts @@ -643,6 +643,38 @@ describe("bundledDependnecies", () => { ]); }); + test(`basic should throw`, async () => { + await Promise.all([ + write( + join(packageDir, "package.json"), + JSON.stringify({ + name: "pack-bundled", + version: "4.4.4", + bundledDependencies: "a", + }), + ), + ]); + + const { stdout, stderr, exited } = Bun.spawn({ + cmd: [bunExe(), "pm", "pack"], + cwd: packageDir, + stdout: "pipe", + stderr: "pipe", + stdin: "ignore", + env: bunEnv, + }); + + const err = await Bun.readableStreamToText(stderr); + expect(err).toContain("error:"); + expect(err).toContain("to be an array of strings or boolean"); + expect(err).not.toContain("warning:"); + expect(err).not.toContain("failed"); + expect(err).not.toContain("panic:"); + + const exitCode = await exited; + expect(exitCode).toBe(1); + }); + test("resolve dep of bundled dep", async () => { // Test that a bundled dep can have it's dependencies resolved without // needing to add them to `bundledDependencies`. Also test that only From 7690e9d780c46119dcb7fe954714f4a9507dc7ac Mon Sep 17 00:00:00 2001 From: RiskyMH Date: Mon, 13 Jan 2025 22:42:10 +0100 Subject: [PATCH 4/4] oops test --- test/cli/install/bun-pack.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/cli/install/bun-pack.test.ts b/test/cli/install/bun-pack.test.ts index 5d35031a07b7bf..317889809b5385 100644 --- a/test/cli/install/bun-pack.test.ts +++ b/test/cli/install/bun-pack.test.ts @@ -643,7 +643,7 @@ describe("bundledDependnecies", () => { ]); }); - test(`basic should throw`, async () => { + test(`invalid bundledDependencies value should throw`, async () => { await Promise.all([ write( join(packageDir, "package.json"), @@ -666,7 +666,7 @@ describe("bundledDependnecies", () => { const err = await Bun.readableStreamToText(stderr); expect(err).toContain("error:"); - expect(err).toContain("to be an array of strings or boolean"); + expect(err).toContain("to be a boolean or an array of strings"); expect(err).not.toContain("warning:"); expect(err).not.toContain("failed"); expect(err).not.toContain("panic:");