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

依赖冲突导致Cargo无法构建含winit与nokhwa的Rust项目

解决nokhwa 0.9.4与web-sys的依赖冲突问题

问题背景

在基于winit的Rust项目中添加nokhwa 0.9.4版本时,Cargo编译出现依赖冲突,错误信息如下:

error: failed to select a version for `web-sys`.
    ... required by package `wgpu v0.11.0`
    ... which satisfies dependency `wgpu = "^0.11"` of package `nokhwa v0.9.4`
    ... which satisfies dependency `nokhwa = "^0.9.4"` of package `winit_wgpu_test v0.1.0 (C:\schabby\Rust\winit_wgpu_test)`
versions that meet the requirements `^0.3.53` are: 0.3.67, 0.3.66, 0.3.65, 0.3.64, 0.3.63, 0.3.62, 0.3.61, 0.3.60, 0.3.59, 0.3.58, 0.3.57, 0.3.56, 0.3.55, 0.3.54, 0.3.53

the package `wgpu` depends on `web-sys`, with features: `GpuBufferUsage` but `web-sys` does not have these features.


all possible versions conflict with previously selected packages.

  previously selected package `web-sys v0.3.64`
    ... which satisfies dependency `web-sys = "^0.3.53"` of package `wgpu v0.11.0`
    ... which satisfies dependency `wgpu = "^0.11"` of package `nokhwa v0.9.4`
    ... which satisfies dependency `nokhwa = "^0.9.4"` of package `winit_wgpu_test v0.1.0 (C:\schabby\Rust\winit_wgpu_test)`

failed to select a version for `web-sys` which could resolve this conflict

当前项目的Cargo.toml配置:

[dependencies]
env_logger = "0.10.0"
pollster = "0.3.0"
winit = { version = "0.29.10", features = ["rwh_05"]}

[dependencies.nokhwa]
version = "0.9.4"
features = ["input-msmf", "output-wgpu"]

注:必须使用nokhwa 0.9.4版本,因为新版本中output-wgpu特性已损坏;尝试过通过环境变量、命令行、Cargo.toml配置启用--cfg=web_sys_unstable_apis,但均无效。

解决方案

1. 强制锁定web-sys到兼容版本

wgpu 0.11.0依赖的web-sys版本范围是^0.3.53,但高版本web-sys已将GpuBufferUsage特性改名为小写的gpu_buffer_usage,导致匹配失败。需要锁定web-sys到仍保留大写特性名的旧版本(比如0.3.53):

在Cargo.toml中添加以下内容:

[patch.crates-io]
web-sys = { version = "0.3.53" }

2. 正确配置web_sys_unstable_apis

之前的配置无效可能是因为方式不对,以下是两种可靠的配置方式:

  • 临时编译(PowerShell):
    $env:RUSTFLAGS="--cfg=web_sys_unstable_apis"
    cargo build
    
  • 永久项目配置:
    在项目根目录创建.cargo/config.toml文件,写入:
    [build]
    rustflags = ["--cfg=web_sys_unstable_apis"]
    

3. 验证依赖状态

执行以下命令确认web-sys版本已被正确锁定:

cargo tree -p web-sys

确保输出中web-sys的版本为0.3.53,且没有其他依赖强制拉取更高版本。

内容的提问来源于stack exchange,提问作者Sebestyén Szabó

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 22:50:27