rustonnxonnxruntime

Getting errors in `wgpu` crate when used as a dependency of `wonnx`


When I try and build my project, I get these errors in the wgpu crate, which is a dependency of the wonnx crate that I am using.

❯ $env:RUSTFLAGS="--cfg=web_sys_unstable_apis"; wasm-pack build --target bundler
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
[ME]: Truncated output for stackoverflow                                                                                                                                                                                                        
   Compiling wgpu v0.16.3                                                                                                                                                                                                          
error[E0061]: this function takes 1 argument but 2 arguments were supplied                                                                                                                                                         
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.16.3\src\backend\web.rs:1523:13
     |
1523 |             web_sys::GpuVertexState::new(desc.vertex.entry_point, &module.0);
     |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -------------------------
     |                                          |
     |                                          unexpected argument of type `&str`
     |                                          help: remove the extra argument
     |
note: associated function defined here
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\web-sys-0.3.69\src\features\gen_GpuVertexState.rs:27:12
     |
27   |     pub fn new(module: &GpuShaderModule) -> Self {
     |            ^^^

error[E0061]: this function takes 2 arguments but 3 arguments were supplied                                                                                                                                                        
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.16.3\src\backend\web.rs:1598:17
     |
1598 |                 web_sys::GpuFragmentState::new(frag.entry_point, &module.0, &targets);
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ----------------             -------- unexpected argument of type `&Array`
     |                                                |
     |                                                expected `&GpuShaderModule`, found `&str`
     |
     = note: expected reference `&GpuShaderModule`
                found reference `&str`
note: associated function defined here
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\web-sys-0.3.69\src\features\gen_GpuFragmentState.rs:27:12
     |
27   |     pub fn new(module: &GpuShaderModule, targets: &::wasm_bindgen::JsValue) -> Self {
     |            ^^^
help: remove the extra argument
     |
1598 -                 web_sys::GpuFragmentState::new(frag.entry_point, &module.0, &targets);
1598 +                 web_sys::GpuFragmentState::new(/* &GpuShaderModule */, &module.0);
     |

error[E0061]: this function takes 1 argument but 2 arguments were supplied
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.16.3\src\backend\web.rs:1623:13
     |
1623 |             web_sys::GpuProgrammableStage::new(desc.entry_point, &shader_module.0);
     |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ------------------
     |                                                |
     |                                                unexpected argument of type `&str`
     |                                                help: remove the extra argument
     |
note: associated function defined here
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\web-sys-0.3.69\src\features\gen_GpuProgrammableStage.rs:27:12
     |
27   |     pub fn new(module: &GpuShaderModule) -> Self {
     |            ^^^

error[E0599]: no method named `write_timestamp` found for struct `GpuCommandEncoder` in the current scope                                                                                                                          
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.16.3\src\backend\web.rs:2304:14
     |
2302 | /         encoder_data
2303 | |             .0
2304 | |             .write_timestamp(&query_set_data.0, query_index);
     | |             -^^^^^^^^^^^^^^^ method not found in `GpuCommandEncoder`
     | |_____________|
     | 

error[E0308]: mismatched types                                                                                                                                                                                                     
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.16.3\src\backend\web.rs:2556:17
     |
2554 |             .set_bind_group_with_u32_array_and_f64_and_dynamic_offsets_data_length(
     |              --------------------------------------------------------------------- arguments to this method are incorrect
2555 |                 index,
2556 |                 &bind_group_data.0,
     |                 ^^^^^^^^^^^^^^^^^^ expected `Option<&GpuBindGroup>`, found `&GpuBindGroup`
     |
     = note:   expected enum `Option<&GpuBindGroup>`
             found reference `&GpuBindGroup`
note: method defined here
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\web-sys-0.3.69\src\features\gen_GpuComputePassEncoder.rs:201:12
     |
201  |     pub fn set_bind_group_with_u32_array_and_f64_and_dynamic_offsets_data_length(
     |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try wrapping the expression in `Some`
     |
2556 |                 Some(&bind_group_data.0),
     |                 +++++                  +

error[E0308]: mismatched types
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.16.3\src\backend\web.rs:2681:17
     |
2679 |             .set_bind_group_with_u32_array_and_f64_and_dynamic_offsets_data_length(
     |              --------------------------------------------------------------------- arguments to this method are incorrect
2680 |                 index,
2681 |                 &bind_group_data.0,
     |                 ^^^^^^^^^^^^^^^^^^ expected `Option<&GpuBindGroup>`, found `&GpuBindGroup`
     |
     = note:   expected enum `Option<&GpuBindGroup>`
             found reference `&GpuBindGroup`
note: method defined here
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\web-sys-0.3.69\src\features\gen_GpuRenderBundleEncoder.rs:131:12
     |
131  |     pub fn set_bind_group_with_u32_array_and_f64_and_dynamic_offsets_data_length(
     |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try wrapping the expression in `Some`
     |
2681 |                 Some(&bind_group_data.0),
     |                 +++++                  +

error[E0308]: mismatched types
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.16.3\src\backend\web.rs:2731:21
     |
2729 |                 encoder_data.0.set_vertex_buffer_with_f64_and_f64(
     |                                ---------------------------------- arguments to this method are incorrect
2730 |                     slot,
2731 |                     &buffer_data.0,
     |                     ^^^^^^^^^^^^^^ expected `Option<&GpuBuffer>`, found `&GpuBuffer`
     |
     = note:   expected enum `Option<&GpuBuffer>`
             found reference `&GpuBuffer`
note: method defined here
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\web-sys-0.3.69\src\features\gen_GpuRenderBundleEncoder.rs:617:12
     |
617  |     pub fn set_vertex_buffer_with_f64_and_f64(
     |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try wrapping the expression in `Some`
     |
2731 |                     Some(&buffer_data.0),
     |                     +++++              +

error[E0308]: mismatched types
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.16.3\src\backend\web.rs:2739:55
     |
2739 |                     .set_vertex_buffer_with_f64(slot, &buffer_data.0, offset as f64);
     |                      --------------------------       ^^^^^^^^^^^^^^ expected `Option<&GpuBuffer>`, found `&GpuBuffer`
     |                      |
     |                      arguments to this method are incorrect
     |
     = note:   expected enum `Option<&GpuBuffer>`
             found reference `&GpuBuffer`
note: method defined here
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\web-sys-0.3.69\src\features\gen_GpuRenderBundleEncoder.rs:546:12
     |
546  |     pub fn set_vertex_buffer_with_f64(
     |            ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try wrapping the expression in `Some`
     |
2739 |                     .set_vertex_buffer_with_f64(slot, Some(&buffer_data.0), offset as f64);
     |                                                       +++++              +

error[E0308]: mismatched types
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.16.3\src\backend\web.rs:2896:17
     |
2894 |             .set_bind_group_with_u32_array_and_f64_and_dynamic_offsets_data_length(
     |              --------------------------------------------------------------------- arguments to this method are incorrect
2895 |                 index,
2896 |                 &bind_group_data.0,
     |                 ^^^^^^^^^^^^^^^^^^ expected `Option<&GpuBindGroup>`, found `&GpuBindGroup`
     |
     = note:   expected enum `Option<&GpuBindGroup>`
             found reference `&GpuBindGroup`
note: method defined here
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\web-sys-0.3.69\src\features\gen_GpuRenderPassEncoder.rs:218:12
     |
218  |     pub fn set_bind_group_with_u32_array_and_f64_and_dynamic_offsets_data_length(
     |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try wrapping the expression in `Some`
     |
2896 |                 Some(&bind_group_data.0),
     |                 +++++                  +

error[E0308]: mismatched types
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.16.3\src\backend\web.rs:2946:21
     |
2944 |                 pass_data.0.set_vertex_buffer_with_f64_and_f64(
     |                             ---------------------------------- arguments to this method are incorrect
2945 |                     slot,
2946 |                     &buffer_data.0,
     |                     ^^^^^^^^^^^^^^ expected `Option<&GpuBuffer>`, found `&GpuBuffer`
     |
     = note:   expected enum `Option<&GpuBuffer>`
             found reference `&GpuBuffer`
note: method defined here
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\web-sys-0.3.69\src\features\gen_GpuRenderPassEncoder.rs:704:12
     |
704  |     pub fn set_vertex_buffer_with_f64_and_f64(
     |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try wrapping the expression in `Some`
     |
2946 |                     Some(&buffer_data.0),
     |                     +++++              +

error[E0308]: mismatched types
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.16.3\src\backend\web.rs:2954:55
     |
2954 |                     .set_vertex_buffer_with_f64(slot, &buffer_data.0, offset as f64);
     |                      --------------------------       ^^^^^^^^^^^^^^ expected `Option<&GpuBuffer>`, found `&GpuBuffer`
     |                      |
     |                      arguments to this method are incorrect
     |
     = note:   expected enum `Option<&GpuBuffer>`
             found reference `&GpuBuffer`
note: method defined here
    --> C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\web-sys-0.3.69\src\features\gen_GpuRenderPassEncoder.rs:633:12
     |
633  |     pub fn set_vertex_buffer_with_f64(
     |            ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try wrapping the expression in `Some`
     |
2954 |                     .set_vertex_buffer_with_f64(slot, Some(&buffer_data.0), offset as f64);
     |                                                       +++++              +

   Compiling num v0.4.3                                                                                                                                                                                                            
Some errors have detailed explanations: E0061, E0308, E0599.                                                                                                                                                                       
For more information about an error, try `rustc --explain E0061`.
   Compiling futures v0.3.30
error: could not compile `wgpu` (lib) due to 11 previous errors                                                                                                                                                                    
warning: build failed, waiting for other jobs to finish...
Error: Compiling your crate to WebAssembly failed                                                                                                                                                                                  
Caused by: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit code: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"
[package]
name = "wonnx-repro"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm-bindgen = "0.2.92"
web-sys = { version = "0.3.69" }
wonnx = "0.5.1"

lib.rs can be empty.

Please see the complete repro repo at jacobhq/wonnx-repro

Output of cargo tree in this gist due to stackoverflow length limits.

I tried:

If this is a known issue, and I'm being dumb, please point it out in the comments. If you've never heard of it before, please also comment, and I'll end up opening a GitHub issue (maybe a better place to start I know).

Thank you!


Solution

  • You are specifying web_sys_unstable_apis, which is, by definition, unstable. That is, changes can occur in the APIs across minor versions.

    This is what happened here. wonnx relies on an old, and unmaintained, version of wgpu (0.16). That versions relies on some old version of web-sys (0.3.61). web-sys has gained new minor versions since, and minor versions promise to keep the API the same, but excluding unstable APIs. Cargo used the newer web-sys versions (because they are SemVer-compatible), which broke wgpu.

    The real fault here is probably of wgpu, since when relying on unstable APIs, you should generally specify a precise version requirement (=version) exactly to avoid situations like that. But I can understand people are hesitant to use precise requirements, since they can break in various ways.

    The fault is also partially of wonnx, by not updating to the newer wgpu version. This is something you can open an issue about, but I don't know how much work it will be to update to the current wgpu version, since it is SemVer-incompatible.

    Other than waiting for wonnx to upgrade their wgpu, the easiest fix is to pin web-sys version's yourself. That means patching wgpu. Instead of cloning wgpu, an easier way will be to copy it from your registry cache, normally located in <home>/.cargo/registry/src/index.crates.io-<some_hash>. Find the wgpu-0.16.3 directory there (it will be there if you already tried to build your project, if not you can specify it as a dependency some temporary project and cargo fetch), copy it, change Cargo.toml in the lines:

    [target."cfg(target_arch = \"wasm32\")".dependencies.web-sys]
    features = [...]
    version = "0.3.61"
    
    ...
    
    [target."cfg(target_arch = \"wasm32\")".dev-dependencies.web-sys]
    features = [...]
    version = "0.3.61"
    

    To:

    [target."cfg(target_arch = \"wasm32\")".dependencies.web-sys]
    features = [...]
    version = "=0.3.61"
    
    ...
    
    [target."cfg(target_arch = \"wasm32\")".dev-dependencies.web-sys]
    features = [...]
    version = "=0.3.61"
    

    Then in your project's Cargo.toml, include this:

    [patch.crates-io]
    wgpu = { path = "../path/to/your/wgpu/clone" }
    

    You also need to update your web-sys version to =0.3.61.

    And that's it!