“git 子模块更新”在 build.rs 中使用 std::process::Command 无效

发布于 2025-01-19 02:57:58 字数 1055 浏览 2 评论 0 原文

我正在尝试将 C++ 软件移植到 Rust,其中一部分是使用 C++ 库获取 git 子模块。我想将该操作集成到我的自定义 build.rs 脚本中,而不是在运行之前强制用户手动执行此操作 >货物构建。这是我的 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
}

不幸的是:命令没有执行 ->未获取子模块 -> C++ 编译器开始抱怨缺少标头 ->货物抛出错误。除此之外,build.rs 完全没问题,因为在 cargo build 之前手动运行 git submodule update 后它工作得很好。令人惊讶的是,将命令更改为 echo Cargo:Warning=test,捕获 std::process::Command使用 io::stdout().write_all(&output.stdout).unwrap(); 输出它导致货物正确报告 警告。 touch测试文件,然后在编译后rm它也同样有效。为什么 git 不起作用,但这些命令却起作用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事风中埋 2025-01-26 02:57:58

鉴于您自己的答案,我还想建议您查看返回类型std::process::Command::output。这是一个 std::io::Result ,虽然我不能确定,但​​我希望它会失败,例如如果指定的命令不可访问,但返回一个 Ok 非零退出代码的变体

每当您运行 std::process::Command 时,您都可以(并且可能应该)检查所获得的 Output 结构中的退出代码,以便您可以查明执行该操作的步骤。您的构建脚本失败。

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.

镜花水月 2025-01-26 02:57:58

我在尝试解决此问题时不小心删除了子模块,我通过手动运行 git submodule update 检测到该问题,但没有效果。 cargo build我的评论中描述的修复后自动完美工作。请假装这件事没有发生并且您没有看到这个问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文