Error when read non-UTF8 sourcecode file
Checks
- [x] I have searched github.com/aws/amazon-q-developer-cli/issues and there are no duplicates of my issue
- [x] I have run
q doctorin the affected terminal session - [x] I have run
q restartand replicated the issue again
Operating system
macOS 15.3.2 (24D81)
Expected behaviour
Q Developer can read or retry non-UTF8 sourcecode without errors.
Actual behaviour
Q Developer occured the error when read non-UTF8 sourcecode.
Read from filesystem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Reading directory: /Users/kensuke/Desktop/myproject/source code with maximum depth of 0
Reading from filesystem...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Reading:
🟢 Completed in 0.2s
次に、主要なソースファイルの内容を確認して、プロジェクトの構造を理解しましょう。まずはメインファイルと思われるsource.cppを見てみます。
Read from filesystem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Amazon Q is having trouble responding right now:
0: failed to print tool: stream did not contain valid UTF-8
Location:
crates/q_cli/src/cli/chat/mod.rs:476
Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
Steps to reproduce
- move project directory included non-UTF8 sourcecode.(Shift-JIS)
- ask "This directory contains the server program. Please read this source code first. Then, generate a context file so that Amazon Q Developer can execute the program properly." to q chat
- Q Developer occured the error when read non-UTF8 sourcecode.
Environment
[q-details]
version = "1.7.2"
hash = "b6bf5f1675dd7395009e8bc65c1145820671f211"
date = "2025-03-21T01:36:16.66764Z (16d ago)"
variant = "full"
[system-info]
os = "macOS 15.3.2 (24D81)"
chip = "Apple M2"
total-cores = 8
memory = "8.00 GB"
[environment]
cwd = "/Users/USER/Desktop/myproject/source code"
cli-path = "/Users/USER/Desktop/myproject/source code"
os = "Mac"
shell-path = "/bin/zsh"
shell-version = "5.9"
terminal = "macOS"
install-method = "unknown"
[env-vars]
PATH = "/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Users/USER/.local/bin"
QTERM_SESSION_ID = "b4d44d9a3a4e4e248117563bd71e799c"
Q_SET_PARENT_CHECK = "1"
Q_TERM = "1.7.2"
SHELL = "/bin/zsh"
TERM = "xterm-256color"
__CFBundleIdentifier = "com.apple.Terminal"
same error
same error in v1.7.3
Proposed Fix for Non-UTF8 File Reading Issue (by Amazon Q Developer CLI)
Problem
Amazon Q Developer CLI currently fails when attempting to read non-UTF8 encoded files (such as Shift-JIS), producing the error: "stream did not contain valid UTF-8".
Root Cause
The issue occurs in crates/q_cli/src/cli/chat/tools/fs_read.rs where the code uses read_to_string() method which only works with UTF-8 encoded files. When encountering files with other encodings like Shift-JIS, the method fails.
https://github.com/aws/amazon-q-developer-cli/blob/43f666f462ebb54d04131bff5ef197c54293a75b/crates/q_cli/src/cli/chat/tools/fs_read.rs#L134
Proposed Solution
Implement character encoding detection and conversion by:
-
Adding Required Dependencies: • chardetng: For character encoding detection • encoding_rs: For converting detected encodings to UTF-8
-
Modifying the File Reading Process: • First read the file as binary data (byte array) • Use chardetng to detect the file's encoding • Convert the content to UTF-8 using the detected encoding via encoding_rs • Implement a fallback mechanism for cases where encoding detection fails
-
Implementation Example:
// Read file as binary
let bytes = ctx.fs().read(&path).await?;
// Detect encoding
let encoding = chardetng::detect(&bytes);
// Convert to UTF-8 using detected encoding
let (content, _, had_errors) = encoding.decode(&bytes);
// Log if there were conversion issues
if had_errors {
debug!("Encountered errors when decoding file with detected encoding: {}", encoding.name());
}
// Use the converted string
let file = content.into_owned();
- Fallback Mechanism:
• If encoding detection or conversion fails, use
String::from_utf8_lossy()to force conversion to UTF-8 • This ensures at least some readable content by replacing invalid UTF-8 sequences with the replacement character (�)
This change will allow Amazon Q Developer CLI to properly handle files with various encodings, including Shift-JIS and other non-UTF8 encodings, making the tool more robust for international users.
I am having the same issue. Can this be addressed rapidly?
This appears to have been fixed in PR #264 (merged June 17, 2025), which replaced read_to_string with String::from_utf8_lossy(&file_bytes) across all file reading paths in fs_read.rs. The current upstream/main uses from_utf8_lossy consistently, so non-UTF8 files (including Shift-JIS) should now be read with replacement characters instead of failing.
Could someone with a non-UTF8 file confirm whether this is still reproducible in a recent version? If not, this issue can be closed.