{"record":{"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":513,"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":495,"sourceCodeEnd":531,"githubUrl":"https://github.com/rust-lang/cargo/blob/42eee92bc9aa4449f4e25d84e4ceb1cf8e0f87f9/crates/cargo-util/src/process_builder.rs#L495-L531","documentation":"In the same argfile-building loop, after confirming each argument is valid UTF-8, Cargo checks `arg.contains('\\n')`. Because the argfile format writes exactly one argument per line (`writeln!(buf, \"{arg}\")`), an embedded newline would split a single argument across lines and corrupt parsing. Any argument containing a newline triggers this `io::Error` (kind `Other`).","triggerScenarios":"A `ProcessBuilder` argument containing a `\\n` byte, materialized into an argfile. For example, a multiline string value, a JSON/document argument with newlines, or a path with an embedded newline (possible on some filesystems).","commonSituations":"Passing a multiline configuration snippet, script body, or structured payload as a single command-line argument; tooling that embeds raw file contents into an argument; unusual filenames containing newlines.","solutions":["Strip or replace newlines in the argument before passing it to the `ProcessBuilder`.","Write multiline content to a separate file and pass the file path as the argument instead.","Encode the argument (e.g. base64 or JSON on a single line) so it contains no newlines."],"exampleFix":"// before\nlet cfg = \"key1=val1\\nkey2=val2\";\npb.arg(cfg);\n\n// after\nlet cfg = \"key1=val1\\nkey2=val2\".replace('\\n', \" \");\npb.arg(cfg);","handlingStrategy":"validation","validationCode":"fn args_no_newlines(args: &[String]) -> Result<(), String> {\n    for a in args {\n        if a.contains('\\n') { return Err(format!(\"arg contains newline: {a:?}\")); }\n    }\n    Ok(())\n}\n// call before populating a ProcessBuilder that may use argfile mode","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Strip newlines from any argument before adding it to a ProcessBuilder.","Pass multiline content via a file path, not inline.","Encode binary/multiline data (base64/JSON single-line) before passing as an arg."],"tags":["process","argfile","newline","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"}