如何固定板条箱的间接依赖关系? [英] How do I pin indirect dependencies of a crate?

查看:15
本文介绍了如何固定板条箱的间接依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目 A 依赖于依赖于库 C 的库 B.

库 B 将依赖版本设置为 "*" (any),以便 Cargo 下载 C 的最新版本.

如何指示 Cargo 使用库 C 的特定版本构建库 B?


我正在尝试构建 iron.

当前构建失败,但我可以看到上次成功构建,包括 Rust 和 Cargo 包版本.

我下载了构建中使用的特定 Rust nightly,并通过编辑 Cargo.toml 将铁的直接依赖项设置为该构建中使用的依赖项:

[依赖项]超 =0.0.18"类型映射 =0.0.5";url =0.2.9";

rust-serializedurltime 包的依赖,下载为最新版本,不能编译使用我的特定 Rust 版本.

如果我使用上面 Travis 构建中使用的版本,我相信它会成功编译.

解决方案

手动编辑

您可以查看 Iron,修改 Cargo.toml 以指定版本(正如您已经完成的那样).然后你重复这个过程,检查url,修改它的Cargo.toml,然后确保你在Iron的url中使用你的url版本代码>Cargo.toml.冲洗并重复.

补丁覆盖

来自 货物文档:

<块引用>

Cargo.toml[patch] 部分可用于覆盖与其他副本的依赖关系.语法类似于 [dependencies] 部分:

[patch.crates-io]foo = { git = 'https://github.com/example/foo' }酒吧 = { 路径 = '我的/本地/酒吧' }

<块引用>

可以使用不存在的 crate 版本修补源,也可以使用已经存在的 crate 版本修补它们.如果使用源中已存在的 crate 版本修补源,则替换源的原始 crate.

路径覆盖

来自货物文档:><块引用>

有时您只是临时处理一个板条箱,而您不想像上面的 [patch] 部分那样修改 Cargo.toml.对于这个用例,Cargo 提供了一个更有限的覆盖版本,称为路径覆盖.

路径覆盖通过.cargo/config.toml 而不是 Cargo.toml.在 .cargo/config.toml 中,您将指定一个名为 paths 的键:

