Skip to content

bundler: take the label of the duplicate output path note from the chunk namer - #47306

Open
robobun wants to merge 1 commit into
robobun/77f76f8e/asset-output-path-claimfrom
robobun/77f76f8e/note-label-from-namer
Open

robobun wants to merge 1 commit into
robobun/77f76f8e/asset-output-path-claimfrom
robobun/77f76f8e/note-label-from-namer

Conversation

@robobun

@robobun robobun commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #46655, which must merge first.

Problem

  • The note under Multiple files share the same output path names the option to change. It can name the wrong one: two pages with equal stylesheets and --minify print entry naming is './[name]-[hash].[ext]', but only --chunk-naming moves those files.
  • The report chooses the label with its own test (src/bundler/linker_context/generateChunksInParallel.rs:515). compute_chunks chooses the template with another (computeChunks.rs:653).

Fix

  • computeChunks.rs has one rule, takes_entry_naming, for the namer and the new chunk_named_by. The report matches on chunk_named_by.
  • The entry point of an executable gets no naming note: no option sets its ./[name] template.
  • The report prints one note for each template of an option, not one for each option.
  • Verified: test/bundler/bundler_naming.test.ts, 7 of 8 new rows fail on bundler: add copied assets to the duplicate output path check #46655. cli and bun-build-api pass unedited. Self-reviewed: 14 concerns raised, 13 addressed, see Notes.

Background

  • --entry-naming names entry points and pages. --chunk-naming names the other chunks, also a page's script and stylesheet.
  • --compile names the entry point of the executable after the output file, and marks that chunk with a flag bit.
  • Not built: a third copy of the template test in the report, or a Chunk field that only a failed build reads.

Downsides

  • Failing builds print other notes than before. Outputs do not change (26 files compared).
  • --compile pays 1 instruction per build. Other builds pay none. .text grows by 512 bytes.
  • The build in the first bullet still fails: this PR corrects only its note.
Notes

How it was found. By inspection, while #46655 was checked. No user asked for this fix. Two closed issues show the wrong word in a paste (#17674 and #25330: entry naming is './chunk-[hash].[ext]' for stylesheet chunks). Those builds fail because two stylesheet chunks with equal bytes get one name. They fail after this PR too, with chunk naming is in the note.

Why its own PR, and why stacked. The wrong labels are older than #46655, and only the row ExecutableAndAsset needs its asset check. #46655 rewrites the same lines of the report, so a PR on main would edit them twice. Do not merge this PR into its base branch. After #46655 merges: git rebase --onto main 672fc42162.

The notes, before and after. "Before" is #46655 at 672fc42. Each row is a row of naming/CollisionNote.

Row Build Before Now
PageChunks two pages, naming.chunk = "js/[name].[ext]" entry naming is './js/[name].[ext]' chunk naming is './js/[name].[ext]'
PagesAndPageChunks the same with naming.entry = "[name].[ext]" entry naming is './js/[name].[ext]' only entry naming is './[name].[ext]' and chunk naming is './js/[name].[ext]'
ImportedPages two pages that only import() reaches chunk naming is './[name].[ext]' entry naming is './[name].[ext]'
ExecutableAndPageChunk executable app.js, page script at app.[ext] entry naming is './[name]' chunk naming is './app.[ext]'
ExecutableAndImportedChunk executable app, import() chunk at app entry naming is './[name]' and chunk naming is './app' chunk naming is './app'
ExecutableAndAsset executable app.bin that embeds data/app.bin, naming.asset = "[name].[ext]" entry naming is './[name]' and the asset note the asset note
TwoChunkTemplates executable with two pages and two lazy.ts, naming.chunk = "js/[name].[ext]" entry naming is './chunk-[hash].[ext]' and chunk naming is './js/[name].[ext]' both under chunk naming
EntryPointAndSharedChunk an entry point and the shared chunk with its name unchanged: chunk naming for the shared chunk. main says asset naming, #46655 changes that, and no test pinned it.

History. #10761 wrote the namer and the report with one condition. #15940 (HTML entry points) changed the condition in the namer only. #41264 (in 1.4.2) added the ./[name] arm to the namer only: the report then said entry naming is './[name]' when the executable was not the first output at its path. #41317 (in 1.4.3 and 1.4.4) lists the first output too, so that note prints for every collision with the executable.

Why a flag bit and an enum. The --compile arm depends on loop state (the first server-side entry point), so a test on one chunk cannot find it. Bit 3 of chunk::Flags was free. NamedBy is computed and never stored. takes_entry_naming is private to computeChunks.rs, so a caller cannot skip the bit, and the match in the report does not compile when a variant is added.

