Skip to content

Commit 7f0bf05

Browse files
committed
Avoid use of undefined std::process::Command behavior
std::process::Command is known to be unpredictable when a relative path is used together with `current_dir`, and outright fails on macOS Catalina [0]. Absolutize paths before passing them to std::process::Command to stay within the documented constraints of the API. [0]: rust-lang/rust#37868
1 parent 1bbe3f1 commit 7f0bf05

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

rdkafka-sys/build.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,21 @@ macro_rules! println_stderr(
1515
} }
1616
);
1717

18-
fn run_command_or_fail(dir: &str, cmd: &str, args: &[&str]) {
19-
println_stderr!("Running command: \"{} {}\" in dir: {}", cmd, args.join(" "), dir);
18+
fn run_command_or_fail<P>(dir: &str, cmd: P, args: &[&str])
19+
where
20+
P: AsRef<Path>,
21+
{
22+
let cmd = cmd.as_ref();
23+
let cmd = if cmd.components().count() > 1 && cmd.is_relative() {
24+
// If `cmd` is a relative path (and not a bare command that should be
25+
// looked up in PATH), absolutize it relative to `dir`, as otherwise the
26+
// behavior of std::process::Command is undefined.
27+
// https://github.com/rust-lang/rust/issues/37868
28+
PathBuf::from(dir).join(cmd).canonicalize().expect("canonicalization failed")
29+
} else {
30+
PathBuf::from(cmd)
31+
};
32+
println_stderr!("Running command: \"{} {}\" in dir: {}", cmd.display(), args.join(" "), dir);
2033
let ret = Command::new(cmd).current_dir(dir).args(args).status();
2134
match ret.map(|status| (status.success(), status.code())) {
2235
Ok((true, _)) => { return },

0 commit comments

Comments
 (0)