rust-lang/cargo · error
` ` resolved to non-UTF value (` `)
Error message
`{}` resolved to non-UTF value (`{}`) What it means
In the same `resolve_relative_path` function, when `diff_paths` succeeds the resulting `PathBuf` is converted to a string via `to_str()`. If the path contains non-UTF-8 bytes (valid on Unix for arbitrary OS paths), `to_str()` returns `None` and the function errors, interpolating the label and the lossy display of the path. This guards downstream string handling from non-UTF-8 paths.
Solutions
- Rename the offending path component(s) to valid UTF-8 so `to_str()` succeeds.
- Set the `LANG`/`LC_ALL` locale appropriately and ensure the filesystem path is UTF-8 encoded.
- Move the workspace to a directory tree whose every component is valid UTF-8.
Example fix
# before: a non-UTF-8 directory name (e.g. raw bytes) mv $'bad\xffdir' gooddir # then re-run cargo — resolve_relative_path will succeed
Defensive patterns
Strategy: validation
Validate before calling
use std::path::Path;
fn all_components_utf8(p: &Path) -> bool {
p.components().all(|c| c.as_os_str().is_char_boundary(0) && c.as_os_str().to_str().is_some())
}
// verify every workspace path component is UTF-8 before path relativization
assert!(all_components_utf8(Path::new("/Users/me/ws"))); Prevention
- Restrict workspace paths to valid UTF-8 components.
- Avoid copying directories with legacy/Latin-1 names into workspace trees.
- Run a pre-build lint that fails on non-UTF-8 path components.
When it happens
Trigger: Calling `resolve_relative_path` where the relativized result contains invalid UTF-8 bytes — typically on Unix when a workspace path component uses non-UTF-8 bytes (e.g. a directory named with Latin-1 or raw bytes), and `diff_paths` produces a relative path preserving those bytes.
Common situations: Workspaces created by tooling that preserves byte-accurate names, files copied from systems with different encodings, or deliberately non-UTF-8 paths used in test fixtures. The error surfaces the moment Cargo tries to treat the path as a string for lockfile/manifest relativization.
Related errors
- non UTF8 path
- can only edit absolute paths, got
- Character at line is invalid. Cargo only supports UTF-8.
- {}: {:?}
- no executable for ` ` found in PATH
AI-assisted analysis of rust-lang/cargo@98a09e7e7d (2026-08-11).
Data as JSON: /api/errors/07df17af2ee9358f.
Report an issue: GitHub.
Appendix: source
Thrown at src/workspace/workspace.rs:2096
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(manifestView on GitHub (pinned to 98a09e7e7d)