-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix capacity under-allocation in ESMConditions.init with user conditions #30479
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.
🟣 Heads up: the same
if ... else 0 + ...precedence footgun this PR fixes also exists inloadersFromTransformOptionsin this file (lines 1575-1578). It's harmless there — the under-reserved capacity still covers theputAssumeCapacitycalls and the remaining inserts usegetOrPutValuewhich grows the map — so it's just a missed pre-allocation, but you may want to fix both occurrences together. (Pre-existing, not introduced by this PR.)Extended reasoning...
What the bug is
This PR fixes a Zig operator-precedence footgun in
ESMConditions.initwhereif (allow_addons) 1 else 0 + conditions.lenparses asif (allow_addons) 1 else (0 + conditions.len). The identical pattern still exists in the same file atloadersFromTransformOptions(src/bundler/options.zig:1575-1578):In Zig grammar, an
IfExpr's else-branch greedily consumes a full expression, so each trailing+ ...binds into the precedingelserather than into the outer sum. The indentation suggests four addends, but the parser sees nested if-expressions.Step-by-step proof
Take
target = .bun(sotarget.isBun()is true,target == .browseris false). Withdefault_loader_ext_bun.len = 2,default_loader_ext_browser.len = 1,default_loader_ext.len = 18:extensions.len + (if (true) 2 else (0 + (if (false) 1 else (0 + 18)))).2. Everything afterelse(includingdefault_loader_ext.len) is discarded.extensions.len + 2.extensions.len + 2 + 0 + 18 = extensions.len + 20.So when
target.isBun(), the capacity hint drops 18 entries. (When the target is neither bun nor browser, the nested elses do happen to sum to0 + 0 + 18, which is accidentally correct.)Why it doesn't crash
Unlike
ESMConditions.init, this instance is benign:stringHashMapFromArraysonly callsputAssumeCapacityforkeys=input_loaders.extensions. Sinceinput_loaders.extensions.lenis always the first addend (outside anyif), the reserved capacity is always ≥ the number ofputAssumeCapacitycalls regardless of how the rest mis-parses.default_loader_ext/default_loader_ext_bun/default_loader_ext_browserinserts usetry loaders.getOrPutValue(...), which grows the map on demand.keys.len == 0(no user-supplied loaders, the common case),stringHashMapFromArraysskipsensureTotalCapacityentirely, so the hint is already best-effort.Impact
Zero correctness impact — purely a missed pre-allocation that causes one or two extra rehashes during options setup. No crash, no behavioral difference.
How to fix
Same approach as this PR — hoist the conditionals into locals so the addition is unambiguous:
This is pre-existing and untouched by the diff; mentioning it only because the PR's express purpose is eliminating this exact footgun in
options.zig, so fixing both occurrences together seems natural. Not a blocker.