Repository navigation
Conversation
…t the dedup graph When the entrypoint starts with '#!/usr/bin/env bun', ParseTask overrides that file's target to Bun. Its imports are then registered in build_graphs[Bun]. But when those imports are parsed, the target determination fell through to topts.target (the build target, e.g. Node), so their own imports were looked up in build_graphs[Node] and not found, causing a second parse and a second copy of any module reachable both directly from the entry and from another import. The fallback used to be task.known_target (the target the importer registered this file under) and was changed to bundler.options.target in 514d37b as part of the server-components refactor. Restoring known_target as the fallback keeps every module in one dedup graph for the hashbang case, while the explicit server-components / use-directive branch still selects the browser transpiler's target. Fixes the duplicated source entry (and duplicated sourcesContent) in the generated source map, and fixes the runtime identity break where the two copies of a class were distinct constructors. Also fixes #25767: dependencies now inherit the entry's Bun target and are printed with escaped non-ASCII identifiers, so the '// @Bun' pragma's Latin-1 promise holds for the whole chunk.
|
Updated 12:59 PM PT - Jul 9th, 2026
❌ @robobun, your commit 7a440ff has 2 failures in
🧪 To try this PR locally: bunx bun-pr 33862That installs a local version of the PR into your bun-33862 --bun |
WalkthroughThe PR refactors target resolution in Bun's bundler ParseTask for hashbang entry files, introducing a ChangesHashbang Target Resolution
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
|
This PR may be a duplicate of:
🤖 Generated with Claude Code |
|
Not a duplicate of #33859. They touch different files and fix different bugs that happen to share a trigger (hashbang entry with a non-bun build target):
This PR also happens to resolve #25767 because dependencies now inherit the entry's Bun target and are printed with escaped identifiers, so the pragma's Latin-1 promise holds. But the module-duplication fix here is the primary change and is not covered by #33859. |
…lving its imports Follow-up to the known_target propagation change: the hashbang entry itself is registered only in build_graphs[build_target] by enqueue_entry_item, but its descendants now resolve against build_graphs[Bun]. A descendant that circularly imports the entry would miss and allocate a second source index for it. Before resolving a file's imports, ensure the file's own path is present in build_graphs[ast.target] so circular edges dedupe. Server-component boundaries are excluded because on_parse_task_complete intentionally maps their path to the generated reference proxy instead.
There was a problem hiding this comment.
The circular-entry fix in db87d46 looks right and the new tests cover it — no further issues from me, but given this touches the bundler's per-target dedup-graph registration on every parse (and threads through the server-components boundary logic), it's worth a human sign-off.
Extended reasoning...
Overview
This PR changes how ast.target is determined for parsed files in the bundler (src/bundler/ParseTask.rs) and adds a mirroring step in run_resolution_for_parse_task (src/bundler/bundle_v2.rs) that registers each parsed file's path in build_graphs[ast.target] before resolving its imports. Six new itBundled tests in test/bundler/bundler_edgecase.test.ts cover the diamond-import, export-star barrel, and circular-back-to-entry topologies for both node and browser build targets with a #!/usr/bin/env bun entry.
I previously flagged that the first revision regressed circular imports back to the entry; the author confirmed with a repro and addressed it in db87d46 by mirroring into build_graphs[ast.target] (gated on use_directive == None so server-component boundaries still map to the generated proxy). I re-checked the follow-up: the key (result.source.path.text) matches how every other call site keys path_to_source_index_map, and in the common case (ast.target == registration target) the get().is_none() guard makes it a no-op. The bug hunter found nothing on this revision.
Security risks
None identified. This is bundler graph bookkeeping — no auth, crypto, network, or untrusted-input parsing surface changes.
Level of scrutiny
Medium-high. The change is small in line count but sits in core bundler graph construction that runs for every parsed file, and it interacts with two subtle subsystems: hashbang-driven target flipping and server-components' separate-SSR-graph handling. The first revision introduced a real regression (circular entry re-parse) that was only caught in review, which suggests the invariants here are non-obvious. The author explicitly scoped out a related multi-entry design tension.
Other factors
Good test coverage was added for all three topologies raised in review. The ParseTask.rs change restores pre-514d37b3d2 behavior for the non-server-components branch, and the bundle_v2.rs mirroring is defensive and narrowly gated. Still, per the approval criteria this is not a simple/mechanical change and touches a critical code path, so I'm deferring rather than approving.
There was a problem hiding this comment.
Thanks for the follow-up in db87d46 — the circular-entry case is now covered and I found nothing further, but the build_graphs[ast.target] mirroring in run_resolution_for_parse_task runs on every parse result and touches the core dedup-map invariants, so a maintainer familiar with the bundler graph should sign off.
Extended reasoning...
Overview
This PR changes how per-file ast.target is determined in src/bundler/ParseTask.rs (restoring task.known_target as the fallback so a #!/usr/bin/env bun entry's whole subtree keys into one build_graphs map), and — after my earlier review — adds a mirroring step in src/bundler/bundle_v2.rs::run_resolution_for_parse_task that inserts each parsed file's path into build_graphs[ast.target] before its imports are resolved. Six itBundled tests are added covering diamond, export *, and circular-back-to-entry topologies for both node and browser build targets.
Security risks
None. This is bundler graph bookkeeping — no auth, crypto, network, or untrusted-input parsing surface.
Level of scrutiny
High. The dedup map (path_to_source_index_map keyed by target) is the mechanism that guarantees a source file is parsed and emitted exactly once; getting it wrong produces silent runtime miscompiles (broken class identity, doubled side effects) rather than build errors. The follow-up commit adds an unconditional insertion on this map for every parse result gated only on use_directive == None, which interacts with server-components (where on_parse_task_complete deliberately remaps the same path to a generated proxy), the dev server, and multi-entry --splitting. The author scoped the guard carefully and the reasoning checks out against the code I traced, but this is exactly the kind of cross-cutting invariant a bundler maintainer should confirm.
Other factors
My earlier finding (circular import back to the hashbang entry re-parsing the entry) was addressed with a code fix plus dedicated CircularEntry tests, and the thread is resolved. The bug-hunting pass on the latest revision found nothing. The author also ran the bake/dev-server suites, which exercise the server-components path the guard is meant to preserve. The remaining multi-entry design tension is acknowledged and reasonably left out of scope. Net: I have no outstanding concerns of my own, but the change is not simple/mechanical enough for bot approval.
|
CI status: the bundler changes are green on every lane. The remaining red is unrelated flake on
plus already-flaky-tagged retries for Ready for review. |
|
Re-checked against main at 165dc9f now that #25767 is closed. The #25767 was closed after verifying the issue's literal repro, which has no hashbang. On main, a |
|
Verification against current main (731aa92). I applied this PR's six test cases on main without its source change:
With the source change applied on main, all six pass and the The branch now conflicts with main in #25767 was closed as fixed on main. That verification used the snippet from the issue body, which has no hashbang. That snippet does not reproduce the bug on 1.3.4 or on main. The hashbang case still reproduces on main, so I reopened the issue. This change fixes it as well: the dependency's string prints as |
|
Closing as stale: this PR conflicts with main, and its last commit is from 2026-07-09. The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR. |
What does this PR do?
Fixes a bundler bug where a module reachable from both a
#!/usr/bin/env bunentry and one of its non-entry imports is parsed and emitted twice, breaking runtime identity and duplicating the source in the generated source map.Also fixes #25767 as a consequence (see below).
Repro
The bundle contains
class Sharedand a renamedclass Shared2;ReExportedbinds to the second copy. The source map lists../shared.tsat two indices with the file's entire text duplicated insourcesContent.Cause
target_from_hashbangoverrides the entry file's target toBun(ParseTask.rs).run_resolution_for_parse_taskthen usesresult.ast.target(= Bun) to key the path-to-source-index map, soshared.tsandreexport.tsare registered inbuild_graphs[Bun].When those files are parsed, the target determination at
ParseTask.rsfalls through totopts.target(the build target, Node here), so theirast.targetis Node.reexport.tsthen resolves its import ofshared.tsagainstbuild_graphs[Node], does not find it, and allocates a second source index.Before 514d37b (the server-components refactor) the fallback was
task.known_target, which is the target the importer registered this file under, so the graph stayed unified.Fix
Restore
task.known_targetas the fallback. The server-components"use client"path (which deliberately switches a server-graph file to the browser transpiler) is preserved by capturing whether that reassignment happened and keepingtopts.targetfor that branch only.With the fix, every module transitively reachable from a hashbang-Bun entry inherits
ast.target == Bunand dedupes in one graph. This also means the printer escapes non-ASCII identifiers for those modules, which resolves #25767 (the// @bunpragma's Latin-1 promise now holds for the whole chunk, not just the entry). Related: #33859 addresses #25767 by suppressing the pragma instead; the two changes do not conflict.How did you verify your code works?
Added four
itBundledcases intest/bundler/bundler_edgecase.test.tscoveringtarget=nodeandtarget=browser, each with a direct import and anexport *barrel. Each asserts:true true(class identity preserved) or the expected value;class Shareddeclaration;sourcesarray has no duplicate entries.All four fail on the released binary (duplicate
class Shared2emitted) and pass with this change. Also ranbundler_bun,bundler_banner,bundler_browser,bundler_cjs2esm,bundler_barrel,bundler_npm,bake/dev-and-prod,bake/dev/bundle,bake/dev/react-response: all pass.no test proof · iteration 1 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/bundler/bundler_edgecase.test.ts