From fd06bee952332d8826126d244d71766b85c60131 Mon Sep 17 00:00:00 2001 From: RiskyMH Date: Fri, 7 Feb 2025 06:33:09 +0000 Subject: [PATCH 1/5] `bun pm pack` support files starting with `./` --- src/cli/pack_command.zig | 6 ++++++ test/cli/install/bun-pack.test.ts | 26 ++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/cli/pack_command.zig b/src/cli/pack_command.zig index dc9db0f63a364d..b3547f7872b303 100644 --- a/src/cli/pack_command.zig +++ b/src/cli/pack_command.zig @@ -2129,6 +2129,12 @@ pub const PackCommand = struct { @"has leading **/, (could start with '!')" = true; } + // `./foo` matches the same as `foo` + if (strings.hasPrefixComptime(remain, "./")) { + remain = remain["./".len..]; + if (remain.len == 0) return null; + } + const trailing_slash = remain[remain.len - 1] == '/'; if (trailing_slash) { // trim trailing slash diff --git a/test/cli/install/bun-pack.test.ts b/test/cli/install/bun-pack.test.ts index ac74efa8e7a6dc..23b391565e3a5e 100644 --- a/test/cli/install/bun-pack.test.ts +++ b/test/cli/install/bun-pack.test.ts @@ -921,7 +921,7 @@ describe("files", () => { write( join(packageDir, "package.json"), JSON.stringify({ - name: "pack-files-3", + name: "pack-files-2", version: "1.2.3", files: ["index.js"], }), @@ -932,16 +932,34 @@ describe("files", () => { ]); await pack(packageDir, bunEnv); - const tarball = readTarball(join(packageDir, "pack-files-3-1.2.3.tgz")); + const tarball = readTarball(join(packageDir, "pack-files-2-1.2.3.tgz")); expect(tarball.entries).toMatchObject([{ "pathname": "package/package.json" }, { "pathname": "package/index.js" }]); }); + test("matches './' as the root", async () => { + await Promise.all([ + write( + join(packageDir, "package.json"), + JSON.stringify({ + name: "pack-files-3", + version: "1.2.3", + files: ["./dist"], + }), + ), + write(join(packageDir, "dist", "index.js"), "console.log('hello ./dist/index.js')"), + ]); + + await pack(packageDir, bunEnv); + const tarball = readTarball(join(packageDir, "pack-files-3-1.2.3.tgz")); + expect(tarball.entries).toMatchObject([{ "pathname": "package/package.json" }, { "pathname": "package/dist/index.js" }]); + }); + test("recursive only if leading **/", async () => { await Promise.all([ write( join(packageDir, "package.json"), JSON.stringify({ - name: "pack-files-2", + name: "pack-files-4", version: "1.2.123", files: ["**/index.js"], }), @@ -953,7 +971,7 @@ describe("files", () => { ]); await pack(packageDir, bunEnv); - const tarball = readTarball(join(packageDir, "pack-files-2-1.2.123.tgz")); + const tarball = readTarball(join(packageDir, "pack-files-4-1.2.123.tgz")); expect(tarball.entries).toMatchObject([ { "pathname": "package/package.json" }, { "pathname": "package/index.js" }, From b8af1ee145674ac538c646e86c98205609248ab3 Mon Sep 17 00:00:00 2001 From: RiskyMH Date: Fri, 7 Feb 2025 06:40:06 +0000 Subject: [PATCH 2/5] more intresting test --- test/cli/install/bun-pack.test.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/cli/install/bun-pack.test.ts b/test/cli/install/bun-pack.test.ts index 23b391565e3a5e..799abd7dda50a8 100644 --- a/test/cli/install/bun-pack.test.ts +++ b/test/cli/install/bun-pack.test.ts @@ -943,15 +943,20 @@ describe("files", () => { JSON.stringify({ name: "pack-files-3", version: "1.2.3", - files: ["./dist"], + files: ["./dist", "!./subdir", "!./dist/index.js"], }), ), write(join(packageDir, "dist", "index.js"), "console.log('hello ./dist/index.js')"), + write(join(packageDir, "subdir", "index.js"), "console.log('hello ./subdir/index.js')"), + write(join(packageDir, "src", "dist", "index.js"), "console.log('hello ./src/dist/index.js')"), ]); await pack(packageDir, bunEnv); const tarball = readTarball(join(packageDir, "pack-files-3-1.2.3.tgz")); - expect(tarball.entries).toMatchObject([{ "pathname": "package/package.json" }, { "pathname": "package/dist/index.js" }]); + expect(tarball.entries).toMatchObject([ + { "pathname": "package/package.json" }, + { "pathname": "package/dist/index.js" }, + ]); }); test("recursive only if leading **/", async () => { From 573e0c31669446e07b54ed3cc144a15f2d7ccc40 Mon Sep 17 00:00:00 2001 From: RiskyMH Date: Sat, 8 Feb 2025 06:47:35 +0000 Subject: [PATCH 3/5] better method --- src/cli/pack_command.zig | 10 +++------- test/cli/install/bun-pack.test.ts | 4 +++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/cli/pack_command.zig b/src/cli/pack_command.zig index b3547f7872b303..00bc1785682f4e 100644 --- a/src/cli/pack_command.zig +++ b/src/cli/pack_command.zig @@ -1359,7 +1359,9 @@ pub const PackCommand = struct { var files_array = _files_array; while (files_array.next()) |files_entry| { if (files_entry.asString(ctx.allocator)) |file_entry_str| { - const parsed = try Pattern.fromUTF8(ctx.allocator, file_entry_str) orelse continue; + var path_buf: PathBuffer = undefined; + const normalized = bun.path.normalizeBuf(file_entry_str, &path_buf, .posix); + const parsed = try Pattern.fromUTF8(ctx.allocator, try ctx.allocator.dupe(u8, normalized)) orelse continue; try includes.append(ctx.allocator, parsed); continue; } @@ -2129,12 +2131,6 @@ pub const PackCommand = struct { @"has leading **/, (could start with '!')" = true; } - // `./foo` matches the same as `foo` - if (strings.hasPrefixComptime(remain, "./")) { - remain = remain["./".len..]; - if (remain.len == 0) return null; - } - const trailing_slash = remain[remain.len - 1] == '/'; if (trailing_slash) { // trim trailing slash diff --git a/test/cli/install/bun-pack.test.ts b/test/cli/install/bun-pack.test.ts index 799abd7dda50a8..482a5cc56776d9 100644 --- a/test/cli/install/bun-pack.test.ts +++ b/test/cli/install/bun-pack.test.ts @@ -943,12 +943,13 @@ describe("files", () => { JSON.stringify({ name: "pack-files-3", version: "1.2.3", - files: ["./dist", "!./subdir", "!./dist/index.js"], + files: ["./dist", "!./subdir", "!./dist/index.js", "./////src//index.ts"], }), ), write(join(packageDir, "dist", "index.js"), "console.log('hello ./dist/index.js')"), write(join(packageDir, "subdir", "index.js"), "console.log('hello ./subdir/index.js')"), write(join(packageDir, "src", "dist", "index.js"), "console.log('hello ./src/dist/index.js')"), + write(join(packageDir, "src", "index.ts"), "console.log('hello ./src/index.ts')"), ]); await pack(packageDir, bunEnv); @@ -956,6 +957,7 @@ describe("files", () => { expect(tarball.entries).toMatchObject([ { "pathname": "package/package.json" }, { "pathname": "package/dist/index.js" }, + { "pathname": "package/src/index.ts" }, ]); }); From 5dc56f5423ac4fb1276c565169665988c34faf31 Mon Sep 17 00:00:00 2001 From: RiskyMH Date: Sat, 8 Feb 2025 08:24:56 +0000 Subject: [PATCH 4/5] more efficient --- 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 00bc1785682f4e..6749cf9f329975 100644 --- a/src/cli/pack_command.zig +++ b/src/cli/pack_command.zig @@ -1356,10 +1356,10 @@ pub const PackCommand = struct { var includes: std.ArrayListUnmanaged(Pattern) = .{}; defer includes.deinit(ctx.allocator); + var path_buf: PathBuffer = undefined; var files_array = _files_array; while (files_array.next()) |files_entry| { if (files_entry.asString(ctx.allocator)) |file_entry_str| { - var path_buf: PathBuffer = undefined; const normalized = bun.path.normalizeBuf(file_entry_str, &path_buf, .posix); const parsed = try Pattern.fromUTF8(ctx.allocator, try ctx.allocator.dupe(u8, normalized)) orelse continue; try includes.append(ctx.allocator, parsed); From d911de789464960e9a969650030b084fd0b7cac1 Mon Sep 17 00:00:00 2001 From: Michael H Date: Sat, 8 Feb 2025 19:26:06 +1100 Subject: [PATCH 5/5] 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 6749cf9f329975..b23e5ebaaf5f64 100644 --- a/src/cli/pack_command.zig +++ b/src/cli/pack_command.zig @@ -1361,7 +1361,7 @@ pub const PackCommand = struct { while (files_array.next()) |files_entry| { if (files_entry.asString(ctx.allocator)) |file_entry_str| { const normalized = bun.path.normalizeBuf(file_entry_str, &path_buf, .posix); - const parsed = try Pattern.fromUTF8(ctx.allocator, try ctx.allocator.dupe(u8, normalized)) orelse continue; + const parsed = try Pattern.fromUTF8(ctx.allocator, normalized) orelse continue; try includes.append(ctx.allocator, parsed); continue; }