mvlabat / bevy_egui

This crate provides an Egui integration for the Bevy game engine. 🇺🇦 Please support the Ukrainian army: https://savelife.in.ua/en/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Compile errors when building to WASM target

HyperCodec opened this issue · comments

I'm not sure how this is happening or how this is reproducible, but I have the following in my Cargo.toml:

[features]
default = []
debug = ["dep:bevy-inspector-egui", "bevy_rapier3d/debug-render"]
editor = ["dep:bevy_editor_pls"]

[profile.dev]
opt-level = 1

[profile.dev.package."*"]
opt-level = 3

[dependencies]
bevy = "0.13.0"
bevy-inspector-egui = { version = "0.23.3", optional = true }
bevy_editor_pls = { version = "0.8.1", optional = true }
bevy_egui = "0.26.0"
bevy_fps_controller = "0.2.5"
bevy_hanabi = "0.10.0"
bevy_rapier3d = "0.25.0"

[[bin]]
name = "editor"
required-features = ["editor"]

Compiling with cargo build --target wasm32-unknown-unknown produces the following errors:

error[E0412]: cannot find type `ClipboardEvent` in crate `web_sys`
   --> /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_egui-0.26.0/src/web_clipboard.rs:105:41
    |
105 |     closure: Closure<dyn FnMut(web_sys::ClipboardEvent)>,
    |                                         ^^^^^^^^^^^^^^ help: a struct with a similar name exists: `KeyboardEvent`
    |
   ::: /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/web-sys-0.3.67/src/features/gen_KeyboardEvent.rs:5:1
    |
5   | #[wasm_bindgen]
    | --------------- similarly named struct `KeyboardEvent` defined here

error[E0412]: cannot find type `ClipboardEvent` in crate `web_sys`
   --> /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_egui-0.26.0/src/web_clipboard.rs:118:71
    |
118 |     let closure = Closure::<dyn FnMut(_)>::new(move |_event: web_sys::ClipboardEvent| {
    |                                                                       ^^^^^^^^^^^^^^ help: a struct with a similar name exists: `KeyboardEvent`
    |
   ::: /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/web-sys-0.3.67/src/features/gen_KeyboardEvent.rs:5:1
    |
5   | #[wasm_bindgen]
    | --------------- similarly named struct `KeyboardEvent` defined here

error[E0412]: cannot find type `ClipboardEvent` in crate `web_sys`
   --> /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_egui-0.26.0/src/web_clipboard.rs:152:71
    |
152 |     let closure = Closure::<dyn FnMut(_)>::new(move |_event: web_sys::ClipboardEvent| {
    |                                                                       ^^^^^^^^^^^^^^ help: a struct with a similar name exists: `KeyboardEvent`
    |
   ::: /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/web-sys-0.3.67/src/features/gen_KeyboardEvent.rs:5:1
    |
5   | #[wasm_bindgen]
    | --------------- similarly named struct `KeyboardEvent` defined here

error[E0412]: cannot find type `ClipboardEvent` in crate `web_sys`
   --> /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_egui-0.26.0/src/web_clipboard.rs:186:70
    |
186 |     let closure = Closure::<dyn FnMut(_)>::new(move |event: web_sys::ClipboardEvent| {
    |                                                                      ^^^^^^^^^^^^^^ help: a struct with a similar name exists: `KeyboardEvent`
    |
   ::: /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/web-sys-0.3.67/src/features/gen_KeyboardEvent.rs:5:1
    |
5   | #[wasm_bindgen]
    | --------------- similarly named struct `KeyboardEvent` defined here

error[E0599]: no method named `clipboard` found for struct `Navigator` in the current scope
   --> /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_egui-0.26.0/src/web_clipboard.rs:233:35
    |
233 |         let Some(clipboard) = nav.clipboard() else {
    |                                   ^^^^^^^^^ method not found in `Navigator`

Some errors have detailed explanations: E0412, E0599.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `bevy_egui` (lib) due to 5 previous errors
warning: build failed, waiting for other jobs to finish...

This is happening in a GitHub Codespace on Rust 1.76.0

Are you patching some dependency ? Are you sure your cargo cache is unmodified ?

This error you're showing looks like web-sys is missing a feature, I can "reproduce" the same error message by commenting out the ClipboardEvent feature from web-sys in bevy_egui.

commented web-sys feature

maybe a cargo tree -i web-sys could help figuring thing out 🤔

Are you patching some dependency ? Are you sure your cargo cache is unmodified ?

This error you're showing looks like web-sys is missing a feature, I can "reproduce" the same error message by commenting out the ClipboardEvent feature from web-sys in bevy_egui.

commented web-sys feature

maybe a cargo tree -i web-sys could help figuring thing out 🤔

I do not have any patches, what I provided is the entire Cargo.toml (excluding the top part with package name, author, etc). I ran after a cargo clean and it continues to have the same error. I'll try the tree command whenever I'm able to get back.

The crate now requires RUSTFLAGS=--cfg=web_sys_unstable_apis when compiling for WASM with clipboard support. I'll add this to the readme.

Alternatively, you can also add these two lines to your project's .cargo/config.toml:

[target.wasm32-unknown-unknown]
rustflags = ["--cfg=web_sys_unstable_apis"]

The crate now requires RUSTFLAGS=--cfg=web_sys_unstable_apis when compiling for WASM with clipboard support. I'll add this to the readme.

Alternatively, you can also add these two lines to your project's .cargo/config.toml:

[target.wasm32-unknown-unknown]
rustflags = ["--cfg=web_sys_unstable_apis"]

This compiles, but now I get a bunch of improperly formatted WASM binary that errors when I try to bindgen:

target/wasm32-unknown-unknown/debug/creatept.wasm: 1: }}: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: : not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: ~: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: target/wasm32-unknown-unknown/debug/creatept.wasm: 1: : not found
}}}}}}: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: : not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: ~~: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 2: }: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: : not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: ~: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: }}: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: : not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: : not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: }: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 2:}}
                                                       }}: not found