paths = [/path/to/uuid"]

特定版本

您可以简单地为您知道适用于您的 Rust 版本的每个依赖项指定版本(或 SHA 哈希).Cargo 应该能够解决传递依赖关系,并且如果有一个满足所有要求的版本,可以将您锁定到以前的版本.

或者,您可以使用 cargo update -p somecrate --precise major.minor.patch 指定 crate 的确切版本并将其记录在您的 Cargo.lock.

这可能不适用于所有情况;Rust 可以将同一库的多个版本编译成一个二进制文件.这意味着没有一个地方可以指定一个适用于所有地方的版本.

解决赏金问题

约翰补充::><块引用>

我有一个依赖 zstd 的板条箱.zstd 依赖于 zstd-safe.zstd-safe 依赖于 zstd-sys.我的箱子被链接到一个 C++ 二进制文件中,该二进制文件也将 zstd 链接为 C 库.zstd C 库的版本和 zstd-sys 提供的 zstd 的版本必须完全匹配,否则我会得到 链接错误.所以我想固定一个确切版本的 zstd-sys

本案例可以遵循特定版本";上面的例子,但因为 zstd-sys 使用 links 键,在整个 crate 图中只能有 one 那个 crate.这意味着您可以将 zstd-sys 添加到您的顶级依赖项中,并且如果引入了冲突版本,Cargo 会抱怨:

[依赖项]zstd =0.9.0";zstd-sys ==1.6.1";

如果我编辑它以指定版本 1.6.0,我会收到一个错误:

错误:无法为 `zstd-sys` 选择一个版本.... 包`zstd-safe v4.1.1+zstd.1.5.0` 需要...依赖于`zstd v0.9.0+zstd.1.5.0`...这取决于`so v0.1.0 (/private/tmp/so)`满足`=1.6.1`的版本是:1.6.1+zstd.1.5.0包`zstd-sys` 链接到本地​​库`zstd`,但它与之前链接到`zstd` 的包冲突:包`zstd-sys v1.6.0+zstd.1.5.0`...这取决于`so v0.1.0 (/private/tmp/so)`

My project A depends on library B that depends on library C.

Library B sets the dependency version to "*" (any) so Cargo will download the latest version of C.

How can I instruct Cargo to build library B using a specific version of library C?


I'm trying to build iron.

The current build is failing, but I can see the last successful build, including Rust and Cargo package versions.

I downloaded the specific Rust nightly used in the build and I've set the the direct dependencies of iron to the ones used in that build by editing Cargo.toml:

[dependencies]
hyper = "0.0.18"
typemap = "0.0.5"
url = "0.2.9"

rust-serialized, which is a dependency of the url and time packages, is downloaded as the latest version which doesn't compile with my specific Rust version.

If I used the version used in the Travis build above I'm sure it will compile successfully.

解决方案

Manual editing

You can check out Iron, modify Cargo.toml to specify versions (as you have already done). Then you repeat the process, checking out url, modifying its Cargo.toml, then make sure you are using your version of url in Iron's Cargo.toml. Rinse and repeat.

Patch overrides

From the Cargo docs:

The [patch] section of Cargo.toml can be used to override dependencies with other copies. The syntax is similar to the [dependencies] section:

[patch.crates-io]
foo = { git = 'https://github.com/example/foo' }
bar = { path = 'my/local/bar' }

Sources can be patched with versions of crates that do not exist, and they can also be patched with versions of crates that already exist. If a source is patched with a crate version that already exists in the source, then the source's original crate is replaced.

Path overrides

From the Cargo docs:

Sometimes you're only temporarily working on a crate and you don't want to have to modify Cargo.toml like with the [patch] section above. For this use case Cargo offers a much more limited version of overrides called path overrides.

Path overrides are specified through .cargo/config.toml instead of Cargo.toml. Inside of .cargo/config.toml you'll specify a key called paths:

paths = ["/path/to/uuid"]

Specific versions

You might be able to simply specify versions (or SHA hashes) for each dependency that you know works with your Rust version. Cargo should be able to resolve the transitive dependencies and lock you to a previous version if there is one that fits all the requirements.

Alternatively, you can use cargo update -p somecrate --precise major.minor.patch to specify the exact version of a crate and record it in your Cargo.lock.

This may not work in all cases; Rust can have multiple versions of the same library compiled into one binary. That would mean that there's no one place you can specify a version that applies all over.

Addressing the bounty

John adds:

I have a crate that depends on zstd. zstd depends on zstd-safe. zstd-safe depends on zstd-sys. My crate gets linked into a C++ binary that also links zstd as a C library. The versions of the zstd C library and the zstd vendored by zstd-sys must match exactly, or I get link errors. So I want to pin an exact version of zstd-sys

This case can follow the "specific versions" example above, but because zstd-sys uses a links key, there can only ever be exactly one of that crate in the entire crate graph. That means you can add zstd-sys to your top-level dependencies and feel comfortable that Cargo will complain if a conflicting version is introduced:

[dependencies]
zstd = "0.9.0"
zstd-sys = "=1.6.1"

If I edit this to specify version 1.6.0, I get an error:

error: failed to select a version for `zstd-sys`.
    ... required by package `zstd-safe v4.1.1+zstd.1.5.0`
    ... which is depended on by `zstd v0.9.0+zstd.1.5.0`
    ... which is depended on by `so v0.1.0 (/private/tmp/so)`
versions that meet the requirements `=1.6.1` are: 1.6.1+zstd.1.5.0

the package `zstd-sys` links to the native library `zstd`, but it conflicts with a previous package which links to `zstd` as well:
package `zstd-sys v1.6.0+zstd.1.5.0`
    ... which is depended on by `so v0.1.0 (/private/tmp/so)`

这篇关于如何固定板条箱的间接依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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