rust-lang/cargo · error · anyhow::Error

`{}` resolved to non-UTF value (`{}`)

Error message

`{}` resolved to non-UTF value (`{}`)

What it means

Returned by `resolve_relative_path` (src/workspace/workspace.rs:2151) when `diff_paths` succeeded but `to_str()` returned `None` — the resolved relative path contains bytes that are not valid UTF-8. On Unix filenames are arbitrary bytes, so a non-UTF8 directory/file name in the path makes it impossible to embed into a TOML manifest or Cargo.toml, and the function errors with the label and the lossy display of the path.

Source

Thrown at src/workspace/workspace.rs:2151

pub fn resolve_relative_path(
    label: &str,
    old_root: &Path,
    new_root: &Path,
    rel_path: &str,
) -> CargoResult<String> {
    let joined_path = normalize_path(&old_root.join(rel_path));
    match diff_paths(joined_path, new_root) {
        None => Err(anyhow!(
            "`{}` was defined in {} but could not be resolved with {}",
            label,
            old_root.display(),
            new_root.display()
        )),
        Some(path) => Ok(path
            .to_str()
            .ok_or_else(|| {
                anyhow!(
                    "`{}` resolved to non-UTF value (`{}`)",
                    label,
                    path.display()
                )
            })?
            .to_owned()),
    }
}

/// Finds the path of the root of the workspace.
pub fn find_workspace_root(
    manifest_path: &Path,
    gctx: &GlobalContext,
) -> CargoResult<Option<PathBuf>> {
    find_workspace_root_with_loader(manifest_path, gctx, |self_path| {
        let source_id = SourceId::for_manifest_path(self_path)?;
        let manifest = read_manifest(self_path, source_id, gctx)?;
        Ok(manifest

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Rename the offending file/directory so every component is valid UTF-8 (`convmv -r -f latin1 -t utf8 .` on Linux).
  2. Move the workspace to a path composed entirely of UTF-8 characters.
  3. Avoid non-ASCII or raw-byte names in directories Cargo must traverse.

Example fix

# rename a non-UTF8 directory
convvm -r --notest -f latin1 -t utf8 ./bad-name
# then re-run cargo
Defensive patterns

Strategy: validation

Validate before calling

fn all_utf8(p: &std::path::Path) -> bool { p.to_str().is_some() }
// assert all_utf8(&old_root) && all_utf8(&new_root) && all_utf8(&joined)

Prevention

When it happens

Trigger: Any path component under `old_root`/`new_root`/`rel_path` containing non-UTF8 bytes (common on Linux where filenames are byte strings), surfaced when `path.to_str()` is `None`.

Common situations: Files created by tooling that uses Latin-1 or raw byte names; mounts with odd encodings; copying a workspace from a system with a different locale; `tar` extraction preserving non-UTF8 entry names.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/07df17af2ee9358f.json. Report an issue: GitHub.