From d72fc38e62092a2c75e040e1fae2b106a15edd01 Mon Sep 17 00:00:00 2001 From: korrat Date: Sat, 24 Jun 2023 16:10:00 +0200 Subject: [PATCH] Use raw_arg for CMD on Windows (#616) --- crates/lib/src/command.rs | 18 +++++++++++++----- crates/lib/src/command/tests.rs | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/crates/lib/src/command.rs b/crates/lib/src/command.rs index 3573cec..df423b9 100644 --- a/crates/lib/src/command.rs +++ b/crates/lib/src/command.rs @@ -99,9 +99,21 @@ impl Command { args, command, } => { + if command.is_empty() { + return Err(RuntimeError::CommandShellEmptyCommand); + } + let (shcmd, shcliopt) = match shell { #[cfg(windows)] - Shell::Cmd => ("cmd.exe", "/C"), + Shell::Cmd => { + use std::os::windows::process::CommandExt as _; + use std::process::Command as StdCommand; + + // TODO this is a workaround until TokioCommand has a raw_arg method. See tokio-rs/tokio#5810. + let mut std_command = StdCommand::new("cmd.exe"); + std_command.args(args).arg("/C").raw_arg(command); + return Ok(TokioCommand::from(std_command)); + } #[cfg(windows)] Shell::Powershell => ("powershell.exe", "-Command"), @@ -117,10 +129,6 @@ impl Command { } }; - if command.is_empty() { - return Err(RuntimeError::CommandShellEmptyCommand); - } - let mut c = TokioCommand::new(shcmd); c.args(args); c.arg(shcliopt).arg(command); diff --git a/crates/lib/src/command/tests.rs b/crates/lib/src/command/tests.rs index f8ab72a..a324d52 100644 --- a/crates/lib/src/command/tests.rs +++ b/crates/lib/src/command/tests.rs @@ -85,7 +85,7 @@ async fn windows_shell_cmd() -> Result<(), std::io::Error> { assert!(Command::Shell { shell: Shell::Cmd, args: Vec::new(), - command: "echo hi".into() + command: r#""echo" hi"#.into() } .to_spawnable() .unwrap()