Hmbown/CodeWhale · error

is not valid UTF-8

Error message

{} is not valid UTF-8

What it means

Workspace `.env` credentials are parsed as UTF-8 text. If the file's bytes are not valid UTF-8 the loader refuses it with this error instead of lossily decoding, because credential values must round-trip exactly.

Solutions

  1. Re-save the `.env` as UTF-8 (without BOM is safest).
  2. Check for binary content: `file .env` should report text.
  3. Recreate the file with plain ASCII keys and values.

Example fix

// before (shell)
iconv -f UTF-16 -t UTF-8 .env.utf16 > .env  # wrong encoding source
// after
iconv -f UTF-16LE -t UTF-8 .env.utf16 > .env
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");
}

Type guard

fn is_valid_utf8_file(path: &Path) -> bool {
    std::fs::read(path).map(|b| std::str::from_utf8(&b).is_ok()).unwrap_or(false)
}

Try / catch

match load_workspace_dotenv_credentials_from_path(&path) {
    Err(e) if e.to_string().contains("not valid UTF-8") => eprintln!("re-encode .env as UTF-8: {e}"),
    other => other?,
}

Prevention

When it happens

Trigger: `load_workspace_dotenv_credentials_from_path` reads a `.env` whose bytes fail `std::str::from_utf8` — e.g. a binary file, or one saved in UTF-16/Latin-1.

Common situations: A `.env` edited and saved by a Windows tool as UTF-16, or a placeholder/binary file mistakenly named `.env`.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/e3f38fe4c01ad0cd. Report an issue: GitHub.

Appendix: source

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

            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 73e0f67d83)