Repository navigation
bundler: make sourcemap sources relative to the map file, not outdir
#36601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4573477
bundler: make sourcemap sources relative to the map file, not outdir
robobun 3c8d467
[autofix.ci] apply automated fixes
autofix-ci[bot] 851a27b
trim doc comments on rel_dir / source_map_chunk_abs_dir
robobun 7fdb4fa
absolutize relative outdir before joining chunk rel_dir
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| // https://github.com/oven-sh/bun/issues/3332 | ||
| // Sourcemap `sources` entries must be relative to the .map file's own | ||
| // directory (per the sourcemap spec), not to the build outdir. | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
| import { existsSync, readFileSync } from "node:fs"; | ||
| import path from "node:path"; | ||
|
|
||
| describe("issue 3332: sourcemap sources are relative to the map file", () => { | ||
| test("nested outdir layout (monorepo-style root)", async () => { | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| using dir = tempDir("issue-3332", { | ||
| "xxx/package.json": JSON.stringify({ name: "xxx", dependencies: { "tiny-dep": "1.0.0" } }), | ||
| "xxx/node_modules/tiny-dep/package.json": JSON.stringify({ | ||
| name: "tiny-dep", | ||
| version: "1.0.0", | ||
| main: "index.js", | ||
| }), | ||
| "xxx/node_modules/tiny-dep/index.js": `module.exports = { dep: "tiny" };`, | ||
| "xxx/repos/livex/store/src/util.ts": `export const util = () => "util";`, | ||
| "xxx/repos/livex/web/src/index.ts": ` | ||
| import { util } from "../../store/src/util"; | ||
| import dep from "tiny-dep"; | ||
| console.log(util(), dep); | ||
| `, | ||
| "xxx/repos/livex/server/src/bundler/foo.ts": ` | ||
| import { util } from "../../../store/src/util"; | ||
| import dep from "tiny-dep"; | ||
| console.log("foo", util(), dep); | ||
| `, | ||
| }); | ||
|
|
||
| const root = path.join(String(dir), "xxx"); | ||
| const outdir = path.join(String(dir), "out"); | ||
|
|
||
| const result = await Bun.build({ | ||
| root, | ||
| entrypoints: [ | ||
| path.join(root, "repos/livex/web/src/index.ts"), | ||
| path.join(root, "repos/livex/server/src/bundler/foo.ts"), | ||
| ], | ||
| outdir, | ||
| sourcemap: "external", | ||
| }); | ||
| expect(result.success).toBe(true); | ||
|
|
||
| const maps = result.outputs.filter(o => o.path.endsWith(".map")); | ||
| expect(maps.length).toBe(2); | ||
|
|
||
| for (const map of maps) { | ||
| // Each map lands in a subdirectory of outdir mirroring the source tree, | ||
| // so its directory differs from outdir. That is the case the bug broke. | ||
| const mapDir = path.dirname(map.path); | ||
| expect(mapDir).not.toBe(outdir); | ||
|
|
||
| const json = JSON.parse(readFileSync(map.path, "utf8")); | ||
| expect(Array.isArray(json.sources)).toBe(true); | ||
| expect(json.sources.length).toBeGreaterThanOrEqual(3); | ||
|
|
||
| for (const src of json.sources as string[]) { | ||
| const resolved = path.resolve(mapDir, src); | ||
| // Every source entry must resolve to a real file from the map's own | ||
| // directory. Before the fix these resolved under out/repos/.../xxx/... | ||
| // which does not exist. | ||
| expect(existsSync(resolved), `source ${JSON.stringify(src)} in ${map.path} -> ${resolved}`).toBe(true); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test("flat outdir (single entrypoint in outdir root) still resolves", async () => { | ||
| using dir = tempDir("issue-3332-flat", { | ||
| "src/entry.ts": ` | ||
| import { helper } from "./lib/helper"; | ||
| console.log(helper()); | ||
| `, | ||
| "src/lib/helper.ts": `export const helper = () => "hi";`, | ||
| }); | ||
|
|
||
| const outdir = path.join(String(dir), "dist"); | ||
| const result = await Bun.build({ | ||
| entrypoints: [path.join(String(dir), "src/entry.ts")], | ||
| outdir, | ||
| sourcemap: "external", | ||
| }); | ||
| expect(result.success).toBe(true); | ||
|
|
||
| const [map] = result.outputs.filter(o => o.path.endsWith(".map")); | ||
| expect(map).toBeDefined(); | ||
| // With a single entrypoint and no `root`, the map sits directly in outdir. | ||
| expect(path.dirname(map.path)).toBe(outdir); | ||
|
|
||
| const json = JSON.parse(readFileSync(map.path, "utf8")); | ||
| expect(json.sources.length).toBeGreaterThanOrEqual(2); | ||
| for (const src of json.sources as string[]) { | ||
| const resolved = path.resolve(path.dirname(map.path), src); | ||
| expect(existsSync(resolved), `source ${JSON.stringify(src)} -> ${resolved}`).toBe(true); | ||
| } | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| }); | ||
|
|
||
| test("inline sourcemap in a nested chunk resolves from the map file", async () => { | ||
| using dir = tempDir("issue-3332-inline", { | ||
| "xxx/repos/app/src/util.ts": `export const util = () => 1;`, | ||
| "xxx/repos/app/src/entry.ts": ` | ||
| import { util } from "./util"; | ||
| console.log(util()); | ||
| `, | ||
| "xxx/repos/other/stub.ts": `export {};`, | ||
| }); | ||
|
|
||
| const root = path.join(String(dir), "xxx"); | ||
| const outdir = path.join(String(dir), "out"); | ||
| const result = await Bun.build({ | ||
| root, | ||
| entrypoints: [path.join(root, "repos/app/src/entry.ts"), path.join(root, "repos/other/stub.ts")], | ||
| outdir, | ||
| sourcemap: "inline", | ||
| }); | ||
| expect(result.success).toBe(true); | ||
|
|
||
| const entry = result.outputs.find(o => o.path.endsWith("entry.js")); | ||
| expect(entry).toBeDefined(); | ||
| const jsDir = path.dirname(entry!.path); | ||
| expect(jsDir).not.toBe(outdir); | ||
|
|
||
| const code = readFileSync(entry!.path, "utf8"); | ||
| const m = code.match(/sourceMappingURL=data:application\/json;[^,]+,([A-Za-z0-9+/=]+)/); | ||
| expect(m).toBeTruthy(); | ||
| const json = JSON.parse(Buffer.from(m![1], "base64").toString("utf8")); | ||
| expect(json.sources.length).toBeGreaterThanOrEqual(2); | ||
| for (const src of json.sources as string[]) { | ||
| const resolved = path.resolve(jsDir, src); | ||
| expect(existsSync(resolved), `source ${JSON.stringify(src)} -> ${resolved}`).toBe(true); | ||
| } | ||
| }); | ||
|
|
||
| test("relative outdir with nested chunks", async () => { | ||
| // `Bun.build` stores a relative `outdir` verbatim; the sources-base join | ||
| // must still produce an absolute chunk directory. Run in a subprocess so | ||
| // the test can chdir without touching the harness cwd. | ||
| using dir = tempDir("issue-3332-reloutdir", { | ||
| "proj/a/util.ts": `export const u = 1;`, | ||
| "proj/a/entry.ts": `import { u } from "./util"; console.log(u);`, | ||
| "proj/b/entry.ts": `console.log("b");`, | ||
| "build.ts": ` | ||
| import path from "node:path"; | ||
| import { readFileSync, existsSync } from "node:fs"; | ||
| const root = path.join(import.meta.dir, "proj"); | ||
| const res = await Bun.build({ | ||
| root, | ||
| entrypoints: [path.join(root, "a/entry.ts"), path.join(root, "b/entry.ts")], | ||
| outdir: "dist", | ||
| sourcemap: "external", | ||
| }); | ||
| if (!res.success) { console.error(res.logs); process.exit(1); } | ||
| const map = res.outputs.find(o => o.path.endsWith(path.join("a", "entry.js.map"))); | ||
| if (!map) { console.error("no map for a/entry"); process.exit(1); } | ||
| const mapDir = path.dirname(map.path); | ||
| const json = JSON.parse(readFileSync(map.path, "utf8")); | ||
| if (json.sources.length < 2) { console.error("too few sources", json.sources); process.exit(1); } | ||
| for (const s of json.sources) { | ||
| const r = path.resolve(mapDir, s); | ||
| if (!existsSync(r)) { console.error("MISS", s, "->", r); process.exit(1); } | ||
| } | ||
| console.log("OK " + json.sources.length); | ||
| `, | ||
| }); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "build.ts"], | ||
| 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(stdout.trim()).toBe("OK 2"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.