Hmbown/CodeWhale · error · anyhow::Error

{} is not valid UTF-8

Error message

{} is not valid UTF-8

What it means

After the secure-open and link checks pass, Codewhale decodes the workspace .env bytes with std::str::from_utf8. A single invalid byte anywhere makes the entire file fail with '{path} is not valid UTF-8'. dotenvy needs text input, so this gate runs before any parsing or credential extraction; no partial load happens.

Source

Thrown at crates/tui/src/lib.rs:2482

            Err(error) if error.kind() == io::ErrorKind::NotFound => {}
            Err(error) => {
                return Err(anyhow!(
                    "could not inspect {}: {error}",
                    candidate.display()
                ));
            }
        }
        if ancestor == boundary {
            break;
        }
    }
    Ok(None)
}

fn load_workspace_dotenv_credentials_from_path(path: &Path) -> Result<WorkspaceDotenvReport> {
    let contents = read_stable_workspace_dotenv(path)?;
    let text = std::str::from_utf8(&contents)
        .map_err(|_| anyhow!("{} is not valid UTF-8", path.display()))?;
    if dotenv_has_variable_expansion(text) {
        bail!(
            "{} uses variable expansion; workspace .env values must be literal to prevent ambient-secret substitution",
            path.display()
        );
    }

    let mut report = WorkspaceDotenvReport {
        path: path.to_path_buf(),
        ..WorkspaceDotenvReport::default()
    };
    let entries = dotenvy::from_read_iter(std::io::Cursor::new(contents))
        .collect::<std::result::Result<Vec<_>, _>>()
        .map_err(|_| anyhow!("{} could not be parsed safely", path.display()))?;
    for entry in entries {
        let (key, value) = entry;
        if !is_workspace_dotenv_credential_key(&key) {
            report.ignored.insert(key);

View on GitHub (pinned to 8880682c63)

Solutions

  1. Re-save the file as UTF-8 without BOM (vim :set fenc=utf8; PowerShell Set-Content -Encoding utf8NoBOM)
  2. Run file .env — the output should say 'UTF-8 Unicode text' or 'ASCII text'
  3. Replace non-ASCII bytes inside tokens or values with ASCII-safe equivalents
  4. If the file came from another tool, regenerate or convert it: iconv -f UTF-16 -t UTF-8 .env

Example fix

# before (Windows PowerShell 5 wrote UTF-16)
Set-Content .env "OPENAI_API_KEY=sk-..."

# after
Set-Content -Encoding utf8NoBOM .env "OPENAI_API_KEY=sk-..."
Defensive patterns

Strategy: validation

Validate before calling

let bytes = std::fs::read(".env")?;
if std::str::from_utf8(&bytes).is_err() {
    eprintln!(".env is not UTF-8; re-save it as UTF-8 without BOM before running codewhale");
}

Prevention

When it happens

Trigger: A .env saved as UTF-16 (typical after Windows PowerShell 5 Out-File/Set-Content defaults), Latin-1 or CP1252 with non-ASCII bytes, a binary blob pasted into the file, or encrypted secrets written as raw bytes.

Common situations: Windows PowerShell 5 wrote the file as UTF-16; an editor saved with a legacy codepage; a password manager exported secrets in a non-UTF-8 encoding.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@8880682c63 (2026-08-16). Data as JSON: /api/errors/e3f38fe4c01ad0cd. Report an issue: GitHub.