关于rust:文盘Rust-本地库引发的依赖冲突

5次阅读

共计 3180 个字符,预计需要花费 8 分钟才能阅读完成。

作者:京东科技 贾世闻

问题形容

clickhouse 的原生 rust 客户端目前比拟好的有两个 clickhouse-rs 和 clickhouse.rs。clickhouse-rs 是 tcp 连贯;clickhouse.rs 是 http 连贯。两个库在独自应用时没有任何问题,然而,在同一工程同时援用时会报错。

  • Cargo.toml

    # clickhouse http
    clickhouse = {git = "https://github.com/loyd/clickhouse.rs", features =      ["test-util"]}
    
    # clickhouse tcp
    clickhouse-rs = {git = "https://github.com/suharev7/clickhouse-rs",     features = ["default"]}
    
    
  • 报错如下

        Blocking waiting for file lock on package cache
        Updating git repository `https://github.com/suharev7/clickhouse-rs`
        Updating crates.io index
    error: failed to select a version for `clickhouse-rs-cityhash-sys`.
        ... required by package `clickhouse-rs v1.0.0-alpha.1 (https://github.  com/suharev7/clickhouse-rs#ecf28f46)`
        ... which satisfies git dependency `clickhouse-rs` of package   `conflict v0.1.0 (/Users/jiashiwen/rustproject/conflict)`
    versions that meet the requirements `^0.1.2` are: 0.1.2
    
    the package `clickhouse-rs-cityhash-sys` links to the native library   `clickhouse-rs`, but it conflicts with a previous package which links to   `clickhouse-rs` as well:
    package `clickhouse-rs-cityhash-sys v0.1.2`
        ... which satisfies dependency `clickhouse-rs-cityhash-sys = "^0.1.2"`   (locked to 0.1.2) of package `clickhouse v0.11.2 (https://github.com/  loyd/clickhouse.rs#4ba31e65)`
        ... which satisfies git dependency `clickhouse` (locked to 0.11.2) of   package `conflict v0.1.0 (/Users/jiashiwen/rustproject/conflict)`
    Only one package in the dependency graph may specify the same links value.   This helps ensure that only one copy of a native library is linked in the   final binary. Try to adjust your dependencies so that only one package   uses the links ='clickhouse-rs-cityhash-sys' value. For more information,   see https://doc.rust-lang.org/cargo/reference/resolver.html#links.
    
    failed to select a version for `clickhouse-rs-cityhash-sys` which could   resolve this conflict
    

谬误形容还是很分明的,clickhouse-rs-cityhash-sys 这个库抵触了。认真看了一下两个库的源码,援用 clickhouse-rs-cityhash-sys 库的形式是不一样的。clickhouse.rs 是在其 Cargo.toml 文件中应用最广泛的形式援用的

clickhouse-rs-cityhash-sys = {version = "0.1.2", optional = true}

clickhouse-rs 是通过本地形式援用的

[dependencies.clickhouse-rs-cityhash-sys]
path = "clickhouse-rs-cityhash-sys"
version = "0.1.2"

clickhouse-rs-cityhash-sys 的源码间接放在 clickhouse-rs 工程目录上面。

一开始是有个直观的想法,如果在一个工程中通过 workspace 进行隔离,是不是会解决抵触问题呢?于是,工程的目录构造从这样

.
├── Cargo.lock
├── Cargo.toml
└── src
    └── main.rs

改成了这样

.
├── Cargo.lock
├── Cargo.toml
├── ck_http
│   ├── Cargo.toml
│   └── src
├── ck_tcp
│   ├── Cargo.toml
│   └── src
└── src
    └── main.rs

新建了两个 lib

cargo new ck_http --lib
cargo new ck_tcp --lib

在 workspace 中别离利用 clickhouse-rs 和 clickhouse.rs , 删除根下 Cargo.toml 文件中的依赖关系。很惋惜,workspace 没有解决问题,报错没有一点儿差异。

又认真看了看报错,外面有这样一段

  the package `clickhouse-rs-cityhash-sys` links to the native library   `clickhouse-rs`, but it conflicts with a previous package which links to   `clickhouse-rs`

难道是 clickhouse-rs 这个名字抵触了?间接把 clickhouse-rs 源码拉下来作为本地库来试试呢?于是把 clickhouse-rs clone 到本地,稍稍批改一下 ck_tcp workspace 的 Cargo.toml

clickhouse-rs = {path = "../../clickhouse-rs", features = ["default"]}

编译后抵触仍旧存在。翻翻 clickhouse-rs/clickhouse-rs-cityhash-sys/Cargo.toml,外面的一个配置很可疑

[package]
...
...
links = "clickhouse-rs"

把 links 轻易改个名字比方:links = “ck-rs-cityhash-sys”,编译就通过了。

谬误提醒中这句话很重要

Only one package in the dependency graph may specify the same links value.

看了一下 links 字段的含意

The links field
The links field specifies the name of a native library that is being linked to. More information can be found in the links section of the build script guide.

links 指定了本地包被链接的名字,在这里引起了抵触,改掉本地包中的名字天然解决了抵触,在依赖图中保障唯一性很重要。

本文波及代码 github 仓库,有趣味的同学能够亲自试一试

下期见。

正文完
 0