-
Notifications
You must be signed in to change notification settings - Fork 5k
bunfig: fall back to $HOME/.bunfig.toml when $XDG_CONFIG_HOME has none #38313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import { write } from "bun"; | |
| import { afterAll, beforeAll, describe, expect, it, test } from "bun:test"; | ||
| import { rm } from "fs/promises"; | ||
| import { VerdaccioRegistry, bunExe, bunEnv as env, tempDir } from "harness"; | ||
| import { join } from "path"; | ||
| import { basename, join } from "path"; | ||
| const { iniInternals } = require("bun:internal-for-testing"); | ||
| const { loadNpmrc } = iniInternals; | ||
|
|
||
|
|
@@ -224,6 +224,67 @@ registry = http://localhost:${registry.port}/ | |
| }); | ||
| }); | ||
|
|
||
| // The global .bunfig.toml is looked up with the same rule as the user .npmrc above. | ||
| describe("global .bunfig.toml lookup", () => { | ||
| const bunfig = (cacheDir: string) => `[install]\ncache = "${cacheDir}"\n`; | ||
| const pkg = { "pkg/package.json": JSON.stringify({ name: "bunfig-lookup", version: "0.0.1" }) }; | ||
|
|
||
| // `bun pm cache` prints the install cache directory. Each candidate .bunfig.toml | ||
| // points it at a differently named directory, so the output shows which file was | ||
| // read. BUN_INSTALL_CACHE_DIR would take precedence over bunfig, and CI runners | ||
| // set it as well as XDG_CONFIG_HOME, so both are removed. | ||
| async function pmCache(dir: string, envOverride: Record<string, string>) { | ||
| const spawnEnv = { ...env, HOME: join(dir, "home"), USERPROFILE: join(dir, "home") }; | ||
| delete spawnEnv.XDG_CONFIG_HOME; | ||
| delete spawnEnv.BUN_INSTALL_CACHE_DIR; | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "pm", "cache"], | ||
| cwd: join(dir, "pkg"), | ||
| env: { ...spawnEnv, ...envOverride }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { cacheDir: basename(stdout.trim()), stderr, exitCode }; | ||
| } | ||
|
|
||
| const usesCacheDir = (cacheDir: string) => ({ cacheDir, stderr: "", exitCode: 0 }); | ||
|
|
||
| it.concurrent("uses $XDG_CONFIG_HOME/.bunfig.toml when it exists", async () => { | ||
| using dir = tempDir("bunfig-xdg", { | ||
| ...pkg, | ||
| "home/.bunfig.toml": bunfig("home-cache"), | ||
| "xdg/.bunfig.toml": bunfig("xdg-cache"), | ||
| }); | ||
| const result = await pmCache(String(dir), { XDG_CONFIG_HOME: join(String(dir), "xdg") }); | ||
| expect(result).toEqual(usesCacheDir("xdg-cache")); | ||
| }); | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/23128 | ||
| it.concurrent("falls back to $HOME/.bunfig.toml when $XDG_CONFIG_HOME has no .bunfig.toml", async () => { | ||
| using dir = tempDir("bunfig-xdg-without-bunfig", { | ||
| ...pkg, | ||
| "home/.bunfig.toml": bunfig("home-cache"), | ||
| "xdg/.keep": "", | ||
| }); | ||
| const result = await pmCache(String(dir), { XDG_CONFIG_HOME: join(String(dir), "xdg") }); | ||
| expect(result).toEqual(usesCacheDir("home-cache")); | ||
| }); | ||
|
|
||
| it.concurrent("uses $HOME/.bunfig.toml when $XDG_CONFIG_HOME is unset", async () => { | ||
| using dir = tempDir("bunfig-xdg-unset", { ...pkg, "home/.bunfig.toml": bunfig("home-cache") }); | ||
| const result = await pmCache(String(dir), {}); | ||
| expect(result).toEqual(usesCacheDir("home-cache")); | ||
| }); | ||
|
|
||
| it.concurrent("uses $HOME/.bunfig.toml when $XDG_CONFIG_HOME is empty", async () => { | ||
| using dir = tempDir("bunfig-xdg-empty", { ...pkg, "home/.bunfig.toml": bunfig("home-cache") }); | ||
| const result = await pmCache(String(dir), { XDG_CONFIG_HOME: "" }); | ||
| expect(result).toEqual(usesCacheDir("home-cache")); | ||
| }); | ||
|
Comment on lines
+275
to
+285
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Add coverage for empty HOME values. Line 281 tests an empty As per coding guidelines: “Every behavioral change must include an automated regression test,” and tests must cover relevant boundary states. 🤖 Prompt for AI AgentsSources: Coding guidelines, Learnings |
||
| }); | ||
|
|
||
| it("package config overrides home config", async () => { | ||
| const { packageDir, packageJson } = await registry.createTestDir(); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: oven-sh/bun
Length of output: 24627
🏁 Script executed:
Repository: oven-sh/bun
Length of output: 34517
🏁 Script executed:
Repository: oven-sh/bun
Length of output: 307
🏁 Script executed:
Repository: oven-sh/bun
Length of output: 25350
🏁 Script executed:
Repository: oven-sh/bun
Length of output: 347
🏁 Script executed:
Repository: oven-sh/bun
Length of output: 30413
Replace
bun_sys::exists_zwith a regular-file check.exists_zaccepts directories and ignores read permissions, so either candidate blocks the$HOME/.bunfig.tomlfallback. Usebun_sys::statwithbun_sys::is_regular_file, and add regression tests for both cases.🤖 Prompt for AI Agents