{"id":"05a14b8be4489cc3","repo":"rust-lang/cargo","slug":"argument-for-argfile-contains-newlines-arg","errorCode":null,"errorMessage":"argument for argfile contains newlines: `{arg}`","messagePattern":"argument for argfile contains newlines: `(.+?)`","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"crates/cargo-util/src/process_builder.rs","lineNumber":481,"sourceCode":"        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    }\n\n    /// Builds a command from `ProcessBuilder` for everything but not `args`.\n    fn build_command_without_args(&self) -> Command {\n        let mut command = {\n            let mut iter = self.wrappers.iter().rev().chain(once(&self.program));\n            let mut cmd = Command::new(iter.next().expect(\"at least one `program` exists\"));\n            cmd.args(iter);\n            cmd\n        };","sourceCodeStart":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/crates/cargo-util/src/process_builder.rs#L463-L499","documentation":"Returned as an `io::Error` from the same argfile builder (crates/cargo-util/src/process_builder.rs:480) when an argument passes the UTF-8 check but contains a newline (`\\n`). The argfile puts one argument per line, so an embedded newline would silently split one argument into two and corrupt the spawned command, so cargo rejects it up front.","triggerScenarios":"An argument to an external process (rustc, linker, etc.) contains a literal `\\n`. The check `if arg.contains('\\n')` at line 480 fires when building the argfile fallback for long command lines.","commonSituations":"A feature flag, env var, or RUSTFLAGS value containing a stray newline (copy-paste from a multiline config); a path with a newline (rare); a script-generated `.cargo/config.toml` embedding CR/LF; build tools injecting `\\n` into flags.","solutions":["Strip newline characters from the offending argument (most often an env var like `RUSTFLAGS` or a config value).","Re-enter the value in `.cargo/config.toml` on a single line, avoiding pasted multiline content.","Sanitize any programmatically generated flags before they reach cargo."],"exampleFix":"# before — pasted multiline value\n[build]\nrustflags = [\"--cfg foo\nbar\"]\n# after — single line\n[build]\nrustflags = [\"--cfg foo\"]","handlingStrategy":"validation","validationCode":"fn args_have_no_newlines(args: &[String]) -> bool { args.iter().all(|a| !a.contains('\\n')) }\n// assert args_have_no_newlines(&flags) before invoking cargo with large arg sets","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Strip newlines from env vars and config values (RUSTFLAGS, etc.).","Paste config values as single lines.","Sanitize generated flags before passing to cargo."],"tags":["cargo","process","argfile","newline","cli"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}