Skip to content

Commit

Permalink
Fix bun install reading Github API from wrong environment variable (#…
Browse files Browse the repository at this point in the history
…6247)

* Fix `bun install` reading Github API from wrong environment variable

* Update src/install/install.zig

---------

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
  • Loading branch information
Electroid and Jarred-Sumner committed Oct 3, 2023
1 parent 89bb526 commit 1588030
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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
Expand Down
59 changes: 59 additions & 0 deletions test/cli/install/bun-install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 1588030

Please sign in to comment.