{"id":"9f21dcbd698feed5","repo":"rust-lang/cargo","slug":"argument-for-argfile-contains-invalid-utf-8-charac","errorCode":null,"errorMessage":"argument for argfile contains invalid UTF-8 characters: `{}`","messagePattern":"argument for argfile contains invalid UTF-8 characters: `(.+?)`","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"crates/cargo-util/src/process_builder.rs","lineNumber":472,"sourceCode":"    /// arguments. This is primarily served for rustc/rustdoc command family.\n    fn build_command_with_argfile(&self) -> io::Result<(Command, NamedTempFile)> {\n        use std::io::Write as _;\n\n        let mut tmp = tempfile::Builder::new()\n            .prefix(\"cargo-argfile.\")\n            .tempfile()?;\n\n        let mut arg = OsString::from(\"@\");\n        arg.push(tmp.path());\n        let mut cmd = self.build_command_without_args();\n        cmd.arg(arg);\n        tracing::debug!(\"created argfile at {} for {self}\", tmp.path().display());\n\n        let cap = self.get_args().map(|arg| arg.len() + 1).sum::<usize>();\n        let mut buf = Vec::with_capacity(cap);\n        for arg in &self.args {\n            let arg = arg.to_str().ok_or_else(|| {\n                io::Error::new(\n                    io::ErrorKind::Other,\n                    format!(\n                        \"argument for argfile contains invalid UTF-8 characters: `{}`\",\n                        arg.to_string_lossy()\n                    ),\n                )\n            })?;\n            if arg.contains('\\n') {\n                return Err(io::Error::new(\n                    io::ErrorKind::Other,\n                    format!(\"argument for argfile contains newlines: `{arg}`\"),\n                ));\n            }\n            writeln!(buf, \"{arg}\")?;\n        }\n        tmp.write_all(&mut buf)?;\n        Ok((cmd, tmp))\n    }","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/crates/cargo-util/src/process_builder.rs#L454-L490","documentation":"Returned as an `io::Error` from `ProcessBuilder::build_command` argfile creation (crates/cargo-util/src/process_builder.rs:471) when an argument's `OsString` cannot be converted to a `str` via `to_str()` — i.e. it contains non-UTF8 bytes. The argfile format requires writing each argument as a line of text, so non-UTF8 args cannot be serialized and the whole argfile build fails.","triggerScenarios":"Cargo invokes an external command with so many/long arguments that it exceeds the OS arg-length limit and falls back to an `@argfile`; one of those arguments is a path or value containing non-UTF8 bytes (Unix). `arg.to_str()` returns `None` and the error is raised.","commonSituations":"Building on Linux where a source path or env-derived value contains non-UTF8 bytes; very large dependency graphs pushing rustc invocation over ARG_MAX; paths from unzipped archives with mojibake names; locale mismatches.","solutions":["Ensure all paths and arguments passed to the build are valid UTF-8 (rename offending files).","Reduce argument count/length so cargo does not need an argfile (fewer features, smaller crate, split workspace).","Set relevant env vars (e.g. locale) so tools produce UTF-8 paths."],"exampleFix":"# find and rename non-UTF8 paths cargo is trying to pass\nfind . -print0 | perl -0 -ne 'print if !/^[\\x00-\\x7F]*$/'\n# rename them to ASCII names, then re-run cargo build","handlingStrategy":"validation","validationCode":"fn args_are_utf8(args: &[std::ffi::OsString]) -> bool {\n    args.iter().all(|a| a.to_str().is_some())\n}\n// before relying on argfile fallback, validate args_are_utf8(&args)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure all source paths and flag values are UTF-8.","Reduce argument volume to avoid triggering the argfile path.","Sanitize programmatically generated flags."],"tags":["cargo","process","argfile","utf8","cli"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}