如何告诉 Cargo 使用 git 存储库作为间接依赖的来源而不是 crates.io?

How to tell Cargo to use a git repository as source for an indirect dependency instead of crates.io?

A few days ago, cross-compiling to JavaScript via Emscripten has finally hit nightly. I wanted to compile a project using glium in that manner. However, there are still many Emscripten-related bugs in many crates. While maintainers usually fix those bugs quickly, they don't necessarily release those bug fixes to crates.io马上。

在我的例子中,glium 取决于 glutinglutin 有一个错误现在已修复,但仅在 git 存储库中,而不是在 crates.io 中。 注意glutin不是我项目的直接依赖;只有一个间接的通过 glium!

如何告诉 Cargo 使用 the glutin repository 作为 glutin 的来源而不是 crates.io

您可以在项目的 Cargo.toml 中使用 [replace] 部分 。您可以找到有关该功能的文档 here in the Cargo documentation.

在您的情况下,glium 取决于 glutin 0.6.1。 crates.io 上的版本 0.6.1 仍然包含该错误。所以只需将其添加到您的 Cargo.toml:

[replace]
"glutin:0.6.1" = { git = 'https://github.com/tomaka/glutin' }

但是请注意,

[...] that the replaced crate must not only have the same name but also the same version.

但即使在版本不匹配的情况下(存储库已经包含更新的版本),如果 crate 的维护者为每个版本创建 git 标签(许多在 Rust 中),你仍然可能很幸运社区做)。在这种情况下,您只需指定标签:

[replace]
"glutin:0.6.1" = { 
    git = 'https://github.com/tomaka/glutin' 
    tag = 'v0.6.1'
}

遗憾的是,这不适用于 glutin,因为维护者并未为每个版本创建标签。在这种情况下,你可以简单地找到版本被提升之前的最后一次提交,并用 rev = 'b4a3d0...' 指定它,或者用 [=20= 指定一个特定的分支]键。