From cced5c3e763f82cfb30139b47d1260caf14adf63 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 11 Sep 2026 01:32:54 +0000 Subject: [PATCH 1/4] Escape object keys the same way for Latin-1 and UTF-16 strings in bun:test and Bun.inspect The bun:test value formatter and the console formatter sent a key stored as Latin-1 through the JSON string escaper, and wrote a key stored as UTF-16 raw. A quote, a backslash, or a control character in a UTF-16 key was not escaped. JSC can store an ASCII-only string as UTF-16, so two equal keys could print differently. Both formatters now send both forms through the same escaper over the UTF-8 form of the key. The output for a Latin-1 key does not change. --- src/jsc/ConsoleObject.rs | 38 +------------ src/runtime/test_runner/pretty_format.rs | 35 +----------- .../test/snapshot-tests/bun-snapshots.test.ts | 56 +++++++++++++++++++ test/js/bun/util/inspect.test.js | 21 +++++++ 4 files changed, 83 insertions(+), 67 deletions(-) diff --git a/src/jsc/ConsoleObject.rs b/src/jsc/ConsoleObject.rs index 2dbefc463781..0d9f7336bfa1 100644 --- a/src/jsc/ConsoleObject.rs +++ b/src/jsc/ConsoleObject.rs @@ -2700,19 +2700,6 @@ pub mod formatter { pub(crate) fn write_string(&mut self, str: &bun_core::String) { self.print(format_args!("{str}")); } - - #[inline] - pub(crate) fn write_16_bit(&mut self, input: &[u16]) { - // `format_utf16_type` requires `impl fmt::Write + Sized`; route through - // the `Display` adapter so we go via `bun_io::Write::write_fmt` instead. - self.print(format_args!( - "{}", - bun_core::fmt::FormatUTF16 { - buf: input, - path_fmt_opts: None - } - )); - } } const INDENTATION_BUF: [u8; 64] = [b' '; 64]; @@ -2980,34 +2967,15 @@ pub mod formatter { key, pfmt!(": ", C), )); - } else if key.is_16bit() { - let mut utf16_slice = key.utf16_slice(); - - writer.add_for_new_line(utf16_slice.len() + 2); - - if C { - writer.write_all(pfmt!("", true).as_bytes()); - } - - writer.write_all(b"\""); - - const QUOTE_U16: &[u16] = &[b'"' as u16]; - while let Some(j) = strings::index_of_any16(utf16_slice, QUOTE_U16) { - writer.write_16_bit(&utf16_slice[0..j]); - writer.write_all(b"\""); - utf16_slice = &utf16_slice[j + 1..]; - } - - writer.write_16_bit(utf16_slice); - - writer.print(format_args!("{}", pfmt!("\": ", C))); } else { writer.add_for_new_line(key.len + 2); + // One escaper over the UTF-8 form, so a key prints the same + // whether JSC stores it as Latin-1 or as UTF-16. writer.print(format_args!( "{}{}{}", pfmt!("", C), - bun_core::fmt::format_json_string_latin1(key.slice()), + bun_core::fmt::format_json_string_utf8(&key.to_utf8(), Default::default()), pfmt!(": ", C), )); } diff --git a/src/runtime/test_runner/pretty_format.rs b/src/runtime/test_runner/pretty_format.rs index ea6af49be92a..26f22cd2d7de 100644 --- a/src/runtime/test_runner/pretty_format.rs +++ b/src/runtime/test_runner/pretty_format.rs @@ -707,21 +707,6 @@ impl<'w, W: bun_io::Write> WrappedWriter<'w, W> { pub(crate) fn write_string(&mut self, str: &bun_core::String) { self.print(format_args!("{}", str)); } - - #[inline] - pub(crate) fn write_16_bit(&mut self, input: &[u16]) { - // `format_utf16_type` writes through `fmt::Write`; buffer to a `String` - // and forward bytes (UTF-16 β†’ UTF-8 conversion is the point, so the - // intermediate allocation is unavoidable without a `bun_io::Write` overload). - let mut buf = String::new(); - if bun_fmt::format_utf16_type(input, &mut buf).is_err() { - self.failed = true; - return; - } - if self.ctx.write_all(buf.as_bytes()).is_err() { - self.failed = true; - } - } } use bun_io::AsFmt; @@ -967,29 +952,15 @@ impl<'a, 'f, W: bun_io::Write, const ENABLE_ANSI_COLORS: bool> pretty_fmt_const!(ENABLE_ANSI_COLORS, ""), pretty_fmt_const!(ENABLE_ANSI_COLORS, ""), )); - } else if key.is_16bit() { - let utf16_slice = key.utf16_slice(); - - this.add_for_new_line(utf16_slice.len() + 2); - - if ENABLE_ANSI_COLORS { - writer.write_all(pretty_fmt_const!(true, "").as_bytes()); - } - - writer.write_all(b"\""); - writer.write_16_bit(utf16_slice); - writer.print(format_args!( - "\"{}:{} ", - pretty_fmt_const!(ENABLE_ANSI_COLORS, ""), - pretty_fmt_const!(ENABLE_ANSI_COLORS, ""), - )); } else { this.add_for_new_line(key.len + 2); + // One escaper over the UTF-8 form, so a key prints the same + // whether JSC stores it as Latin-1 or as UTF-16. writer.print(format_args!( "{}{}{}:{} ", pretty_fmt_const!(ENABLE_ANSI_COLORS, ""), - bun_fmt::format_json_string_latin1(key.slice()), + bun_fmt::format_json_string_utf8(&key.to_utf8(), Default::default()), pretty_fmt_const!(ENABLE_ANSI_COLORS, ""), pretty_fmt_const!(ENABLE_ANSI_COLORS, ""), )); diff --git a/test/js/bun/test/snapshot-tests/bun-snapshots.test.ts b/test/js/bun/test/snapshot-tests/bun-snapshots.test.ts index 41bbca2fcd5a..e9f97caa6cf9 100644 --- a/test/js/bun/test/snapshot-tests/bun-snapshots.test.ts +++ b/test/js/bun/test/snapshot-tests/bun-snapshots.test.ts @@ -33,6 +33,62 @@ test("ArrayBuffer values are serialized like typed arrays", () => { `); }); +test("object keys get the same escapes in Latin-1 and UTF-16 strings", () => { + // A string decoded from UTF-16 bytes keeps 16-bit storage, even when every character is ASCII. + const utf16 = (s: string) => Buffer.from(s, "utf16le").toString("utf16le"); + const special = '"\\\n\x1b'; + const value = { + [utf16(["ascii in utf16 ", special].join(""))]: 1, + ["latin1 " + special]: 2, + ["ζ—₯本 " + special]: 3, + ["πŸ˜€ " + special]: 4, + }; + expect(value).toMatchInlineSnapshot(` + { + "ascii in utf16 \\"\\\\\\n\\u001B": 1, + "latin1 \\"\\\\\\n\\u001B": 2, + "ζ—₯本 \\"\\\\\\n\\u001B": 3, + "πŸ˜€ \\"\\\\\\n\\u001B": 4, + } + `); + + // A matcher failure prints the value too. toEqual uses the snapshot formatter. toBe uses the console formatter. + const failure = (fn: () => void) => { + try { + fn(); + } catch (e) { + return Bun.stripANSI((e as Error).message); + } + }; + expect(failure(() => expect(value).toEqual(0))).toMatchInlineSnapshot(` + "expect(received).toEqual(expected) + + - 0 + + { + + "ascii in utf16 \\"\\\\\\n\\u001B": 1, + + "latin1 \\"\\\\\\n\\u001B": 2, + + "ζ—₯本 \\"\\\\\\n\\u001B": 3, + + "πŸ˜€ \\"\\\\\\n\\u001B": 4, + + } + + - Expected - 1 + + Received + 6 + " + `); + expect(failure(() => expect(value).toBe(0))).toMatchInlineSnapshot(` + "expect(received).toBe(expected) + + Expected: 0 + Received: { + "ascii in utf16 \\"\\\\\\n\\u001B": 1, + "latin1 \\"\\\\\\n\\u001B": 2, + "ζ—₯本 \\"\\\\\\n\\u001B": 3, + "πŸ˜€ \\"\\\\\\n\\u001B": 4, + } + " + `); +}); + describe("toMatchSnapshot errors", () => { it("should throw if property matchers exist and received is not an object", () => { expect(() => { diff --git a/test/js/bun/util/inspect.test.js b/test/js/bun/util/inspect.test.js index a1a05dbfa3ca..cd269623afe5 100644 --- a/test/js/bun/util/inspect.test.js +++ b/test/js/bun/util/inspect.test.js @@ -160,6 +160,27 @@ it("utf16 property name", () => { expect(Bun.inspect(db.prepare("select 'πŸ˜€' as 笑").all())).toBe(output); }); +it("property keys get the same escapes in Latin-1 and UTF-16 strings", () => { + // A string decoded from UTF-16 bytes keeps 16-bit storage, even when every character is ASCII. + const utf16 = s => Buffer.from(s, "utf16le").toString("utf16le"); + const special = '"\\\n\x1b'; + expect( + Bun.inspect({ + [utf16(["ascii in utf16 ", special].join(""))]: 1, + ["latin1 " + special]: 2, + ["ζ—₯本 " + special]: 3, + ["πŸ˜€ " + special]: 4, + }), + ).toMatchInlineSnapshot(` + "{ + "ascii in utf16 \\"\\\\\\n\\u001B": 1, + "latin1 \\"\\\\\\n\\u001B": 2, + "ζ—₯本 \\"\\\\\\n\\u001B": 3, + "πŸ˜€ \\"\\\\\\n\\u001B": 4, + }" + `); +}); + it("latin1", () => { expect(Bun.inspect("English")).toBe('"English"'); expect(Bun.inspect("FranΓ§ais")).toBe('"FranΓ§ais"'); From 879ef68300e457718d3d6be972909af26d01fc3f Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 11 Sep 2026 10:46:15 +0000 Subject: [PATCH 2/4] Remove index_of_any16 The console key printer was its last caller outside Windows-only code. The two Windows callers now call index_of_any_t, which it wrapped. --- src/bun_core/string/immutable.rs | 4 ---- src/runtime/cli/run_command.rs | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/bun_core/string/immutable.rs b/src/bun_core/string/immutable.rs index 3de1518006d3..a6f1b1f65dd3 100644 --- a/src/bun_core/string/immutable.rs +++ b/src/bun_core/string/immutable.rs @@ -403,10 +403,6 @@ pub fn contains_any(slice: &[u8], chars: &[u8]) -> bool { index_of_any(slice, chars).is_some() } -pub fn index_of_any16(self_: &[u16], chars: &[u16]) -> Option { - index_of_any_t(self_, chars) -} - pub fn index_of_any_t(str: &[T], chars: &[T]) -> Option { if let (Lanes::U8(s), Lanes::U8(c)) = (lanes(str), lanes(chars)) { return index_of_any(s, c); diff --git a/src/runtime/cli/run_command.rs b/src/runtime/cli/run_command.rs index c0a303130aa4..44b8bebe7b34 100644 --- a/src/runtime/cli/run_command.rs +++ b/src/runtime/cli/run_command.rs @@ -3877,7 +3877,7 @@ impl BunXFastPath { // Trigger quoting only on // space/tab/quote β€” compare the FULL u16, not the truncated low byte. - let needs_quote = strings::index_of_any16(warg, bun_core::w!(" \t\"")).is_some(); + let needs_quote = strings::index_of_any_t(warg, bun_core::w!(" \t\"")).is_some(); if !needs_quote { buffer[..warg.len()].copy_from_slice(warg); @@ -3885,7 +3885,7 @@ impl BunXFastPath { } // Fast path: no embedded `"`/`\` β†’ simple wrap. - let has_quote_or_backslash = strings::index_of_any16(warg, bun_core::w!("\"\\")).is_some(); + let has_quote_or_backslash = strings::index_of_any_t(warg, bun_core::w!("\"\\")).is_some(); if !has_quote_or_backslash { buffer[0] = b'"' as u16; buffer[1..1 + warg.len()].copy_from_slice(warg); From d160104ad7c5e36e9dab6b69fcbe559c72aa06be Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 11 Sep 2026 10:48:45 +0000 Subject: [PATCH 3/4] Drop the comment above the key escaper call in both printers --- src/jsc/ConsoleObject.rs | 2 -- src/runtime/test_runner/pretty_format.rs | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/jsc/ConsoleObject.rs b/src/jsc/ConsoleObject.rs index 0d9f7336bfa1..df2b9d9de43f 100644 --- a/src/jsc/ConsoleObject.rs +++ b/src/jsc/ConsoleObject.rs @@ -2970,8 +2970,6 @@ pub mod formatter { } else { writer.add_for_new_line(key.len + 2); - // One escaper over the UTF-8 form, so a key prints the same - // whether JSC stores it as Latin-1 or as UTF-16. writer.print(format_args!( "{}{}{}", pfmt!("", C), diff --git a/src/runtime/test_runner/pretty_format.rs b/src/runtime/test_runner/pretty_format.rs index 26f22cd2d7de..130f3b1560f9 100644 --- a/src/runtime/test_runner/pretty_format.rs +++ b/src/runtime/test_runner/pretty_format.rs @@ -955,8 +955,6 @@ impl<'a, 'f, W: bun_io::Write, const ENABLE_ANSI_COLORS: bool> } else { this.add_for_new_line(key.len + 2); - // One escaper over the UTF-8 form, so a key prints the same - // whether JSC stores it as Latin-1 or as UTF-16. writer.print(format_args!( "{}{}{}:{} ", pretty_fmt_const!(ENABLE_ANSI_COLORS, ""), From fe60f18e6200430be7244c1c8234905dcada4fc6 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 11 Sep 2026 10:59:02 +0000 Subject: [PATCH 4/4] test: cover U+2028, U+2029, and U+FEFF in a UTF-16 key --- .../test/snapshot-tests/bun-snapshots.test.ts | 23 +++++++++++-------- test/js/bun/util/inspect.test.js | 11 +++++---- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/test/js/bun/test/snapshot-tests/bun-snapshots.test.ts b/test/js/bun/test/snapshot-tests/bun-snapshots.test.ts index e9f97caa6cf9..31f5b731307a 100644 --- a/test/js/bun/test/snapshot-tests/bun-snapshots.test.ts +++ b/test/js/bun/test/snapshot-tests/bun-snapshots.test.ts @@ -40,15 +40,18 @@ test("object keys get the same escapes in Latin-1 and UTF-16 strings", () => { const value = { [utf16(["ascii in utf16 ", special].join(""))]: 1, ["latin1 " + special]: 2, - ["ζ—₯本 " + special]: 3, - ["πŸ˜€ " + special]: 4, + // A Latin-1 string cannot hold these three characters. + ["utf16 only \u2028\u2029\ufeff"]: 3, + ["ζ—₯本 " + special]: 4, + ["πŸ˜€ " + special]: 5, }; expect(value).toMatchInlineSnapshot(` { "ascii in utf16 \\"\\\\\\n\\u001B": 1, "latin1 \\"\\\\\\n\\u001B": 2, - "ζ—₯本 \\"\\\\\\n\\u001B": 3, - "πŸ˜€ \\"\\\\\\n\\u001B": 4, + "utf16 only \\u2028\\u2029\\uFEFF": 3, + "ζ—₯本 \\"\\\\\\n\\u001B": 4, + "πŸ˜€ \\"\\\\\\n\\u001B": 5, } `); @@ -67,12 +70,13 @@ test("object keys get the same escapes in Latin-1 and UTF-16 strings", () => { + { + "ascii in utf16 \\"\\\\\\n\\u001B": 1, + "latin1 \\"\\\\\\n\\u001B": 2, - + "ζ—₯本 \\"\\\\\\n\\u001B": 3, - + "πŸ˜€ \\"\\\\\\n\\u001B": 4, + + "utf16 only \\u2028\\u2029\\uFEFF": 3, + + "ζ—₯本 \\"\\\\\\n\\u001B": 4, + + "πŸ˜€ \\"\\\\\\n\\u001B": 5, + } - Expected - 1 - + Received + 6 + + Received + 7 " `); expect(failure(() => expect(value).toBe(0))).toMatchInlineSnapshot(` @@ -82,8 +86,9 @@ test("object keys get the same escapes in Latin-1 and UTF-16 strings", () => { Received: { "ascii in utf16 \\"\\\\\\n\\u001B": 1, "latin1 \\"\\\\\\n\\u001B": 2, - "ζ—₯本 \\"\\\\\\n\\u001B": 3, - "πŸ˜€ \\"\\\\\\n\\u001B": 4, + "utf16 only \\u2028\\u2029\\uFEFF": 3, + "ζ—₯本 \\"\\\\\\n\\u001B": 4, + "πŸ˜€ \\"\\\\\\n\\u001B": 5, } " `); diff --git a/test/js/bun/util/inspect.test.js b/test/js/bun/util/inspect.test.js index cd269623afe5..03534b3b82af 100644 --- a/test/js/bun/util/inspect.test.js +++ b/test/js/bun/util/inspect.test.js @@ -168,15 +168,18 @@ it("property keys get the same escapes in Latin-1 and UTF-16 strings", () => { Bun.inspect({ [utf16(["ascii in utf16 ", special].join(""))]: 1, ["latin1 " + special]: 2, - ["ζ—₯本 " + special]: 3, - ["πŸ˜€ " + special]: 4, + // A Latin-1 string cannot hold these three characters. + ["utf16 only \u2028\u2029\ufeff"]: 3, + ["ζ—₯本 " + special]: 4, + ["πŸ˜€ " + special]: 5, }), ).toMatchInlineSnapshot(` "{ "ascii in utf16 \\"\\\\\\n\\u001B": 1, "latin1 \\"\\\\\\n\\u001B": 2, - "ζ—₯本 \\"\\\\\\n\\u001B": 3, - "πŸ˜€ \\"\\\\\\n\\u001B": 4, + "utf16 only \\u2028\\u2029\\uFEFF": 3, + "ζ—₯本 \\"\\\\\\n\\u001B": 4, + "πŸ˜€ \\"\\\\\\n\\u001B": 5, }" `); });