-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix capacity miscalculation in ESMConditions.init #30604
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.
🟣 Nit (pre-existing, not introduced here): the same if-else precedence trap also lives ~480 lines down in
loadersFromTransformOptions—input_loaders.extensions.len + if (target.isBun()) ... else 0 + if (...) ... else 0 + default_loader_ext.len. That one is harmless because the subsequent inserts usegetOrPutValue(which grows), so it just under-reserves and triggers a couple of extra reallocations rather than overflowing. But since it's the identical root cause in the same file, you may want to give it the sameconst x: usize = if (...) ... else 0;treatment while you're here.Extended reasoning...
What the issue is
This PR correctly fixes the Zig if-else precedence bug in
ESMConditions.init, whereif (allow_addons) 1 else 0 + conditions.lenparses asif (allow_addons) 1 else (0 + conditions.len). However, the exact same pattern exists further down in the same file, inloadersFromTransformOptions:In Zig, the else-branch of an
ifexpression greedily consumes a full expression. Soa + if (c) x else 0 + yparses asa + (if (c) x else (0 + y)), not(a + (if (c) x else 0)) + y. The chained ifs here therefore do not produce the sum the indentation suggests.Step-by-step proof
Take
target = .bun(sotarget.isBun()is true andtarget == .browseris false), withinput_loaders.extensions.len = 3:if (target == .browser) default_loader_ext_browser.len else 0 + default_loader_ext.lenparses with the else-branch consuming0 + default_loader_ext.len. Since the condition is false, it evaluates to0 + 16 = 16.if (target.isBun()) default_loader_ext_bun.len else 0 + (16)— condition is true, so it evaluates todefault_loader_ext_bun.len = 2. The else-branch (0 + 16) is discarded entirely.total_capacity = input_loaders.extensions.len + 2 = 5.The intended value was
3 + 2 + 0 + 16 = 21. So the map is reserved for 5 entries instead of 21.Why it doesn't crash (unlike the ESMConditions case)
stringHashMapFromArraysonly doesputAssumeCapacityforkeys.lenitems (the user-supplied extensions). Sinceextensions.lenis always a term in the sum regardless of which if-branch is taken, the reserved capacity is always>= extensions.len, so those inserts never overflow.default_loader_ext/default_loader_ext_bun/default_loader_ext_browserinserts all usegetOrPutValue, which grows the map on demand.input_loaders.extensions.len == 0(no custom loaders),stringHashMapFromArraysskipsensureTotalCapacityentirely, so the reservation hint is ignored anyway.So the only impact is a few unnecessary reallocations during build setup when custom loaders are passed — no correctness or safety issue.
Why mention it
It's the identical root cause this PR is fixing (Zig if-else precedence in a capacity calculation), in the same file, and the same fix style applies cleanly:
This is pre-existing — the PR doesn't touch, call, or otherwise interact with
loadersFromTransformOptions— so it's purely a non-blocking "while you're here" suggestion, not something that should hold up the merge.