From 2b5d7ca94981d5ef65e83c524edb166ed2e89069 Mon Sep 17 00:00:00 2001 From: Claude Bot Date: Sat, 4 Oct 2025 05:38:46 +0000 Subject: [PATCH] fix: --port flag no longer affects file import URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/bun.js/ModuleLoader.zig | 4 +- test/regression/issue/14096.test.ts | 103 ++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 test/regression/issue/14096.test.ts diff --git a/src/bun.js/ModuleLoader.zig b/src/bun.js/ModuleLoader.zig index d0bcafe343..c926e6ae8f 100644 --- a/src/bun.js/ModuleLoader.zig +++ b/src/bun.js/ModuleLoader.zig @@ -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(); diff --git a/test/regression/issue/14096.test.ts b/test/regression/issue/14096.test.ts new file mode 100644 index 0000000000..b5ee6b69c6 --- /dev/null +++ b/test/regression/issue/14096.test.ts @@ -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"); +});