From f6b694ee2c1171e7c2afedcabc0aa8ee10d730b1 Mon Sep 17 00:00:00 2001 From: Liz Date: Fri, 20 Oct 2023 06:28:59 +0200 Subject: [PATCH] fix(install): dont replace git urls when already present (#6607) * fix: dont replace git urls when already present * fix: set request e_string * test: add test for git url duplication --- src/install/install.zig | 13 ++++++++++ test/cli/install/bun-add.test.ts | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/install/install.zig b/src/install/install.zig index 0301fc2d30..276e6f70b8 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -5221,6 +5221,19 @@ pub const PackageManager = struct { } } break; + } else { + if (request.version.tag == .github or request.version.tag == .git) { + for (query.expr.data.e_object.properties.slice()) |item| { + if (item.value) |v| { + const url = request.version.literal.slice(request.version_buf); + if (v.data == .e_string and v.data.e_string.eql(string, url)) { + request.e_string = v.data.e_string; + remaining -= 1; + break; + } + } + } + } } } } diff --git a/test/cli/install/bun-add.test.ts b/test/cli/install/bun-add.test.ts index 62f5cc6cc5..fb942faa4c 100644 --- a/test/cli/install/bun-add.test.ts +++ b/test/cli/install/bun-add.test.ts @@ -1114,6 +1114,49 @@ it("should handle Git URL in dependencies (SCP-style)", async () => { expect(requested).toBe(0); }, 20000); +it("should not save git urls twice", async () => { + const urls: string[] = []; + setHandler(dummyRegistry(urls)); + await writeFile( + join(package_dir, "package.json"), + JSON.stringify({ + name: "foo", + version: "0.0.1", + }), + ); + const { exited: exited1 } = spawn({ + cmd: [bunExe(), "add", "https://github.com/liz3/bun-ui.git"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + + expect(await exited1).toBe(0); + + const package_json_content = await file(join(package_dir, "package.json")).json(); + expect(package_json_content.dependencies).toEqual({ + "bun-ui": "https://github.com/liz3/bun-ui.git", + }); + + const { exited: exited2 } = spawn({ + cmd: [bunExe(), "add", "https://github.com/liz3/bun-ui.git"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + + expect(await exited2).toBe(0); + + const package_json_content2 = await file(join(package_dir, "package.json")).json(); + expect(package_json_content2.dependencies).toEqual({ + "bun-ui": "https://github.com/liz3/bun-ui.git", + }); +}, 20000); + it("should prefer optionalDependencies over dependencies of the same name", async () => { const urls: string[] = []; setHandler(