在 Cargo 中禁用注册表更新 [英] Disable registry update in Cargo

查看:63
本文介绍了在 Cargo 中禁用注册表更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何禁止 cargo updatecargo build 尝试访问 github.com;但仍然从 crates.io

How do I disable cargo update or cargo build from attempting to access github.com; but still download the appropriate package from crates.io

我的cargo.toml 中只有一个依赖项

I have a single dependency in my cargo.toml

[dependencies]
chrono = "0.2.14"

运行 cargo build

E:\>cargo build
    Updating registry `https://github.com/rust-lang/crates.io-index`
Unable to update registry https://github.com/rust-lang/crates.io-index

我们在工作时被 github.com 阻止,但不是 crates.io.有没有一个选项让cargo 仍然可以下载它需要的包而无需更新它的注册表?

We are blocked from github.com at work but not crates.io. Is there an option where cargo can still download the packages it needs without needing to update it's registry?

推荐答案

如果您查看 configuring 的文档Cargo,您会注意到 [registry] 部分中有一个 index 键.这可以是 Git 存储库的任何路径.

If you look at the documentation for configuring Cargo, you'll note there is an index key in the [registry] section. This can be any path to a Git repository.

因此,您可以制作 crates.io 索引的本地克隆.我通过像这样克隆它来验证这一点:

As such, you can make a local clone of the crates.io index. I verified this by cloning it like so:

git clone --bare https://github.com/rust-lang/crates.io-index.git

然后编辑我的 Cargo 配置(具体来说,我更改了 ~/.cargo/config,但这应该适用于文档描述的任何地方)以包含:

then editing my Cargo configuration (specifically, I changed ~/.cargo/config, but this should work anywhere the documentation describes) to contain:

[registry]
index = "file:///F:/Data/Repositories/crates.io-index.git"

注意事项:

  1. 反映包的实际内容.那些来自不同的主机.但是,我不知道如何反映这些内容:Cargo 在本地缓存这些内容要好得多.应该足以cargo fetch包,然后将缓存的*.crate文件复制到$HOME/.cargo/registry/缓存/*.

  1. This does not mirror the actual contents of the packages. Those come from a different host. I do not know how to mirror those, however: Cargo is much better about caching those locally. It should be enough to cargo fetch packages, then copy the cached *.crate files in $HOME/.cargo/registry/cache/*.

这会导致 Cargo.lock 文件中的包裹标识符发生变化.这对于开发库来说不是问题,但对于二进制文件来说,它确实会成为一个问题.标准做法是将您的 Cargo.lock 签入二进制文件的源代码管理中,以便下游的每个人都使用完全相同的包版本进行构建.但是,修改后的索引意味着没有其他人能够在该锁定文件就位的情况下构建包.

This causes the package identifiers in your Cargo.lock file to change. This isn't a problem for developing libraries, but it does become a problem for binaries. Standard practice is to check your Cargo.lock into source control for binaries so that everyone downstream builds with the exact same package versions. However, the modified index means no one else will be able to build the package with that lock file in place.

我已经通过在二进制包中放置另一个配置覆盖来解决这个问题,将索引重置为官方",但这在您的情况下甚至可能是不可能的.在这种情况下,您可能需要从源代码管理中排除 Cargo.lock,或者只是有一个不使用官方索引"分支.

I've worked around this by placing another config override within binary packages that resets the index to the "official" one, but that may not even be possible in your situation. In that case, you may need to either exclude Cargo.lock from source control, or just have a "doesn't use the official index" branch.

这篇关于在 Cargo 中禁用注册表更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