Repository navigation
Fix: Improve text encoding for shell output in VSCode preview (#6178) - #6182
Conversation
…#6178) Replace String::from_utf8_lossy() with intelligent encoding detection in StreamOutput::from_utf8_lossy() to fix garbled text in VSCode shell preview when running commands with non-ASCII characters in Windows/WSL. The new bytes_to_string_smart() function tries multiple encoding strategies: 1. UTF-8 (fast path for valid UTF-8) 2. Windows-1252 (common in Windows environments) 3. Latin-1 (fallback for extended ASCII) 4. Lossy UTF-8 (final fallback)
|
All contributors have signed the CLA ✍️ ✅ |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
Thanks for the contribution! Please sign the CLA, fix the CI failures (looks like a simple formatting issue), and take a look at the codex code review feedback. |
|
I have read the CLA Document and I hereby sign the CLA |
Apply cargo fmt to fix formatting issues: - Reorder module declarations alphabetically - Fix line length and spacing in text_encoding.rs - Add newline at end of file
Allow ESC (\x1b) character in Windows-1252 and Latin-1 detection to support colored shell output with ANSI escape sequences. This addresses the code review feedback that typical shell output embeds ANSI colour sequences alongside Windows-1252 characters, and the previous strict control character check was causing these to fall back to lossy UTF-8 conversion.
joshka-oai
left a comment
There was a problem hiding this comment.
General approval with the approach and code changes from a visual inspection. I haven't yet tested this however, but will later this week.
We already have encoding_rs in our dependency list, and it's a widely used package (275M downloads). Chardetng is by the same developer.
|
@codex review I've modified this to use encoding_rs and chardetng (another crate by the same developer as encoding_rs that detects the character sets). Codex rewrote most of the unit tests and added some explicit handling for codepage 866 / Windows 1252 oddness - I tried to get it to find a canonical link to some page that documents the problem it was solving here, but it could only really say that there was a problem and point at the two codepages as evidence. The logic seems reasonable here though. I'm not an expert however on those things. @LIHUA919 @nos1609 would you mind taking a quick look at this. I can see that unit tests are reasonably detailed and seemingly cover the previous concerns, but this is one of those places where the real world tends to have some edge cases that are difficult to anticipate. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // is to only coerce IBM866 to Windows-1252 when (a) the high bytes are exclusively the punctuation | ||
| // values listed below and (b) we spot adjacent ASCII. This targets the real failure case without | ||
| // clobbering legitimate Cyrillic text. If another code page has a similar collision, introduce a | ||
| // dedicated allowlist (like this one) plus unit tests that capture the actual shell output we want | ||
| // to preserve. Windows-1252 byte values for smart punctuation. | ||
| const WINDOWS_1252_PUNCT_BYTES: [u8; 8] = [ | ||
| 0x91, // ‘ (left single quotation mark) | ||
| 0x92, // ’ (right single quotation mark) | ||
| 0x93, // “ (left double quotation mark) | ||
| 0x94, // ” (right double quotation mark) | ||
| 0x95, // • (bullet) | ||
| 0x96, // – (en dash) | ||
| 0x97, // — (em dash) | ||
| 0x99, // ™ (trade mark sign) |
There was a problem hiding this comment.
Add missing CP1252 punct bytes
Right now WINDOWS_1252_PUNCT_BYTES only contains {0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x99}. That still leaves other CP1252-only punctuation (e.g. the ellipsis … at 0x85, single/double low quotes at 0x82/0x84, dagger 0x86, etc.) outside the allowlist. The chardetng issue you are working around happens for any byte in the 0x80–0x9F window once the detector guesses IBM866, so inputs such as b"\x85 test" (VSCode rendering … test) still come back as "Е test" because the heuristic refuses to flip the encoding away from IBM866. That’s the same garbled output we set out to fix. Please extend the allowlist (and add a regression test) so these common characters also coerce to Windows-1252 instead of slipping through as Cyrillic.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
I'm going to push back against this one for now - we could chase all the various characters here. Let's wait for an actual problem to occur to re-review which characters should be in this list.
|
@joshka-oai The only doubt actually from my point of view is the lack of Windows 1251 codepage. The more kind-of "universal" would be to handle the Linux's LatArCyrHeb approch to cover non Latin-charaters output, especially cyrillic ones. The Windows 1252 looks like not covering the ArCyrHeb segments of the caracter maps. Just a small "hello world" example PS if got it right, this covers the case |
|
@nos1609 disregard the main PR comment here - I got codex to reimplement this from the manual detection into a more automated one using https://crates.io/crates/chardetng and https://crates.io/crates/encoding_rs. These should handle detecting 1251 and a large variety of other codepages. I'll add your test here as a new unit test to make sure that scenario is covered correctly though. To test this in your VS Code extension: Rebuild codex locally (cargo build) and edit your settings.json file to point to it. Something like this (adjust the path as appropriate): Then when you restart the extension, it will use this CLI executable. You'll see a little orange icon in the lower right corner of the extension panel to remind you that you're using a custom CLI. Make sure to remove this when you're done testing to ensure that you get up to date versions of the CLI when updating the extention. |
🐛 Problem
Users running commands with non-ASCII characters (like Russian text "пример") in Windows/WSL environments experience garbled text in VSCode's shell preview window, with Unicode replacement characters (�) appearing instead of the actual text.
Issue: #6178
🔧 Root Cause
The issue was in
StreamOutput<Vec<u8>>::from_utf8_lossy()method incodex-rs/core/src/exec.rs, which usedString::from_utf8_lossy()to convert shell output bytes to strings. This function immediately replaces any invalid UTF-8 byte sequences with replacement characters, without attempting to decode using other common encodings.In Windows/WSL environments, shell output often uses encodings like:
🛠️ Solution
Replaced the simple
String::from_utf8_lossy()call with intelligent encoding detection via a newbytes_to_string_smart()function that tries multiple encoding strategies:📁 Changes
New Files
codex-rs/core/src/text_encoding.rs- Smart encoding detection modulecodex-rs/core/tests/suite/text_encoding_fix.rs- Integration testsModified Files
codex-rs/core/src/lib.rs- Added text_encoding modulecodex-rs/core/src/exec.rs- Updated StreamOutput::from_utf8_lossy()codex-rs/core/tests/suite/mod.rs- Registered new test module✅ Testing
String::from_utf8_lossy()approachAll tests pass:
🎯 Impact
🧪 Test Scenarios
The fix has been tested with:
📋 Checklist