在 Cargo 中禁用注册表更新
Disable registry update in Cargo
如何禁止 cargo update
或 cargo build
尝试访问 github.com;但仍然从 crates.io
下载合适的包
我的 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 仍然可以下载它需要的包而不需要更新它的注册表?
如果您查看 configuring Cargo 的文档,您会注意到 [registry]
部分中有一个 index
键。这可以是 Git 存储库的任何路径。
因此,您可以制作 crates.io 索引的本地克隆。我通过像这样克隆它来验证这一点:
git clone --bare https://github.com/rust-lang/crates.io-index.git
然后编辑我的 Cargo 配置(具体来说,我更改了 ~/.cargo/config
,但这应该适用于文档描述的任何地方)以包含:
[registry]
index = "file:///F:/Data/Repositories/crates.io-index.git"
注意几点:
这不反映软件包的实际内容。那些来自不同的主机。但是,我不知道如何镜像这些:Cargo 在本地缓存这些方面要好得多。它应该足以cargo fetch
个包,然后将缓存的*.crate
个文件复制到$HOME/.cargo/registry/cache/*
.
这会导致您的 Cargo.lock
文件中的包标识符发生变化。这对于开发库来说不是问题,但是 会成为 二进制文件 的问题。标准做法是将您的 Cargo.lock
检查到二进制文件的源代码控制中,以便下游的每个人都使用完全相同的包版本构建。但是,修改后的索引意味着没有其他人能够使用该锁定文件构建包。
我通过在二进制包中放置 另一个 配置覆盖来解决这个问题,将索引重置为 "official" 一个,但这甚至可能是不可能的你的情况。在这种情况下,您可能需要从源代码管理中排除 Cargo.lock
,或者只需要一个 "doesn't use the official index" 分支。
遗憾的是,货物仅支持 --offline
和 config 以及 net.offline
。
这个flag加在this PR.
由于不再支持 registry.index 配置值,
我可以替换官方 crates.io 注册表使用添加这个
.cargo/config 文件在我的项目中
(或在 $CARGO_HOME%\.cargo 下):
[source]
[source.mirror]
registry = "http://localhost:8185/auser/crates.io-index.git"
[source.crates-io]
replace-with = "mirror"
也可以使用基于文件的 git 注册表克隆:
registry = "file://c:/github/crates.io-index.git"
使用 git clone --bare 或 --mirror 下载
cargo build 现在打印
Updating 'c:\github\crates.io-index.git' index
而不是 Updating crates.io index
如何禁止 cargo update
或 cargo build
尝试访问 github.com;但仍然从 crates.io
我的 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 仍然可以下载它需要的包而不需要更新它的注册表?
如果您查看 configuring Cargo 的文档,您会注意到 [registry]
部分中有一个 index
键。这可以是 Git 存储库的任何路径。
因此,您可以制作 crates.io 索引的本地克隆。我通过像这样克隆它来验证这一点:
git clone --bare https://github.com/rust-lang/crates.io-index.git
然后编辑我的 Cargo 配置(具体来说,我更改了 ~/.cargo/config
,但这应该适用于文档描述的任何地方)以包含:
[registry]
index = "file:///F:/Data/Repositories/crates.io-index.git"
注意几点:
这不反映软件包的实际内容。那些来自不同的主机。但是,我不知道如何镜像这些:Cargo 在本地缓存这些方面要好得多。它应该足以
cargo fetch
个包,然后将缓存的*.crate
个文件复制到$HOME/.cargo/registry/cache/*
.这会导致您的
Cargo.lock
文件中的包标识符发生变化。这对于开发库来说不是问题,但是 会成为 二进制文件 的问题。标准做法是将您的Cargo.lock
检查到二进制文件的源代码控制中,以便下游的每个人都使用完全相同的包版本构建。但是,修改后的索引意味着没有其他人能够使用该锁定文件构建包。我通过在二进制包中放置 另一个 配置覆盖来解决这个问题,将索引重置为 "official" 一个,但这甚至可能是不可能的你的情况。在这种情况下,您可能需要从源代码管理中排除
Cargo.lock
,或者只需要一个 "doesn't use the official index" 分支。
遗憾的是,货物仅支持 --offline
和 config 以及 net.offline
。
这个flag加在this PR.
由于不再支持 registry.index 配置值, 我可以替换官方 crates.io 注册表使用添加这个 .cargo/config 文件在我的项目中 (或在 $CARGO_HOME%\.cargo 下):
[source]
[source.mirror]
registry = "http://localhost:8185/auser/crates.io-index.git"
[source.crates-io]
replace-with = "mirror"
也可以使用基于文件的 git 注册表克隆:
registry = "file://c:/github/crates.io-index.git"
使用 git clone --bare 或 --mirror 下载
cargo build 现在打印
Updating 'c:\github\crates.io-index.git' index
而不是 Updating crates.io index