From 1bf28e0d77a8b2261befbdb708cefd03e0126960 Mon Sep 17 00:00:00 2001 From: dave caruso Date: Wed, 11 Oct 2023 02:27:07 -0700 Subject: feat(install): automatically migrate package-lock.json to bun.lockb (#6352) * work so far * stuff * a * basics work * stuff * yoo * build lockfile * correct * f * a * install fixture havent tested * i made it worse * lol * be more reasonable * make the test easier to pass because bun install doesn't handle obscure lockfile edge cases :/ * a * works now * ok * a * a * cool * nah * fix stuff * l * a * idfk * LAME * prettier errors * does this fix tests? * Add more safety checks to Integrity * Add another check * More careful lifetime handling * Fix linux debugger issue * a * tmp dir and snapshot test --------- Co-authored-by: Jarred SUmner --- src/cli/package_manager_command.zig | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/cli') diff --git a/src/cli/package_manager_command.zig b/src/cli/package_manager_command.zig index aef7c28a9..00326bc97 100644 --- a/src/cli/package_manager_command.zig +++ b/src/cli/package_manager_command.zig @@ -243,6 +243,32 @@ pub const PackageManagerCommand = struct { } } + Global.exit(0); + } else if (strings.eqlComptime(subcommand, "migrate")) { + if (!pm.options.enable.force_save_lockfile) try_load_bun: { + std.fs.cwd().accessZ("bun.lockb", .{ .mode = .read_only }) catch break :try_load_bun; + + Output.prettyErrorln( + \\error: bun.lockb already exists + \\run with --force to overwrite + , .{}); + Global.exit(1); + } + const load_lockfile = @import("../install/migration.zig").detectAndLoadOtherLockfile( + pm.lockfile, + ctx.allocator, + pm.log, + pm.options.lockfile_path, + ); + if (load_lockfile == .not_found) { + Output.prettyErrorln( + \\error: could not find any other lockfile + , .{}); + Global.exit(1); + } + handleLoadLockfileErrors(load_lockfile, pm); + const lockfile = load_lockfile.ok; + lockfile.saveToDisk(pm.options.lockfile_path); Global.exit(0); } @@ -258,6 +284,7 @@ pub const PackageManagerCommand = struct { \\ bun pm hash-print print the hash stored in the current lockfile \\ bun pm cache print the path to the cache folder \\ bun pm cache rm clear the cache + \\ bun pm migrate migrate another package manager's lockfile without installing anything \\ \\Learn more about these at https://bun.sh/docs/install/utilities \\ -- cgit v1.2.3 From 691cf338c2a88fd2629f50934fe35fcb5bd08ae2 Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Thu, 12 Oct 2023 15:02:05 -0700 Subject: fix editing package json when adding github dependency (#6432) * fix package name added to package json * check for github tag * remove alloc * some tests * fix test --- src/cli/bunx_command.zig | 24 +++++---- src/install/install.zig | 1 - test/cli/install/bun-install.test.ts | 91 ++++++++++++++++++++++++++++++++++ test/cli/install/bunx.test.ts | 40 ++++++++++++++- test/js/bun/test/jest-extended.test.js | 4 +- 5 files changed, 146 insertions(+), 14 deletions(-) (limited to 'src/cli') diff --git a/src/cli/bunx_command.zig b/src/cli/bunx_command.zig index dba2d5664..e7f35d10d 100644 --- a/src/cli/bunx_command.zig +++ b/src/cli/bunx_command.zig @@ -236,6 +236,8 @@ pub const BunxCommand = struct { const initial_bin_name = if (strings.eqlComptime(update_request.name, "typescript")) "tsc" + else if (update_request.version.tag == .github) + update_request.version.value.github.repo.slice(update_request.version_buf) else if (strings.lastIndexOfChar(update_request.name, '/')) |index| update_request.name[index + 1 ..] else @@ -262,19 +264,19 @@ pub const BunxCommand = struct { else update_request.version.literal.slice(update_request.version_buf); - const package_fmt: []const u8 = brk: { - if (!strings.eql(update_request.version_buf, update_request.name)) { - break :brk try std.fmt.allocPrint( - ctx.allocator, - "{s}@{s}", - .{ - update_request.name, - display_version, - }, - ); + const package_fmt = brk: { + if (update_request.version.tag == .github) { + break :brk update_request.version.literal.slice(update_request.version_buf); } - break :brk update_request.name; + break :brk try std.fmt.allocPrint( + ctx.allocator, + "{s}@{s}", + .{ + update_request.name, + display_version, + }, + ); }; const PATH_FOR_BIN_DIRS = PATH; diff --git a/src/install/install.zig b/src/install/install.zig index 67fbf6b6b..6bff738df 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -6370,7 +6370,6 @@ pub const PackageManager = struct { request.name = allocator.dupe(u8, name) catch unreachable; request.name_hash = String.Builder.stringHash(name); } else if (version.tag == .github and version.value.github.committish.isEmpty()) { - request.name = input; request.name_hash = String.Builder.stringHash(version.literal.slice(input)); } else { request.name_hash = String.Builder.stringHash(version.literal.slice(input)); diff --git a/test/cli/install/bun-install.test.ts b/test/cli/install/bun-install.test.ts index a0d28bf70..bbe315244 100644 --- a/test/cli/install/bun-install.test.ts +++ b/test/cli/install/bun-install.test.ts @@ -1503,6 +1503,97 @@ it("should handle ^0.0.2 in dependencies", async () => { await access(join(package_dir, "bun.lockb")); }); +it("should edit package json correctly with git dependencies", async () => { + const urls: string[] = []; + setHandler(dummyRegistry(urls)); + const package_json = JSON.stringify({ + name: "foo", + version: "0.0.1", + dependencies: {}, + }); + await writeFile(join(package_dir, "package.json"), package_json); + var { stdout, stderr, exited } = spawn({ + cmd: [bunExe(), "i", "dylan-conway/install-test"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + var err = await new Response(stderr).text(); + expect(err).toContain("Saved lockfile"); + expect(err).not.toContain("error:"); + expect(await exited).toBe(0); + expect(await file(join(package_dir, "package.json")).json()).toEqual({ + name: "foo", + version: "0.0.1", + dependencies: { + "install-test": "dylan-conway/install-test", + }, + }); + await writeFile(join(package_dir, "package.json"), package_json); + ({ stdout, stderr, exited } = spawn({ + cmd: [bunExe(), "i", "dylan-conway/install-test#HEAD"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + })); + err = await new Response(stderr).text(); + expect(err).toContain("Saved lockfile"); + expect(err).not.toContain("error:"); + expect(await exited).toBe(0); + expect(await file(join(package_dir, "package.json")).json()).toEqual({ + name: "foo", + version: "0.0.1", + dependencies: { + "install-test": "dylan-conway/install-test#HEAD", + }, + }); + await writeFile(join(package_dir, "package.json"), package_json); + ({ stdout, stderr, exited } = spawn({ + cmd: [bunExe(), "i", "github:dylan-conway/install-test"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + })); + err = await new Response(stderr).text(); + expect(err).toContain("Saved lockfile"); + expect(err).not.toContain("error:"); + expect(await exited).toBe(0); + expect(await file(join(package_dir, "package.json")).json()).toEqual({ + name: "foo", + version: "0.0.1", + dependencies: { + "install-test": "github:dylan-conway/install-test", + }, + }); + await writeFile(join(package_dir, "package.json"), package_json); + ({ stdout, stderr, exited } = spawn({ + cmd: [bunExe(), "i", "github:dylan-conway/install-test#HEAD"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + })); + err = await new Response(stderr).text(); + expect(err).toContain("Saved lockfile"); + expect(err).not.toContain("error:"); + expect(await exited).toBe(0); + expect(await file(join(package_dir, "package.json")).json()).toEqual({ + name: "foo", + version: "0.0.1", + dependencies: { + "install-test": "github:dylan-conway/install-test#HEAD", + }, + }); + await access(join(package_dir, "bun.lockb")); +}); + it("should handle ^0.0.2-rc in dependencies", async () => { const urls: string[] = []; setHandler(dummyRegistry(urls, { "0.0.2-rc": { as: "0.0.2" } })); diff --git a/test/cli/install/bunx.test.ts b/test/cli/install/bunx.test.ts index 0e1eba8f3..b8baceb1b 100644 --- a/test/cli/install/bunx.test.ts +++ b/test/cli/install/bunx.test.ts @@ -158,7 +158,7 @@ console.log( }); it("should work for github repository", async () => { - await rm(join(await realpath(tmpdir()), "@withfig"), { force: true, recursive: true }); + await rm(join(await realpath(tmpdir()), "github:piuccio"), { force: true, recursive: true }); // without cache const withoutCache = spawn({ cmd: [bunExe(), "x", "github:piuccio/cowsay", "--help"], @@ -195,3 +195,41 @@ it("should work for github repository", async () => { expect(out.trim()).toContain("Usage: cowsay"); expect(await cached.exited).toBe(0); }); + +it("should work for github repository with committish", async () => { + await rm(join(await realpath(tmpdir()), "github:piuccio"), { force: true, recursive: true }); + const withoutCache = spawn({ + cmd: [bunExe(), "x", "github:piuccio/cowsay#HEAD", "hello bun!"], + cwd: x_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + + expect(withoutCache.stderr).toBeDefined(); + let err = await new Response(withoutCache.stderr).text(); + expect(err).not.toContain("error"); + expect(withoutCache.stdout).toBeDefined(); + let out = await new Response(withoutCache.stdout).text(); + expect(out.trim()).toContain("hello bun!"); + expect(await withoutCache.exited).toBe(0); + + // cached + const cached = spawn({ + cmd: [bunExe(), "x", "github:piuccio/cowsay#HEAD", "hello bun!"], + cwd: x_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + + expect(cached.stderr).toBeDefined(); + err = await new Response(cached.stderr).text(); + expect(err).not.toContain("error"); + expect(cached.stdout).toBeDefined(); + out = await new Response(cached.stdout).text(); + expect(out.trim()).toContain("hello bun!"); + expect(await cached.exited).toBe(0); +}); diff --git a/test/js/bun/test/jest-extended.test.js b/test/js/bun/test/jest-extended.test.js index 2194a8399..0b8174433 100644 --- a/test/js/bun/test/jest-extended.test.js +++ b/test/js/bun/test/jest-extended.test.js @@ -603,7 +603,9 @@ describe("jest-extended", () => { ); expect("hello world").not.toEqualIgnoringWhitespace("hello world!"); - expect({}).not.toEqualIgnoringWhitespace({}); + expect(() => { + expect({}).not.toEqualIgnoringWhitespace({}); + }).toThrow("requires argument to be a string"); }); // Symbol -- cgit v1.2.3