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>
This commit is contained in:
Claude Bot
2025-10-04 05:38:46 +00:00
parent c8cb7713fc
commit 2b5d7ca949
2 changed files with 106 additions and 1 deletions

View File

@@ -1516,7 +1516,9 @@ pub fn transpileSourceCode(
}
const value = brk: {
if (!jsc_vm.origin.isEmpty()) {
// Only use origin for file imports when running from dev server
// --port flag should only affect Bun.serve, not file import URLs
if (!jsc_vm.origin.isEmpty() and jsc_vm.is_from_devserver) {
var buf = bun.handleOom(MutableString.init2048(jsc_vm.allocator));
defer buf.deinit();
var writer = buf.writer();

View File

@@ -0,0 +1,103 @@
// Regression test for issue #14096: --port should not affect file imports
// https://github.com/oven-sh/bun/issues/14096
import { test, expect } 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");
});