Compare commits

...

2 Commits

Author SHA1 Message Date
autofix-ci[bot]
b3d3d7d20f [autofix.ci] apply automated fixes 2025-09-28 10:46:54 +00:00
Claude Bot
11a8cba9ce fix: resolve bunfig.toml cafile paths relative to bunfig location
Previously, when cafile was specified in bunfig.toml with a relative path,
it was resolved relative to the current working directory when PackageManager.init()
was called. This caused issues when bunfig.toml was not in the current directory
or when running commands from different directories.

This fix ensures that relative cafile paths in bunfig.toml are resolved
relative to the bunfig.toml file location itself, matching user expectations
and making the behavior consistent with other configuration file systems.

Fixes #23046

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-28 10:44:31 +00:00
2 changed files with 181 additions and 1 deletions

View File

@@ -423,10 +423,24 @@ pub const Bunfig = struct {
}
if (install_obj.get("cafile")) |cafile| {
install.cafile = try cafile.asStringCloned(allocator) orelse {
const cafile_path = try cafile.asStringCloned(allocator) orelse {
try this.addError(cafile.loc, "Invalid cafile. Expected a string.");
return;
};
// If cafile is a relative path, resolve it relative to the bunfig.toml location
if (cafile_path.len > 0 and !std.fs.path.isAbsolute(cafile_path)) {
var path_buf: bun.PathBuffer = undefined;
const bunfig_dir = std.fs.path.dirname(this.source.path.text) orelse ".";
install.cafile = try allocator.dupe(u8, bun.path.joinAbsStringBuf(
bunfig_dir,
&path_buf,
&.{cafile_path},
.auto,
));
} else {
install.cafile = cafile_path;
}
}
if (install_obj.get("ca")) |ca| {

View File

@@ -0,0 +1,166 @@
import { spawn, write } from "bun";
import { describe, expect, test } from "bun:test";
import { mkdir } from "fs/promises";
import { bunExe, bunEnv as env, stderrForInstall, tmpdirSync } from "harness";
import { join } from "path";
describe("bunfig cafile", () => {
test("relative cafile path in bunfig.toml is resolved relative to bunfig location", async () => {
// Create a test directory structure
const testDir = tmpdirSync();
const configDir = join(testDir, "config");
const packageDir = testDir;
const packageJson = join(packageDir, "package.json");
await mkdir(configDir, { recursive: true });
// Create a dummy CA file in the config directory
const caFile = join(configDir, "test-ca.crt");
await write(caFile, "-----BEGIN CERTIFICATE-----\nDUMMY_CERT\n-----END CERTIFICATE-----");
// Create bunfig.toml with relative path to CA file (same directory as bunfig)
const bunfig = `[install]
cafile = "test-ca.crt"
cache = false
`;
await write(join(configDir, "bunfig.toml"), bunfig);
// Create package.json
await write(
packageJson,
JSON.stringify({
name: "cafile-test-pkg",
version: "1.0.0",
private: false,
}),
);
// Test that the cafile path is correctly resolved when loading bunfig
// The cafile should be resolved relative to the bunfig.toml location, not CWD
const result = spawn({
cmd: [bunExe(), "publish", "--dry-run", "-c", join(configDir, "bunfig.toml")],
cwd: packageDir,
stdout: "pipe",
stderr: "pipe",
env: {
...env,
// Disable any real network operations
BUN_CONFIG_REGISTRY: "http://localhost:0/",
},
});
const stderr = stderrForInstall(await result.stderr.text());
const stdout = await result.stdout.text();
const exitCode = await result.exited;
// With the fix, it should not error about being unable to find/load the CA file
expect(stderr).not.toContain("failed to find CA file");
expect(stderr).not.toContain("failed to load CA file");
expect(stderr).not.toContain("the CA file is invalid");
// Note: The publish will fail for other reasons (no registry, etc.),
// but the important thing is that the CA file was found and loaded correctly.
});
test("absolute cafile path in bunfig.toml works", async () => {
// Create a test directory structure
const testDir = tmpdirSync();
const packageDir = testDir;
const packageJson = join(packageDir, "package.json");
// Create a dummy CA file with absolute path
const caFile = join(testDir, "absolute-test-ca.crt");
await write(caFile, "-----BEGIN CERTIFICATE-----\nDUMMY_CERT\n-----END CERTIFICATE-----");
// Create bunfig.toml with absolute path to CA file
const bunfig = `[install]
cafile = "${caFile}"
cache = false
`;
await write(join(packageDir, "bunfig.toml"), bunfig);
// Create package.json
await write(
packageJson,
JSON.stringify({
name: "cafile-test-pkg-abs",
version: "1.0.0",
private: false,
}),
);
// Test that absolute cafile path works
const result = spawn({
cmd: [bunExe(), "publish", "--dry-run"],
cwd: packageDir,
stdout: "pipe",
stderr: "pipe",
env: {
...env,
// Disable any real network operations
BUN_CONFIG_REGISTRY: "http://localhost:0/",
},
});
const stderr = stderrForInstall(await result.stderr.text());
const stdout = await result.stdout.text();
const exitCode = await result.exited;
// Should not have CA file loading errors
expect(stderr).not.toContain("failed to find CA file");
expect(stderr).not.toContain("failed to load CA file");
expect(stderr).not.toContain("the CA file is invalid");
});
test("command line --cafile overrides bunfig.toml", async () => {
// Create a test directory structure
const testDir = tmpdirSync();
const packageDir = testDir;
const packageJson = join(packageDir, "package.json");
// Create two CA files
const caFileBunfig = join(testDir, "bunfig-ca.crt");
const caFileCLI = join(testDir, "cli-ca.crt");
await write(caFileBunfig, "-----BEGIN CERTIFICATE-----\nBUNFIG_CERT\n-----END CERTIFICATE-----");
await write(caFileCLI, "-----BEGIN CERTIFICATE-----\nCLI_CERT\n-----END CERTIFICATE-----");
// Create bunfig.toml pointing to the bunfig CA file
const bunfig = `[install]
cafile = "${caFileBunfig}"
cache = false
`;
await write(join(packageDir, "bunfig.toml"), bunfig);
// Create package.json
await write(
packageJson,
JSON.stringify({
name: "cafile-test-override",
version: "1.0.0",
private: false,
}),
);
// Test that command line --cafile overrides bunfig.toml
const result = spawn({
cmd: [bunExe(), "publish", "--dry-run", "--cafile", caFileCLI],
cwd: packageDir,
stdout: "pipe",
stderr: "pipe",
env: {
...env,
// Disable any real network operations
BUN_CONFIG_REGISTRY: "http://localhost:0/",
},
});
const stderr = stderrForInstall(await result.stderr.text());
const stdout = await result.stdout.text();
const exitCode = await result.exited;
// Should use the CLI CA file, not the bunfig one
expect(stderr).not.toContain("failed to find CA file");
expect(stderr).not.toContain("failed to load CA file");
expect(stderr).not.toContain("the CA file is invalid");
});
});