Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/benchmark/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ use super::timing_result::TimingResult;
use anyhow::{bail, Context, Result};
use statistical::mean;

#[cfg(windows)]
fn normalize_relative_command_path_for_cmd(command_line: &str) -> String {
let executable_end = command_line
.find(char::is_whitespace)
.unwrap_or(command_line.len());
format!(
"{}{}",
command_line[..executable_end].replace('/', "\\"),
&command_line[executable_end..]
)
}

#[cfg(all(test, windows))]
mod tests {
use super::normalize_relative_command_path_for_cmd;

#[test]
fn normalizes_only_the_relative_executable_path() {
assert_eq!(
normalize_relative_command_path_for_cmd("../target/debug/tool.exe --output value/a"),
"..\\target\\debug\\tool.exe --output value/a"
);
}
}

pub enum BenchmarkIteration {
NonBenchmarkRun,
Warmup(u64),
Expand Down Expand Up @@ -193,13 +218,21 @@ impl Executor for ShellExecutor<'_> {
let on_windows_cmd = cfg!(windows) && *self.shell == Shell::Default("cmd.exe");
let mut command_builder = self.shell.command();
command_builder.arg(if on_windows_cmd { "/C" } else { "-c" });
let command_line = command.get_command_line();

// Windows needs special treatment for its behavior on parsing cmd arguments
if on_windows_cmd {
#[cfg(windows)]
command_builder.raw_arg(command.get_command_line());
if command_line.starts_with("./") || command_line.starts_with("../") {
// `cmd.exe /C` treats the leading `.` as a command when passed
// verbatim. Normalize only the executable path and let `Command`
// quote the complete invocation.
command_builder.arg(normalize_relative_command_path_for_cmd(&command_line));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve raw cmd quoting for relative commands

On Windows, this branch switches cmd.exe /C from raw_arg to arg whenever the benchmark starts with ./ or ../. For a command such as ./tool.exe "input file.txt", Rust applies C-runtime escaping to the inner quotes (for example \"...\"), but cmd.exe /c has different escaping rules—the Rust raw_arg docs call out this exact case: https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html#tymethod.raw_arg. The result is that quoted arguments after a relative executable can be split or delivered with literal quotes/backslashes; normalize the executable path but keep appending the resulting command string with raw_arg so existing quoted-command behavior is preserved.

Useful? React with 👍 / 👎.

} else {
command_builder.raw_arg(&command_line);
}
} else {
command_builder.arg(command.get_command_line());
command_builder.arg(&command_line);
}

let mut result = run_command_and_measure_common(
Expand Down
15 changes: 15 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,21 @@ fn speed_comparison_sort_order() {
));
}

#[cfg(windows)]
#[test]
fn windows_runs_executable_paths_with_forward_slashes() {
let hyperfine_exe = assert_cmd::cargo::cargo_bin!("hyperfine");
let working_directory = std::env::current_dir().unwrap();
let relative_path = hyperfine_exe.strip_prefix(working_directory).unwrap();
let command_path = std::path::Path::new("..").join(relative_path);
let command = format!(
"{} --version",
command_path.to_string_lossy().replace('\\', "/")
);

hyperfine().arg("--runs=1").arg(command).assert().success();
}

#[cfg(windows)]
#[test]
fn windows_quote_args() {
Expand Down