One note for each template. The report kept one template for each option. With the right labels, the fixed template that --compile gives the browser chunks (./chunk-[hash].[ext], src/bundler/bundle_v2.rs:2010) lands under chunk next to the user's --chunk-naming, and one replaced the other. The report now sorts the templates of an option and prints one note for each.

Measurements. Release builds of 672fc42 and of this commit, same flags.

  • Template decision in compute_chunks: 22 instructions for each JS entry chunk before, 22 after (ptrace single-step over the PC ranges of the block, with 1, 21 and 201 entry points, 10 runs each at 1 and 21, 2 runs at 201, all runs equal).
  • --compile arm: 70 -> 71 instructions with 1 entry point, 69 -> 70 with 21.
  • size_of::<Chunk>(): 792 B before and after. size_of::<LinkerOptions>(): 264 B before and after (rustc -Z print-type-sizes).
  • Release .text: 108,649,993 -> 108,650,505 B (+512). .rodata: +0. compute_chunks: 46,481 -> 46,489 B. generate_chunks_in_parallel::<false>: 40,549 -> 40,828 B (llvm-size -A, llvm-nm).
  • Allocator calls inside compute_chunks for the fixtures P0, P1 and P2 of bundler: add copied assets to the duplicate output path check #46655: 29, 2,428 and 70, before and after (ptrace counters on 11 mimalloc entry points). Whole process (gdb counters): P0 12,751 and P2 149,747 on both builds. P1 is 63,964 to 64,045 before and 63,985 to 64,051 after in 8 runs each: mi_heap_malloc on the worker threads differs from run to run.
  • diff -r of 4 builds from both binaries (splitting with default names, three [hash] templates, a --target=bun server with two pages, two pages): 26 files, 0 differ.
  • New rows: 7 of 8 fail on 672fc42 (EntryPointAndSharedChunk passes) and on bun 1.4.2 (ExecutableAndImportedChunk passes). 8 of 8 pass with this commit. Existing note assertions edited: 0.

Sites of the same class that this PR does not change.

  • Plain --outfile: bun build app.ts --outfile=dist/app.js --asset-naming=app.js with an imported asset prints entry naming is './app.js', consider adding '[hash]'. The CLI turns --outfile into that entry template. bun build: never derive an output directory from the entry point #41879 removes that rewrite and names the chunk in the ./[name] arm.
  • The browser chunks of an executable: the note says chunk naming is './chunk-[hash].[ext]', and --chunk-naming does not change that template. The templates have [hash], so the note is the identical content form and gives no remedy. bake production builds set all three templates the same way (src/runtime/bake/config/transpiler.rs:147).
  • bun build --no-bundle has its own report (Transpiler::reject_duplicate_output_paths). It advises [hash] for a template that has [hash]. That is handed off as a separate fix.
  • The asset note quotes the --asset-naming of the build. One asset takes its template from the transpiler of its target (bundle_v2.rs:4763).
  • No row pins the route of the first Problem bullet: it would pin that two stylesheet chunks with equal bytes fail the build.

Merge with #41879. #41879 defines a closure takes_entry_naming with the same condition inside compute_chunks, and gives plain --outfile the ./[name] arm. The two branches conflict in one line. The resolution: keep the module function, delete the closure, keep chunk.flags.insert(chunk::Flags::NAMED_BY_OUTFILE) in the arm. Keeping the closure compiles too, and leaves two copies of the rule.

Self-review. 14 concerns, 13 addressed:

  1. Delivery: this was one more commit on bundler: add copied assets to the duplicate output path check #46655. It is its own PR now.
  2. The conflict with bun build: never derive an output directory from the entry point #41879: resolution above.
  3. No test pinned the label of a shared chunk: row added.
  4. Other sites of the class: listed above.
  5. Reach: a default-naming build reaches the wrong label (first Problem bullet).
  6. The claim is that the note names the right option. It is not that the build works after the change.
  7. Found by inspection: stated.
  8. The ./[name] note is dated to compile: name the entry point's chunk after the outfile, so other chunks can load it #41264 and bundler: widen [hash] output names when two would collide; add [hashN] #41317.
  9. The asset row uses the template the docs print for executables.
  10. A relabeled chunk replaced the user's template in the note: one note for each template, row TwoChunkTemplates.
  11. The shared chunk row (same as 3).
  12. No row for the --minify route: reason above.
  13. ExecutableAndPageChunk passed without the executable: the build of that row now succeeds without compile.
  14. Not done: a note that names the placeholder to add ([dir] or [name]). The report has no fact for each group to decide that. A follow-up.

