Hmbown/CodeWhale · error

could not be parsed safely

Error message

{} could not be parsed safely

What it means

After UTF-8 validation, the `.env` is parsed with `dotenvy::from_read_iter`. Any parse error from dotenvy is collapsed into this single error so the library never partially applies credentials from a file it could not fully and safely parse.

Solutions

  1. Validate the file line by line; look for unbalanced quotes or malformed `KEY=value` entries.
  2. Temporarily run `dotenvy` against the file (or a linter) to get the precise line number dotenvy reports.
  3. Remove or quote special characters (`$`, backticks, newlines) that break the parser.

Example fix

// before
API_KEY="unterminated value
// after
API_KEY="escaped value"
Defensive patterns

Strategy: validation

Validate before calling

dotenvy::from_path_iter(".env").map(|i| i.for_each(|_| ()))
    .expect(".env must parse cleanly before Codewhale loads it");

Try / catch

match load_workspace_dotenv_credentials_from_path(&path) {
    Err(e) if e.to_string().contains("could not be parsed safely") => eprintln!("fix .env syntax: {e}"),
    other => other?,
}

Prevention

When it happens

Trigger: `dotenvy::from_read_iter` returns an `Err` — malformed lines, unterminated quotes, invalid escape sequences, or unsafe variable expansion constructs in the `.env`.

Common situations: Hand-edited `.env` with a stray quote, multi-line value without proper quoting, or copy-pasted shell syntax dotenvy cannot parse.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


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

Appendix: source

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

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);
            continue;
        }
        if std::env::var_os(&key).is_some() {
            continue;
        }

        // SAFETY: this loader runs synchronously in `main` before the runtime
        // owner or Tokio workers are spawned. No concurrent environment reader
        // exists inside Codewhale, and later startup code treats this process
        // environment as immutable.
        unsafe { std::env::set_var(&key, value) };
        report.loaded.insert(key);
    }
    Ok(report)

View on GitHub (pinned to 73e0f67d83)