货物登记目录

Cargo registry directory

如何指定 cargo 保存解压源的另一个目录,例如在 Windows 这是 %userprofile%/.cargo/registry/src?

我的工作是收集箱子,例如:

myproject-tree/
  foo
    +Cargo.toml
  bar
    +Cargo.toml

所以我希望在 myproject-tree 目录附近的某处解压加载依赖项,例如

myproject-tree/
cargo-cache/

我认为这会让检查依赖代码变得更容易(例如在带有文件树 UI 的代码编辑器中)。我可以看到 CARGO_HOME 好像有这样的东西,但看起来不方便,例如每次我要使用 myproject-tree 时,我都必须设置它。

看起来这是可能的。我认为它叫做 "vendoring" 并且像 Fuchsia 和 Mozilla 这样的存储库在它们的存储库中有第三方包的源副本。这是我发现的:

  • Cargo 支持在 .cargo/config 中配置的“Source replacement”东西,它可以在项目相对目录中:

With this configuration Cargo attempts to look up all crates in the directory "vendor" rather than querying the online registry at crates.io. Using source replacement Cargo can express:

Vendoring - custom sources can be defined which represent crates on the local filesystem. These sources are subsets of the source that they're replacing and can be checked into packages if necessary.

Mirroring - sources can be replaced with an equivalent version which acts as a cache for crates.io itself.

Simply run cargo vendor inside of any Cargo project: