-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix crash in Bun.build when passing many custom conditions #30660
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟣 Pre-existing, non-blocking: the identical
if (cond) X else 0 + Yprecedence footgun also exists inloadersFromTransformOptionsin this same file (lines 1574-1577). It's harmless there — the under-reserved capacity is followed bygetOrPutValuecalls that grow the map, so it only costs an extra rehash — but since this PR exists specifically to eliminate this pattern, you may want to apply the same@intFromBoolfix there for consistency.Extended reasoning...
What the issue is
This PR fixes a Zig precedence footgun in
ESMConditions.initwhereif (allow_addons) 1 else 0 + conditions.lenparses asif (allow_addons) 1 else (0 + conditions.len). The exact same construct appears ~480 lines later in the same file, inloadersFromTransformOptions(src/bundler/options.zig:1574-1577):Zig's
elsebranch is greedy, so this parses as:Step-by-step walkthrough
Take
target = .bun(sotarget.isBun() == true):ifevaluates todefault_loader_ext_bun.len(= 2).elsebranch — including the nested browser check anddefault_loader_ext.len(= 16) — is skipped.input_loaders.extensions.len + 2.input_loaders.extensions.len + 2 + 0 + 16 = input_loaders.extensions.len + 18.Similarly for
target = .browser: the innerifreturnsdefault_loader_ext_browser.len(= 1) and again dropsdefault_loader_ext.len, givingextensions.len + 0 + 1instead ofextensions.len + 0 + 1 + 16. Onlytarget = .nodeaccidentally gets the right answer (both conditions false →0 + 0 + 16).Why this is not a crash (unlike the ESMConditions case)
The fixed bug in
ESMConditions.initwas dangerous because the under-reserved map was followed byputAssumeCapacitycalls for the user-supplied conditions. Here the safety story is different:stringHashMapFromArraysonly callsputAssumeCapacityforinput_loaders.extensions, andinput_loaders.extensions.lenis always the first addend, so it is always covered regardless of how the rest mis-parses.try loaders.getOrPutValue(...), which grows the map as needed.keys.len == 0, the common case),stringHashMapFromArraysskipsensureTotalCapacityentirely, so the hint is unused.So the only effect is a wrong capacity hint causing one or two extra rehashes on a ~20-entry map during one-time options initialization — no observable behavior change, no crash, not measurable.
Addressing the "out of scope" objection
It's fair to note that this PR doesn't touch
loadersFromTransformOptions, doesn't call into it, and the under-reservation there has zero functional impact — on its own it would not warrant a comment. The reason it's worth a non-blocking mention here is narrower than "same file": it is literally the same syntactic trap (else 0 + ...) that this PR's title and description identify as the root cause of a crash. Leaving an identical instance of the pattern a few hundred lines down means the next person to change thosegetOrPutValuecalls toputAssumeCapacity(a plausible micro-optimization) reintroduces the exact crash this PR fixes. Applying the same@intFromBoolrewrite there is a one-line consistency change, and the author already has the pattern in their editor.Suggested fix
(or equivalently, parenthesize each ternary). This is pre-existing and should not block the PR — purely a "while you're here" suggestion.