~~~: not foundunknown-unknown/debug/creatept.wasm: 1: 
target/wasm32-unknown-unknown/debug/creatept.wasm: 2: }}}}: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: }: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1:: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: }: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: asm��~}}: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 2: ~~: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 4: : not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1:~: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 5: }}}}}}: not found
target/wasm32-unknown-unknown/debug/creatept.wasm: 1: Syntax error: end of file unexpected

I am unsure whether this is an issue with egui or just some weird bug with the compiler.

Could you try compiling bevy_egui without the unstable apis flag and with the following set of features? This will probably help to isolate whether it's a clipboard-related issue.

bevy_egui = { version = "0.26", default-features = false, features = ["open_url", "default_fonts", "render"] }

I tried those features, they didn't appear to change anything. I updated wasm-bindgen and the error changed to this:

error: 

it looks like the Rust project used to create this wasm file was linked against
version of wasm-bindgen that uses a different bindgen format than this binary:

  rust wasm file schema version: 0.2.91
     this binary schema version: 0.2.92

Currently the bindgen format is unstable enough that these two schema versions
must exactly match. You can accomplish this by either updating this binary or 
the wasm-bindgen dependency in the Rust project.

You should be able to update the wasm-bindgen dependency with:

    cargo update -p wasm-bindgen --precise 0.2.92

don't forget to recompile your wasm file! Alternatively, you can update the 
binary with:

    cargo install -f wasm-bindgen-cli --version 0.2.91

if this warning fails to go away though and you're not sure what to do feel free
to open an issue at https://github.com/rustwasm/wasm-bindgen/issues!

even after making the changes suggested in the error and recompiling, it still gives me the same error.

Which one of the suggested commands have you run? I'd expect this to be fixed if you run either of those (but not both). If it doesn't help, maybe check that you don't have multiple versions of wasm-bindgen in your tree.

Which one of the suggested commands have you run? I'd expect this to be fixed if you run either of those (but not both). If it doesn't help, maybe check that you don't have multiple versions of wasm-bindgen in your tree.

I updated the version in Cargo.lock for wasm-bindgen, now it starts to load and then panics on some random wgpu pipeline in hanabi

Opened #270 to help users learn and fix it :)

The compilation errors are more user friendly now. Thanks everyone