Compare commits

...

2 Commits

Author SHA1 Message Date
autofix-ci[bot]
1c41eaeb49 fix: --port flag no longer sets origin for file imports
Fixes #14096

The --port flag is documented to "Set the default port for Bun.serve",
not to modify asset import URLs or set the origin. Previously, --port
would automatically set the origin, causing file imports like
`import txt from "./a.txt" with { type: "file" }` to incorrectly return
HTTP URLs like `http://localhost:3000/a.txt` instead of file paths.

This fix removes the automatic conversion of --port to origin. Users who
need to set an origin for their bundled assets should use the explicit
--origin flag instead.

Changes:
- Removed automatic port-to-origin conversion in Arguments.zig
- Added regression tests to verify --port doesn't affect file imports

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-04 06:11:12 +00:00
Claude Bot
2b5d7ca949 fix: --port flag no longer affects file import URLs
Fixes #14096

The --port flag is intended to set the default port for Bun.serve(),
not to modify asset import URLs. Previously, using --port would cause
file imports (e.g., `import txt from "./a.txt" with { type: "file" }`)
to return HTTP URLs like `http://localhost:3000/a.txt` instead of the
actual file path.

This fix adds a check for `is_from_devserver` flag before applying the
origin to file imports, ensuring that --port only affects Bun.serve()
and dev server contexts, not regular file imports.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-04 05:38:46 +00:00
2 changed files with 96 additions and 3 deletions

View File

@@ -803,9 +803,11 @@ pub fn parse(allocator: std.mem.Allocator, ctx: Command.Context, comptime cmd: C
Bun__Node__UseSystemCA = (Bun__Node__CAStore == .system);
}
if (opts.port != null and opts.origin == null) {
opts.origin = try std.fmt.allocPrint(allocator, "http://localhost:{d}/", .{opts.port.?});
}
// Removed: --port should only affect Bun.serve, not set origin for file imports
// Users can explicitly use --origin if they want to set the public path
// if (opts.port != null and opts.origin == null) {
// opts.origin = try std.fmt.allocPrint(allocator, "http://localhost:{d}/", .{opts.port.?});
// }
const output_dir: ?string = null;
const output_file: ?string = null;

View File

@@ -0,0 +1,91 @@
// Regression test for issue #14096: --port should not affect file imports
// https://github.com/oven-sh/bun/issues/14096
import { expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";
test("--port should not convert file imports to HTTP URLs", async () => {
using dir = tempDir("issue-14096", {
"index.js": `
import txt from "./test.txt" with { type: "file" };
console.log(txt);
`,
"test.txt": "hello world",
});
await using proc = Bun.spawn({
cmd: [bunExe(), "run", "--port", "3000", "index.js"],
env: bunEnv,
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(exitCode).toBe(0);
// Should print the file path, NOT http://localhost:3000/test.txt
const output = stdout.trim();
expect(output).not.toContain("http://");
expect(output).not.toContain("localhost");
expect(output).not.toContain("3000");
expect(output).toEndWith("test.txt");
});
test("--port should not affect asset imports (image files)", async () => {
using dir = tempDir("issue-14096-assets", {
"index.js": `
import bmp from "./abc.bmp" with { type: "file" };
console.log(bmp);
`,
"abc.bmp": "fake bmp data",
});
await using proc = Bun.spawn({
cmd: [bunExe(), "run", "--port", "4000", "index.js"],
env: bunEnv,
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(exitCode).toBe(0);
// Should print the file path, NOT http://localhost:4000/abc.bmp
const output = stdout.trim();
expect(output).not.toContain("http://");
expect(output).not.toContain("localhost");
expect(output).not.toContain("4000");
expect(output).toEndWith("abc.bmp");
});
test("file imports work without --port flag (baseline)", async () => {
using dir = tempDir("issue-14096-baseline", {
"index.js": `
import txt from "./test.txt" with { type: "file" };
console.log(txt);
`,
"test.txt": "hello world",
});
await using proc = Bun.spawn({
cmd: [bunExe(), "run", "index.js"],
env: bunEnv,
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(exitCode).toBe(0);
// Should print the file path
const output = stdout.trim();
expect(output).toEndWith("test.txt");
});