Skip to content

Issue with std::process::Command on macOS. #80819

Closed
@mmulet

Description

@mmulet

I tried this code:

Directory structure:

.
+-- main.rs
+-- main
+-- sub_directory
|     +-- hello_world

main.rs:

use std::process::Command;
use std::path::Path;
fn main(){
    Command::new("./hello_world")
    .current_dir(Path::new("sub_directory"))
    .status()
    .expect("Failure to run");
}

I expected to see this happen:
Command should run the hello_world bash script located in ./sub_directory/hello_world. The exit code should be 0.

Instead, this happened:
Command does indeed run the hello_world bash script located in ./sub_directory/hello_world, but the exit code is 2 (Not found), and the main thread panics.
I noticed this issue only on macOS. I'm on macOS Big Sur 11.1 (20C69) on a Mac Pro (Late 2013). Works as expected in Linux.

I can create a workaround by creating an empty file named hello_world as a sibling to main.
Workaound directory structure:

.
+-- main.rs
+-- main
+-- sub_directory
|     +-- hello_world
+-- hello_world

Which leads me to believe that Command is trying to resolve a relative directory without taking into account the `.current_dir("...")" input and returning an incorrect exit status.

Meta

Checked Stable and nightly:

Stable:

rustc --version --verbose:

rustc 1.49.0 (e1884a8e3 2020-12-29)
binary: rustc
commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
commit-date: 2020-12-29
host: x86_64-apple-darwin
release: 1.49.0
Backtrace

thread 'main' panicked at 'Failure to run: Os { code: 2, kind: NotFound, message: "No such file or directory" }', test.rs:5:79
stack backtrace:
   0:        0x10038ec74 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::ha0848bb2602b5d05
   1:        0x1003a9690 - core::fmt::write::h9f3ccac2ef682b93
   2:        0x10038cd06 - std::io::Write::write_fmt::h0a47673aab280496
   3:        0x100390759 - std::panicking::default_hook::{{closure}}::h850c6aaf5e80c2f5
   4:        0x10039041d - std::panicking::default_hook::h037801299da6e1c6
   5:        0x100390ddb - std::panicking::rust_panic_with_hook::h76436d4cf7a368ac
   6:        0x100390905 - std::panicking::begin_panic_handler::{{closure}}::h516c76d70abf04f6
   7:        0x10038f0e8 - std::sys_common::backtrace::__rust_end_short_backtrace::h653290b4f930faed
   8:        0x10039086a - _rust_begin_unwind
   9:        0x1003ae0cf - core::panicking::panic_fmt::hde9134dd19c9a74f
  10:        0x1003adfd5 - core::option::expect_none_failed::h686aad664d56bca5
  11:        0x100377204 - core::result::Result<T,E>::expect::h4db214c9d11b030e
  12:        0x10037ae2e - test::main::h3085c8a5747c85a8
  13:        0x10037a00a - core::ops::function::FnOnce::call_once::h68c80bffef54ded6
  14:        0x100377c7d - std::sys_common::backtrace::__rust_begin_short_backtrace::h6c9d96006b516704
  15:        0x100377d00 - std::rt::lang_start::{{closure}}::h1b85b01ed6ecb62e
  16:        0x100391154 - std::rt::lang_start_internal::h36ccce6e8a047133
  17:        0x100377cd5 - std::rt::lang_start::h968f5e6067066713
  18:        0x10037ae92 - _main

Nightly:

rustc 1.51.0-nightly (c8915eebe 2021-01-07)
binary: rustc
commit-hash: c8915eebeaaef9f7cc1cff6ffd97f578b03c2ac9
commit-date: 2021-01-07
host: x86_64-apple-darwin
release: 1.51.0-nightly
Backtrace

thread 'main' panicked at 'Failure to run: Os { code: 2, kind: NotFound, message: "No such file or directory" }', test.rs:7:6
stack backtrace:
   0: rust_begin_unwind
             at /rustc/c8915eebeaaef9f7cc1cff6ffd97f578b03c2ac9/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/c8915eebeaaef9f7cc1cff6ffd97f578b03c2ac9/library/core/src/panicking.rs:92:14
   2: core::option::expect_none_failed
             at /rustc/c8915eebeaaef9f7cc1cff6ffd97f578b03c2ac9/library/core/src/option.rs:1266:5
   3: core::result::Result<T,E>::expect
   4: test::main
   5: core::ops::function::FnOnce::call_once

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-macosOperating system: macOST-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions