Panic: UTF-8 byte boundary error when editing files with Chinese characters

Resolved 💬 3 comments Opened Dec 26, 2025 by ChaosRealmsAI Closed Dec 29, 2025

Bug Description

Claude Code panics when editing files containing Chinese text. The status bar preview truncates strings at byte boundaries instead of character boundaries, causing a crash when the cut happens in the middle of a multi-byte UTF-8 character.

Error Message

thread '<unnamed>' panicked at /rustc/ed61e7d7e242494fb7057f2657300d9e77bb4fcb/library/core/src/str/mod.rs:833:21:
byte index 20 is not a char boundary; it is inside '体' (bytes 18..21) of `可以给文字斜体`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5, aborting
zsh: abort      claude --dangerously-skip-permissions -c

Steps to Reproduce

  1. Create/edit a file containing Chinese text, e.g., a comment like # 可以给文字斜体
  2. Ask Claude Code to edit/update that file
  3. During the "Whirring…" phase (status bar showing edit progress), the panic occurs

Root Cause Analysis

  • Chinese characters in UTF-8 are 3 bytes each
  • The character occupies bytes 18-20
  • Code attempts to slice at byte index 20, which is inside the character
  • Rust panics because you cannot slice a string at non-character boundaries

Expected Behavior

String truncation should respect UTF-8 character boundaries, e.g., using .chars().take(n) instead of byte slicing.

Environment

  • Claude Code version: 2.0.76
  • OS: macOS (Darwin 25.0.0)
  • Platform: darwin

Suggested Fix

Replace byte-based string slicing with character-aware truncation:

// Instead of:
text[0..20]

// Use:
text.char_indices()
    .take_while(|(i, _)| *i < 20)
    .map(|(_, c)| c)
    .collect::<String>()

Or use a crate like unicode-segmentation for proper grapheme cluster handling.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