Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"git submodule update" in build.rs with std::process::Command has no effect

I'm trying to port C++ software to Rust and one of the parts is fetching a git submodule with a C++ library. I'd like to integrate that action into my custom build.rs script instead of forcing a user to do that manually before running cargo build. Here is my build.rs:

fn main() {
    std::process::Command::new("git")
        .args([
            "submodule",
            "update",
            "--init",
            "--depth 1",
            "--recommend-shallow",
    ])
    .output()
    .expect("Failed to fetch git submodules!");

    // here C++ code compilation with cc::Build happens
}

Unfortunately: the command didn't execute -> the submodule wasn't fetched -> C++ compiler started complaining about missing headers -> cargo threw an error. Otherwise than that build.rs is totally fine, because it worked perfectly after running git submodule update manually before cargo build. Suprisingly enough changing the command to echo cargo:warning=test, catching the output of the std::process::Command and outputting it with io::stdout().write_all(&output.stdout).unwrap(); resulted in cargo correctly reporting a warning. touching a test file and later rming it after the compilation worked as well. Why git doesn't work, but these commands do?

like image 665
Ziemowit Zabawa Avatar asked Mar 06 '26 07:03

Ziemowit Zabawa


1 Answers

Given your own answer, I'd also like to suggest taking a look at the return type of std::process::Command::output. This is an std::io::Result, and while I cannot tell for sure, I would expect this to fail e.g. if the specified command is not accessible, but return an Ok variant on non-zero exit codes.

You can (and probably should) inspect the exit code from the obtained Output struct whenever you run std::process::Command, such that you can pinpoint the step at which your build script fails.

like image 118
tifrel Avatar answered Mar 08 '26 20:03

tifrel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!