Skip to content

Commit b779cbb

Browse files
authored
Rollup merge of rust-lang#67219 - jsgf:command-argv0-debug, r=joshtriplett
Fix up Command Debug output when arg0 is specified. PR rust-lang#66512 added the ability to set argv[0] on Command. As a side effect, it changed the Debug output to print both the program and argv[0], which in practice results in stuttery output (`"echo" "echo" "foo"`). This PR reverts the behaviour to the the old one, so that the command is only printed once - unless arg0 has been set. In that case it emits `"[command]" "arg0" "arg1" ...`.
2 parents f0eb4b4 + ce56e75 commit b779cbb

File tree

6 files changed

+30
-2
lines changed

6 files changed

+30
-2
lines changed

src/libstd/sys/unix/process/process_common.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,12 @@ impl ChildStdio {
375375

376376
impl fmt::Debug for Command {
377377
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
378-
write!(f, "{:?}", self.program)?;
379-
for arg in &self.args {
378+
if self.program != self.args[0] {
379+
write!(f, "[{:?}] ", self.program)?;
380+
}
381+
write!(f, "{:?}", self.args[0])?;
382+
383+
for arg in &self.args[1..] {
380384
write!(f, " {:?}", arg)?;
381385
}
382386
Ok(())
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// run-pass
2+
3+
// ignore-windows - this is a unix-specific test
4+
// ignore-cloudabi no processes
5+
// ignore-emscripten no processes
6+
// ignore-sgx no processes
7+
#![feature(process_set_argv0)]
8+
9+
use std::os::unix::process::CommandExt;
10+
use std::process::Command;
11+
12+
fn main() {
13+
let mut command = Command::new("some-boring-name");
14+
15+
assert_eq!(format!("{:?}", command), r#""some-boring-name""#);
16+
17+
command.args(&["1", "2", "3"]);
18+
19+
assert_eq!(format!("{:?}", command), r#""some-boring-name" "1" "2" "3""#);
20+
21+
command.arg0("exciting-name");
22+
23+
assert_eq!(format!("{:?}", command), r#"["some-boring-name"] "exciting-name" "1" "2" "3""#);
24+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)