Repository navigation
Conversation
EString::javascript_length returned rope_len for any rope, but rope_len counts UTF-8 bytes, so `(DEFINE + "x").length` folded to the byte count when the define value (or any other UTF-8 segment) was non-ascii. The same happened to template literals folded through Template::fold, which flattens the rope but keeps rope_len set. Check every segment for ascii before using the byte length, the same check the non-rope UTF-8 case already did, and make fold.rs call the method instead of carrying its own copy of the logic.
|
Warning Review limit reached
Next review available in: 4 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (4)
Comment |
|
Reproduced on bun 1.4.0 with Fix is in this PR: |
There was a problem hiding this comment.
LGTM — small, correct fix that makes .length folding bail on non-ASCII UTF-8 rope segments instead of returning the byte count.
What was reviewed:
javascript_lengthrewrite: verified all previous cases (UTF-16, non-rope UTF-8 ascii/non-ascii, all-ascii rope) return the same result; only non-ascii ropes and flattened-but-still-rope_len-set templates now returnNone.- Confirmed the deleted
fold.rsshim was the only duplicate and the directstr_.javascript_length()call is unambiguous (single inherent method). - Third caller in
react_compiler/diagnostics/js_string.rsalready handlesNonewith a fallback. - Tests cover head/tail non-ascii segments, template path, 4-byte codepoint, and ascii ropes still folding.
Extended reasoning...
Overview
Rewrites EString::javascript_length in src/ast/e.rs to walk every rope segment and return None when any UTF-8 segment contains a non-ASCII byte, instead of blindly returning rope_len (a byte count). Deletes the private duplicate e_string_javascript_length in src/js_parser/fold.rs and calls the shared method. Adds tests in bundler_minify.test.ts and transpiler.test.js.
Security risks
None. Pure constant-folding logic in the minifier; no untrusted input handling, allocation, or FFI.
Level of scrutiny
Low-medium. The function is ~10 lines and the change is a strict tightening: every case that previously returned a correct Some(n) still does (UTF-16 → slice16().len(); all-ASCII UTF-8, roped or not → len() which equals byte count which equals code-unit count for ASCII). The only behavioral change is that non-ASCII UTF-8 ropes (and templates flattened via resolve_rope_if_needed, which leaves rope_len set) now return None instead of the wrong byte count. None is always safe — it just leaves .length in the output.
Other factors
- Traced
len()andrope_leninvariants:pushsetsrope_lento the byte total and always setsnexton the head;resolve_rope_if_neededflattensdatato matchrope_lenbefore clearingnext. SoSome(self.len())is correct for the all-ASCII case whether the rope is resolved or not. - Checked the third caller (
react_compiler/diagnostics/js_string.rs::len_utf16) — it already falls back onNone, and only sees moreNones where it previously got wrong answers. - Tests exercise the variant matrix (non-ascii at head, in tail, as sole segment, template path, emoji surrogate pair) and confirm all-ascii ropes still fold to a number. PR description states both fail on 1.4.0 and pass with the change.
- The
fold.rsshim deletion is exactly the "one implementation" cleanup REVIEW.md asks for; verified only onefn javascript_lengthremains so the direct method call compiles.
|
Updated 7:05 AM PT - Aug 15th, 2026
❌ @robobun, your commit afe356b has some failures in 🧪 To try this PR locally: bunx bun-pr 38989That installs a local version of the PR into your bun-38989 --bun |
|
Updated 10:01 AM PT - Aug 15th, 2026
✅ @robobun, your commit afe356bcbe00287b106816324eac9e44fc6145ac passed in 🧪 To try this PR locally: bunx bun-pr 38989That installs a local version of the PR into your bun-38989 --bun |
|
Two open PRs cover the
This PR merges cleanly with main 3f3a9ff. It conflicts with each of the two in |
Problem
--minify-syntax,.lengthon a constant-folded string concatenation folds to the UTF-8 byte count when one piece is a non-ascii string stored as UTF-8:bun build --minify-syntax --define 'X="é"'turns(X + "x").lengthinto3, and`${X}x`.lengthinto3. The JS value is2.E::Strings:--definevalues,.env/process.envinlining, inlinedimport.meta.dir/file/path/url, stringified regexps and bigints.EString::javascript_length(src/ast/e.rs) returnedrope_lenfor any rope, butrope_lenis the byte total of the segments (EString::push,fold_string_addition.rs) and nothing makes those bytes ascii.Template::foldflattens its rope withresolve_rope_if_needed, which leavesrope_lenset, so a folded template took the same branch with non-asciidata.src/js_parser/fold.rscarried a private copy of the function with the same bug; that copy is what the.lengthrewrite called.Fix
javascript_lengthwalks the segments of a UTF-8 string and returnsNoneas soon as one contains a non-ascii byte; otherwise the byte length is the JS length. UTF-16 strings are unchanged (they are never ropes and already count code units).Noneleaves.lengthin the output, which is what a non-rope non-ascii string already did (X.lengthprinted as"é".lengthbefore this change).("a" + "b").length->2); for them the walk is theis_all_asciiscan the non-rope case already did.fold.rscalls the method instead of its copy, so there is one implementation.test/bundler/transpiler/transpiler.test.js("rewrite string to length with non-ascii utf-8 strings from define") andtest/bundler/bundler_minify.test.ts(minify/StringLengthFoldingNonAsciiDefine). They cover a non-ascii define segment at the head of the rope, in the tail, the template literal path, a 4-byte (2 code unit) character, and ascii ropes still folding. Both fail on bun 1.4.0 (3/5printed) and pass with this change.transpiler.test.js,bundler_minify.test.tsandbundler_string.test.tsin full (291 pass), andcargo clippy -p bun_ast -p bun_js_parser(clean).Background
fold_string_additionfolds"a" + "b"by linking the rightE::Stringonto the left one through itsnext/endpointers instead of copying bytes.rope_lencaches the byte total of the chain; the printer (orresolve_rope_if_needed) flattens it later.E::Stringstores its text either as UTF-8 bytes or as UTF-16 code units (is_utf16).len()is bytes in the first case and code units in the second. Only UTF-8 strings are joined into ropes; the lexer stores any source literal containing non-ascii as UTF-16, while defines and the other synthesized strings go throughEString::initand stay UTF-8..lengthcounts UTF-16 code units:"é"is 2 bytes of UTF-8 and 1 code unit,"😋"is 4 bytes and 2 code units.Repro on bun 1.4.0
With this change: