3

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?

2
  • 1
    What's the output of only that snippet? Commented Apr 3, 2022 at 20:11
  • @tifrel I won't paste stdout here since it's just compiler's "dude, where's my header?" rambling. Stderr is a bit more interesting, though, because it's git submodule's usage info, which is printed every time you enter a bad command. It made me think and I've just split the --depth 1 argument into 2 separate ones. Sure enough stderr is now empty, but the build script still errors out with litany of errors in stdout. Commented Apr 3, 2022 at 20:46

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I accidentally dropped the submodule while trying to solve this issue, which I detected by running git submodule update manually and achieving no effect. cargo build works perfectly automagically after the fix described in my comment. Please pretend this didn't happen and you haven't seen this question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.