mirror of
https://github.com/oven-sh/bun
synced 2026-02-15 21:32:05 +00:00
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:
@@ -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();
|
||||
|
||||
103
test/regression/issue/14096.test.ts
Normal file
103
test/regression/issue/14096.test.ts
Normal 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");
|
||||
});
|
||||
Reference in New Issue
Block a user