fix(stringutil): operate on runes instead of bytes in Truncate (#22388)#22468
fix(stringutil): operate on runes instead of bytes in Truncate (#22388)#22468johnstcn merged 1 commit intorelease/2.29from
Conversation
There was a problem hiding this comment.
Pull request overview
Updates string utilities and task name generation to be UTF-8 safe, preventing invalid UTF-8 from being produced when truncating multi-byte characters (fixing the reported task creation failure).
Changes:
- Update
stringutil.Truncateto truncate by runes (not bytes) and keep full-word truncation rune-safe. - Add multi-byte truncation test cases (including full-word + ellipsis behavior).
- Add
stringutil.Capitalizeand use it in task display name generation to avoid byte-slicing.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
coderd/util/strings/strings.go |
Make Truncate rune-aware; add Capitalize helper for rune-safe first-letter uppercasing. |
coderd/util/strings/strings_test.go |
Add multi-byte truncation coverage and unit tests for Capitalize. |
coderd/taskname/taskname.go |
Use strutil.Capitalize when finalizing display names. |
coderd/taskname/taskname_test.go |
Add regression test ensuring multi-byte prompt display name is not corrupted. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| runes := []rune(s) | ||
| if len(runes) <= n { | ||
| return s |
There was a problem hiding this comment.
Truncate converts the entire input to []rune up-front, which allocates and walks the whole string even when n is small (common for call sites like task name/display name generation). Consider a rune-safe approach that finds the byte index for the first maxLen runes by iterating with for i := range s (or counting runes until maxLen) so you can slice the original string without allocating a full rune slice.
Fixes #22375
Updates
stringutil.Truncateto properly handle multi-byte UTF-8 characters.Adds tests for multi-byte truncation with word boundary.
Created by Mux using Opus 4.6
(cherry picked from commit 0cfa037)