Suites run. Debug build of this commit: bundler_naming (41 pass, 2 todo), html-import-manifest, the compile/AssetNoOverwrite*, compile/AssetSameName*, compile/TextImportSameBasename and compile/HelloWorld* rows of bundler_compile, cli (3 --compile tests time out at 5 s there: they write a 1.6 GB debug executable) and bun-build-api (can be called thousands of times times out at 180 s there). Release build of this commit: cli, bun-build-api, html-import-manifest, standalone, test/internal/source-lints.


[human-review] gate passed · iteration 1 · 4 files touched

fails on main (without fix)
ASAN without fix: 7 failed, 2 skipped
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/pr_gate.xml" test/bundler/bundler_naming.test.ts
bun test v1.4.4 (367d939d9)

test/bundler/bundler_naming.test.ts:
(pass) bundler > naming/EntryNamingCollission [743.57ms]
118 |           entrypoints: entrypoints.map(entry => join(String(dir), entry)),
119 |           root: String(dir),
120 |           outdir: String(dir),
121 |           throw: false,
122 |         });
123 |         expect(logs.map(log => `${log.level}: ${log.message.split("\n")[0]}`)).toEqual([
                                                                                     ^
error: expect(received).toEqual(expected)

  [
    "error: Multiple files share the same output path",
-   "note: chunk naming is './js/[name].[ext]', consider adding '[hash]' to make filenames unique",
+   "note: entry naming is './js/[name].[ext]', consider adding '[hash]' to make filenames unique",
  ]

- Expected  - 1
+ Received  + 1

      at <anonymous> (/workspace/bun/test/bundler/bundler_naming.test.ts:123:80)
(fail) bundler > naming/CollisionNote > PageChunks [347.98ms]
118 |           entrypoint
... (truncated)

release without fix: 13 failed, 2 skipped
bun test v1.4.3-canary.1 (367d939d9)

test/bundler/bundler_naming.test.ts:
(pass) bundler > naming/EntryNamingCollission [270.33ms]
118 |           entrypoints: entrypoints.map(entry => join(String(dir), entry)),
119 |           root: String(dir),
120 |           outdir: String(dir),
121 |           throw: false,
122 |         });
123 |         expect(logs.map(log => `${log.level}: ${log.message.split("\n")[0]}`)).toEqual([
                                                                                     ^
error: expect(received).toEqual(expected)

  [
    "error: Multiple files share the same output path",
-   "note: chunk naming is './js/[name].[ext]', consider adding '[hash]' to make filenames unique",
+   "note: entry naming is './js/[name].[ext]', consider adding '[hash]' to make filenames unique",
  ]

- Expected  - 1
+ Received  + 1

      at <anonymous> (/workspace/bun/test/bundler/bundler_naming.test.ts:123:80)
(fail) bundler > naming/CollisionNote > PageChunks [233.92ms]
118 |           entrypoints: entrypoints.map(entry => join(String(dir), entry)),
119 |           root: String(dir),
120 |           outdir: String(dir),
121 |           throw: false,
12
... (truncated)
passes on PR (with fix)
ASAN with fix: 2 skipped
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/pr_gate.xml" test/bundler/bundler_naming.test.ts
bun test v1.4.4 (367d939d9)

test/bundler/bundler_naming.test.ts:
(pass) bundler > naming/EntryNamingCollission [855.20ms]
(pass) bundler > naming/CollisionNote > PageChunks [512.37ms]
(pass) bundler > naming/CollisionNote > PagesAndPageChunks [492.94ms]
(pass) bundler > naming/CollisionNote > ImportedPages [569.52ms]
(pass) bundler > naming/CollisionNote > EntryPointAndSharedChunk [660.42ms]
(pass) bundler > naming/CollisionNote > ExecutableAndPageChunk [619.70ms]
(pass) bundler > naming/CollisionNote > ExecutableAndImportedChunk [476.04ms]
(pass) bundler > naming/CollisionNote > ExecutableAndAsset [421.97ms]
(pass) bundler > naming/CollisionNote > TwoChunkTemplates [381.09ms]
(pass) bundler > naming/ImplicitOutbase1 [897.15ms]
(pass) bundler > naming/ImplicitOutbase2 [1060.74ms]
(pass) bundler > naming/EntryNamingTemplate1 [1065.97ms]
(todo) bundler > naming/EntryNamingTemplate2
(pass) bundler > naming/AssetNaming [533.65ms]
(pass) bundler > naming/AssetNamingMkdir [463.90ms]
(pass) bundler > naming
... (truncated)

release with fix: 2 skipped
$ bun scripts/build.ts --profile=release
[configured] bun-profile → bun (stripped)
  target       linux-x64-gnu
  build type   Release
  build dir    ./build/release
  revision     78b6cde570
  features     lto, baseline

21 deps, 432 codegen, 1228 objects in 9686ms

ninja: Entering directory `/workspace/bun/build/release'
[1/5] fetch WebKit (prebuilt)
[WebKit] up to date
[2/5] fetch rust-argon2
[rust-argon2] up to date
[3/5] fetch lolhtml
[lolhtml] up to date
[3/5] cargo plan → /workspace/bun/build/release/rust-target/plan.json
301 units: 203 lib, 32 custom-build (host), 28 run custom-build, 16 proc-macro (host), 17 lib (host), 4 run custom-build (host), 1 rlib
[4/5] reconfigure
[1/1621] rustc bun_output_tags 
[2/1621] rustc unicode_xid 
[3/1621] rustc fnv 
[4/1621] rustc unicode_ident 
[5/1621] rustc heck 
[6/1621] mkdir codegen
[7/1621] mkdir stamps
[8/1621] rustc ident_case 
[9/1621] rustc siphasher 
[10/1621] gen removable features
[11/1621] rustc fastrand 
[12/1621] install /workspace/bun
bun install v1.4.3-canary.1 (367d939d9)

Checked 22 installs across 44 packages (no changes) [1247.00ms]
[13/1621] rustc phf_shared 
[14/1621] esbuild runtime.out.js

  b
... (truncated)
diff hotspot
src/bundler/Chunk.rs                               |  14 ++-
 src/bundler/linker_context/computeChunks.rs        |  29 ++++--
 .../linker_context/generateChunksInParallel.rs     |  91 +++++++++---------
 test/bundler/bundler_naming.test.ts                | 107 +++++++++++++++++++++
 4 files changed, 188 insertions(+), 53 deletions(-)

gate history · 1 passed · 0 rejected · iteration 1

evidence per changed file
file                                                    reads  edits  tests
src/bundler/Chunk.rs                                        1      0     38
src/bundler/linker_context/computeChunks.rs                 1      0     38
src/bundler/linker_context/generateChunksInParallel.rs      9      0     45
test/bundler/bundler_naming.test.ts                         3      1     38

…unk namer

The note under `Multiple files share the same output path` names the
naming option to change. The report chose `entry` or `chunk` with its
own test. That test differs from the one in `compute_chunks` that chose
the template:

- The script and the stylesheet of a page printed
  `entry naming is '<chunk template>'` (since #15940).
- A page that only `import()` reaches printed
  `chunk naming is '<entry template>'` (since #15940).
- The entry point of an executable printed `entry naming is './[name]'`
  (since #41264, and for every collision with it since #41317). No
  naming option sets that template.
- The report kept one template for each option, so a second template
  under one option replaced the first.

`compute_chunks` now has one rule, `takes_entry_naming`. The namer and
`chunk_named_by` both call it, and the report matches on the result of
`chunk_named_by`. The chunk that `--compile` names after the output
file gets no naming note. The report prints one note for each template
of an option.
@robobun

robobun commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

@robobun

robobun commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: the diff is green. CI is red on lanes this PR does not touch. Stacked on #46655, which must merge first.

CI, build #134893: 190 of 196 jobs passed, and the tests of this PR passed on every lane.

  • 1 red job, macOS x64: test/cli/check/check.test.ts, "finishes within 10 s on conditions nested deeper than the stack allows". bun check was killed at the 10 s limit of that test. The same test failed once on Ubuntu 25.04 x64 in this build and passed on the retry. This PR does not touch bun check.
  • 5 red jobs are test-aot-drawn, the sample steps of the AOT compiler tests. They do not block a merge, and this PR does not touch the AOT compiler.

How to reproduce (bun 1.4.2, main, and #46655 at 672fc42), in an empty directory:

mkdir a b
for d in a b; do
  echo '<!doctype html><html><head><script type="module" src="./index.js"></script></head></html>' > $d/index.html
  echo "console.log('$d');" > $d/index.js
done
bun build a/index.html b/index.html --outdir=out --chunk-naming='js/[name].[ext]'; echo "exit $?"

Before:

error: Multiple files share the same output path
  ./js/index.js:
    from input a/index.html
    from input b/index.html

note: entry naming is './js/[name].[ext]', consider adding '[hash]' to make filenames unique

With this branch the last line is note: chunk naming is './js/[name].[ext]', consider adding '[hash]' to make filenames unique. The exit code stays 1.

A build with default names reaches the wrong label too. Give each page <link rel="stylesheet" href="./index.css"> and the same index.css, then run bun build a/index.html b/index.html --outdir=out --minify. Before, the note says entry naming is './[name]-[hash].[ext]'. --entry-naming does not move those files and --chunk-naming='[dir]/chunk-[hash].[ext]' does. With this branch the note says chunk naming is. That build still fails.

Tests: bun bd test test/bundler/bundler_naming.test.ts -t CollisionNote.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants