{"record":{"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":null,"httpStatus":null,"severity":"error","filePath":"crates/cargo-util/src/process_builder.rs","lineNumber":504,"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":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/rust-lang/cargo/blob/42eee92bc9aa4449f4e25d84e4ceb1cf8e0f87f9/crates/cargo-util/src/process_builder.rs#L486-L522","documentation":"In `ProcessBuilder::get_command_and_argfile` (cargo-util), Cargo serializes its arguments into a temporary argfile (one per line) when the argument list is large. Each `OsString` argument is converted to `&str` via `to_str()`; if any argument contains non-UTF-8 bytes the conversion fails and an `io::Error` (kind `Other`) is returned with the lossy rendering of the argument. The argfile format is line-delimited UTF-8 text, so non-UTF-8 arguments cannot be represented.","triggerScenarios":"Building a `ProcessBuilder` whose `args` contain an `OsString` with non-UTF-8 bytes, then calling the method that materializes the argfile (used when command-line length limits force an argfile). Common with non-UTF-8 environment-derived paths or user-supplied arguments on Unix.","commonSituations":"Passing a file path argument that contains non-UTF-8 bytes (rare, but possible on Unix), or a crate/tool name generated from a non-UTF-8 source. Surfaces only when the argument count/size triggers argfile mode.","solutions":["Ensure every argument passed to the `ProcessBuilder` is valid UTF-8 before triggering argfile creation.","Sanitize or percent-encode any path argument that may carry non-UTF-8 bytes.","Avoid deriving `ProcessBuilder` arguments from raw OS strings of unknown encoding."],"exampleFix":"// before\nlet arg: OsString = ...; // may contain non-UTF-8 bytes\npb.arg(arg);\n\n// after: validate / sanitize first\nlet arg = arg.into_string().map_err(|_| \"non-utf8 arg\")?;\npb.arg(arg);","handlingStrategy":"validation","validationCode":"use std::ffi::OsString;\nfn args_all_utf8(args: &[OsString]) -> Result<(), String> {\n    for a in args {\n        a.to_str().ok_or_else(|| format!(\"non-utf8 arg: {:?}\", a))?;\n    }\n    Ok(())\n}\n// call before constructing a ProcessBuilder that may trigger argfile mode","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never feed raw non-UTF-8 OsStrings into ProcessBuilder args.","Sanitize environment-derived paths before passing them as arguments.","Unit-test subprocess invocations with UTF-8 assertions on every arg."],"tags":["process","argfile","utf8","cargo-util","subprocess"],"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"}