You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将cloud-hypervisor这类Cargo工作区项目作为库使用时的依赖版本冲突解决方法咨询

将cloud-hypervisor这类Cargo工作区项目作为库使用时的依赖版本冲突解决方法咨询

我最近尝试把cloud-hypervisor的源码当作库集成到自己的工具里,但遇到了依赖版本冲突的问题,想请教大家有没有解决办法。

情况是这样的:cloud-hypervisor用的是Cargo工作区,根目录的工作区通过workspace.dependencies统一固定了依赖版本,所以他们自己构建二进制的时候不会有问题。但我在自己项目里用cargo add引入时就出问题了,测试步骤很简单:

cargo new chlibtest
cd chlibtest
cargo add vmm --git https://github.com/cloud-hypervisor/cloud-hypervisor --rev v46.0 --features kvm
cargo build

构建时抛出了类型不匹配的错误,核心原因是kvm_ioctls出现了两个版本:cloud-hypervisor的工作区依赖指定的是0.19.1,而vfio-ioctls依赖的是0.22.0,导致两个版本的DeviceFd类型不兼容,具体错误信息如下:

error[E0308]: mismatched types
--> /home/andrew/.cargo/git/checkouts/cloud-hypervisor-33c0e9c9c1156328/c9a39cf/hypervisor/src/kvm/mod.rs:489:39
| 489 | Ok(VfioDeviceFd::new_from_kvm(device_fd))
| -------------------------- ^^^^^^^^^ expected kvm_ioctls::ioctls::device::DeviceFd, found DeviceFd
| | | arguments to this function are incorrect
| note: two different versions of crate kvm_ioctls are being used; two types coming from two different versions of the same crate are different types even if they look the same
| ::: /home/andrew/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/kvm-ioctls-0.22.0/src/ioctls/device.rs:17:1
| 17 | pub struct DeviceFd {
| ^^^^^^^^^^^^^^^^^^^ this is the expected type kvm_ioctls::ioctls::device::DeviceFd
--> /home/andrew/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/kvm-ioctls-0.19.1/src/ioctls/device.rs:17:1
| 17 | pub struct DeviceFd {
| ^^^^^^^^^^^^^^^^^^^ this is the found type DeviceFd
| ::: /home/andrew/.cargo/git/checkouts/cloud-hypervisor-33c0e9c9c1156328/c9a39cf/hypervisor/src/kvm/mod.rs:26:5
| 26 | use kvm_ioctls::{NoDatamatch, VcpuFd, VmFd};
| ---------- one version of crate kvm_ioctls used here, as a direct dependency of the current crate
... 107 | use vfio_ioctls::VfioDeviceFd;
| ----------- one version of crate kvm_ioctls used here, as a dependency of crate vfio_ioctls
= help: you can use cargo tree to explore your dependency tree
note: associated function defined here
--> /home/andrew/.cargo/git/checkouts/vfio-2d61e71a0a55b0af/91f2c7b/vfio-ioctls/src/vfio_device.rs:58:12
| 58 | pub fn new_from_kvm(fd: KvmDeviceFd) -> Self {
| ^^^^^^^^^^^^

cloud-hypervisor的Cargo.toml里工作区依赖是这样的(截取部分):

[workspace.dependencies]
kvm-ioctls = "0.19.1"
vfio-ioctls = { git = "https://github.com/rust-vmm/vfio", branch = "main", default-features = false }

我理解cloud-hypervisor自己构建时没问题,是因为Cargo会用工作区指定的0.19.1版本覆盖vfio-ioctls里的版本要求,但我的项目里做不到这点。我试过在自己项目的workspace.dependencies里指定kvm-ioctls = "0.19.1",也试过用[patch.crates-io]来强制版本,但都没让Cargo统一用同一个版本。

虽然cloud-hypervisor本身不是设计来当库用的,但它对我的工具来说真的很有用,想问问有没有什么办法能绕过这个依赖冲突问题?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 09:38:02