diff options
author | 2023-10-02 18:00:17 -0700 | |
---|---|---|
committer | 2023-10-02 18:00:17 -0700 | |
commit | 15880308b78647755c59a3b656db52e9230d9c3d (patch) | |
tree | bf5a160e9cccc65333bbd9168852a1fb3048f488 | |
parent | 89bb526e1467cc53109eee884c5b6b1cffc7b3fc (diff) | |
download | bun-15880308b78647755c59a3b656db52e9230d9c3d.tar.gz bun-15880308b78647755c59a3b656db52e9230d9c3d.tar.zst bun-15880308b78647755c59a3b656db52e9230d9c3d.zip |
Fix `bun install` reading Github API from wrong environment variable (#6247)
* Fix `bun install` reading Github API from wrong environment variable
* Update src/install/install.zig
---------
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
-rw-r--r-- | src/install/install.zig | 12 | ||||
-rw-r--r-- | test/cli/install/bun-install.test.ts | 59 |
2 files changed, 65 insertions, 6 deletions
diff --git a/src/install/install.zig b/src/install/install.zig index 22c7c3c3d..2663e49a5 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -2131,10 +2131,10 @@ pub const PackageManager = struct { } fn allocGitHubURL(this: *const PackageManager, repository: *const Repository) string { - var github_api_domain: string = "api.github.com"; - if (this.env.map.get("GITHUB_API_DOMAIN")) |api_domain| { - if (api_domain.len > 0) { - github_api_domain = api_domain; + var github_api_url: string = "https://api.github.com"; + if (this.env.map.get("GITHUB_API_URL")) |url| { + if (url.len > 0) { + github_api_url = url; } } @@ -2144,9 +2144,9 @@ pub const PackageManager = struct { return std.fmt.allocPrint( this.allocator, - "https://{s}/repos/{s}/{s}{s}tarball/{s}", + "{s}/repos/{s}/{s}{s}tarball/{s}", .{ - github_api_domain, + strings.withoutTrailingSlash(github_api_url), owner, repo, // repo might be empty if dep is https://github.com/... style diff --git a/test/cli/install/bun-install.test.ts b/test/cli/install/bun-install.test.ts index 063f9ad04..19a5851a1 100644 --- a/test/cli/install/bun-install.test.ts +++ b/test/cli/install/bun-install.test.ts @@ -2906,6 +2906,65 @@ it("should handle GitHub tarball URL in dependencies (https://github.com/user/re await access(join(package_dir, "bun.lockb")); }); +it("should handle GitHub tarball URL in dependencies (https://github.com/user/repo/tarball/ref) with custom GITHUB_API_URL", async () => { + const urls: string[] = []; + setHandler(dummyRegistry(urls)); + await writeFile( + join(package_dir, "package.json"), + JSON.stringify({ + name: "Foo", + version: "0.0.1", + dependencies: { + when: "https://github.com/cujojs/when/tarball/1.0.2", + }, + }), + ); + const { stdout, stderr, exited } = spawn({ + cmd: [bunExe(), "install"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env: { + ...env, + GITHUB_API_URL: "https://example.com/github/api", + }, + }); + expect(stderr).toBeDefined(); + const err = await new Response(stderr).text(); + expect(err).toContain("Saved lockfile"); + expect(stdout).toBeDefined(); + let out = await new Response(stdout).text(); + out = out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, ""); + out = out.replace(/(github:[^#]+)#[a-f0-9]+/, "$1"); + expect(out.split(/\r?\n/)).toEqual([ + " + when@https://github.com/cujojs/when/tarball/1.0.2", + "", + " 1 packages installed", + ]); + expect(await exited).toBe(0); + expect(urls.sort()).toBeEmpty(); + expect(requested).toBe(0); + expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".cache", "when"]); + expect(await readdirSorted(join(package_dir, "node_modules", "when"))).toEqual([ + ".gitignore", + ".gitmodules", + "LICENSE.txt", + "README.md", + "apply.js", + "cancelable.js", + "delay.js", + "package.json", + "test", + "timed.js", + "timeout.js", + "when.js", + ]); + const package_json = await file(join(package_dir, "node_modules", "when", "package.json")).json(); + expect(package_json.name).toBe("when"); + await access(join(package_dir, "bun.lockb")); +}); + it("should handle GitHub URL with existing lockfile", async () => { const urls: string[] = []; setHandler(dummyRegistry(urls)); |