“git 子模块更新”在 build.rs 中使用 std::process::Command 无效
我正在尝试将 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 不起作用,但这些命令却起作用?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于您自己的答案,我还想建议您查看返回类型
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 anstd::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 anOk
variant on non-zero exit codes.You can (and probably should) inspect the exit code from the obtained
Output
struct whenever you runstd::process::Command
, such that you can pinpoint the step at which your build script fails.我在尝试解决此问题时不小心删除了子模块,我通过手动运行 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.