{"record":{"id":"307b2c7fb5db7da2","repo":"rust-lang/cargo","slug":"path-at-was-not-valid-utf-8","errorCode":null,"errorMessage":"path at `{}` was not valid utf-8","messagePattern":"path at `(.+?)` was not valid utf-8","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/cargo-util/src/paths.rs","lineNumber":172,"sourceCode":"        .with_context(|| format!(\"failed to load metadata for path `{}`\", path.display()))\n}\n\n/// Returns metadata for a file without following symlinks.\n///\n/// Equivalent to [`std::fs::metadata`] with better error messages.\npub fn symlink_metadata<P: AsRef<Path>>(path: P) -> Result<Metadata> {\n    let path = path.as_ref();\n    std::fs::symlink_metadata(path)\n        .with_context(|| format!(\"failed to load metadata for path `{}`\", path.display()))\n}\n\n/// Reads a file to a string.\n///\n/// Equivalent to [`std::fs::read_to_string`] with better error messages.\npub fn read(path: &Path) -> Result<String> {\n    match String::from_utf8(read_bytes(path)?) {\n        Ok(s) => Ok(s),\n        Err(_) => anyhow::bail!(\"path at `{}` was not valid utf-8\", path.display()),\n    }\n}\n\n/// Reads a file into a bytes vector.\n///\n/// Equivalent to [`std::fs::read`] with better error messages.\npub fn read_bytes(path: &Path) -> Result<Vec<u8>> {\n    fs::read(path).with_context(|| format!(\"failed to read `{}`\", path.display()))\n}\n\n/// Writes a file to disk.\n///\n/// Equivalent to [`std::fs::write`] with better error messages.\npub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> {\n    let path = path.as_ref();\n    fs::write(path, contents.as_ref())\n        .with_context(|| format!(\"failed to write `{}`\", path.display()))\n}","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/rust-lang/cargo/blob/42eee92bc9aa4449f4e25d84e4ceb1cf8e0f87f9/crates/cargo-util/src/paths.rs#L154-L190","documentation":"paths::read reads a file as bytes then attempts UTF-8 conversion. If the byte content is not valid UTF-8, it bails with 'path at ... was not valid utf-8'. This is a stricter wrapper over std::fs::read_to_string that yields a clearer, path-annotated error.","triggerScenarios":"Calling paths::read on a file whose bytes are not valid UTF-8: binary files, files in Latin-1/GBK/UTF-16/Shift-JIS encodings, or files with stray invalid byte sequences.","commonSituations":"Reading a config/lock/manifest that was saved in a non-UTF-8 encoding (notably UTF-16 BOM on Windows editors); accidentally reading a binary artifact; corrupted file with truncated multi-byte sequences.","solutions":["Re-save the file as UTF-8 (remove BOM for UTF-16; convert encoding).","If binary is expected, use paths::read_bytes instead of paths::read.","Validate encoding before reading (e.g. check for a BOM or run a UTF-8 validity check).","Fix the upstream producer to emit UTF-8."],"exampleFix":"// before\nlet s = paths::read(&path)?;  // panics-style bail on non-utf8\n// after\nlet bytes = paths::read_bytes(&path)?;\nlet s = String::from_utf8_lossy(&bytes).into_owned();","handlingStrategy":"validation","validationCode":"// Check UTF-8 validity before calling paths::read\nlet bytes = std::fs::read(&path)?;\nif std::str::from_utf8(&bytes).is_err() {\n    eprintln!(\"{} is not valid UTF-8\", path.display());\n}\n// or just use paths::read_bytes when encoding is unknown","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Use paths::read_bytes for non-text files.","Ensure editors/tools save config as UTF-8 without BOM."],"tags":["cargo","io","utf-8","encoding","file"],"backgroundTag":null,"analyzedSha":"42eee92bc9aa4449f4e25d84e4ceb1cf8e0f87f9","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}