{"id":"5eabde0c6f4929d7","repo":"rust-lang/cargo","slug":"everything-was-utf8","errorCode":null,"errorMessage":"everything was utf8","messagePattern":"everything was utf8","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_package/mod.rs","lineNumber":658,"sourceCode":"    Ok(result)\n}\n\nfn check_for_file_and_add(\n    label: &str,\n    file_path: &Path,\n    abs_file_path: PathBuf,\n    pkg: &Package,\n    result: &mut Vec<ArchiveFile>,\n    ws: &Workspace<'_>,\n) -> CargoResult<()> {\n    match abs_file_path.strip_prefix(&pkg.root()) {\n        Ok(rel_file_path) => {\n            if !result.iter().any(|ar| ar.rel_path == rel_file_path) {\n                result.push(ArchiveFile {\n                    rel_path: rel_file_path.to_path_buf(),\n                    rel_str: rel_file_path\n                        .to_str()\n                        .expect(\"everything was utf8\")\n                        .to_string(),\n                    contents: FileContents::OnDisk(abs_file_path),\n                })\n            }\n        }\n        Err(_) => {\n            // The file exists somewhere outside of the package.\n            let file_name = file_path.file_name().unwrap();\n            if result.iter().any(|ar| ar.rel_path == file_name) {\n                ws.gctx().shell().warn(&format!(\n                    \"{} `{}` appears to be a path outside of the package, \\\n                            but there is already a file named `{}` in the root of the package. \\\n                            The archived crate will contain the copy in the root of the package. \\\n                            Update the {} to point to the path relative \\\n                            to the root of the package to remove this warning.\",\n                    label,\n                    file_path.display(),\n                    file_name.to_str().unwrap(),","sourceCodeStart":640,"sourceCodeEnd":676,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_package/mod.rs#L640-L676","documentation":"`check_for_file_and_add` converts a relative path to a `&str` via `rel_file_path.to_str().expect(\"everything was utf8\")`. On platforms with non-UTF-8 filenames (non-UTF-8 locale, exotic Unicode normalization, or intentionally binary-named files), `Path::to_str` returns `None` and the expect panics during `cargo package`.","triggerScenarios":"Running `cargo package` when a tracked source file (or a `[[bin]].path`, `[lib].path`, build-script, include, etc.) contains bytes that are not valid UTF-8. Most common on macOS with NFD-normalized names outside the UTF-8 range, or on Linux with byte-sequences invalid in the active locale.","commonSituations":"Non-UTF-8 file or directory names in the crate source tree; files created by tools that emit raw byte names; packaging a crate checked out on a filesystem that mangles encoding; CI on a host with `LANG=C` handling non-ASCII paths.","solutions":["Rename the offending file/directory to valid UTF-8 — find it via `find . -print0 | tr '\\0' '\\n' | grep -axv '.*'` or `locale` checks.","Set a UTF-8 locale in your shell/CI (`export LC_ALL=C.UTF-8 LANG=C.UTF-8`) and retry packaging.","Remove the non-UTF-8 entry from the package via `.gitignore`/`exclude` in `Cargo.toml` if it shouldn't ship.","If the file must be included, report a cargo enhancement request for lossy-path handling in the archive."],"exampleFix":"// before\nrel_str: rel_file_path.to_str().expect(\"everything was utf8\").to_string(),\n\n// after (graceful error pointing at the bad path)\nrel_str: rel_file_path.to_str()\n    .ok_or_else(|| anyhow::format_err!(\n        \"path `{}` is not valid UTF-8 and cannot be packaged\", rel_file_path.display()))?\n    .to_string(),","handlingStrategy":"validation","validationCode":"// Before packaging, scan for non-UTF-8 paths in the crate tree.\nfor entry in walkdir::WalkDir::new(\".\") {\n    let p = entry?.into_path();\n    if p.to_str().is_none() {\n        return Err(anyhow!(\"non-UTF-8 path cannot be packaged: {}\", p.display()));\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep all source file/dir names valid UTF-8.","Set `LC_ALL=C.UTF-8` in CI before `cargo package`.","Add non-UTF-8 artifacts to `.gitignore` or `Cargo.toml` `exclude`."],"tags":["cargo","panic","utf8","filesystem","cargo-package","i18n","rust"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}