mvlabat / bevy_egui

This crate provides an Egui integration for the Bevy game engine. 🇺🇦 Please support the Ukrainian army: https://savelife.in.ua/en/
MIT License
896 stars 241 forks source link

Compile errors when building to WASM target #269

Closed HyperCodec closed 4 months ago

HyperCodec commented 5 months ago

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

Vrixyz commented 5 months ago

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 🤔

HyperCodec commented 5 months ago

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.

mvlabat commented 5 months ago

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: https://github.com/mvlabat/bevy_egui/blob/18a53882945fa87263566a570da2468314c45ccc/.cargo/config.toml#L5-L6

HyperCodec commented 5 months ago

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:

https://github.com/mvlabat/bevy_egui/blob/18a53882945fa87263566a570da2468314c45ccc/.cargo/config.toml#L5-L6

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.

mvlabat commented 5 months ago

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"] }

HyperCodec commented 5 months ago

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.

mvlabat commented 5 months ago

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.

HyperCodec commented 5 months ago

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

Vrixyz commented 5 months ago

Opened https://github.com/mvlabat/bevy_egui/pull/270 to help users learn and fix it :)

mvlabat commented 4 months ago

The compilation errors are more user friendly now. Thanks everyone