Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM cannot build for wasm32-wasi #4906

Open
schtauffen opened this issue Jun 3, 2022 · 8 comments
Open

WASM cannot build for wasm32-wasi #4906

schtauffen opened this issue Jun 3, 2022 · 8 comments
Labels
C-Bug An unexpected or incorrect behavior P-Compile-Failure A failure to compile Bevy apps

Comments

@schtauffen
Copy link

schtauffen commented Jun 3, 2022

Bevy version

0.7

What you did

Cargo.toml

<snip>

[target.'cfg(target_arch="wasm32")'.dependencies]
lunatic = "0.9"
bevy = { version = "0.7", default-features = false }

.cargo/config.toml

[target.wasm32-wasi]
runner = "lunatic"

ran cargo run --bin server --target wasm32-wasi

What went wrong

I expect bevy to compile for use inside wasm. Instead, it gives errors:

error[E0463]: can't find crate for `js_sys`
 --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:1:1
  |
1 | extern crate js_sys;
  | ^^^^^^^^^^^^^^^^^^^^ can't find crate

error[E0463]: can't find crate for `wasm_bindgen`
 --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:2:1
  |
2 | extern crate wasm_bindgen;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

error[E0463]: can't find crate for `web_sys`
 --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:3:1
  |
3 | extern crate web_sys;
  | ^^^^^^^^^^^^^^^^^^^^^ can't find crate

   Compiling image v0.23.14
error[E0433]: failed to resolve: use of undeclared type `AudioContextOptions`
   --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:218:31    
    |
218 |         let mut stream_opts = AudioContextOptions::new();
    |                               ^^^^^^^^^^^^^^^^^^^ use of undeclared type `AudioContextOptions`

error[E0433]: failed to resolve: use of undeclared type `AudioContext`
   --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:221:13    
    |
221 |             AudioContext::new_with_context_options(&stream_opts).map_err(
    |             ^^^^^^^^^^^^ use of undeclared type `AudioContext`

error[E0433]: failed to resolve: use of undeclared type `Closure`
   --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:269:26
    |
269 |                 .replace(Closure::wrap(Box::new(move || {
    |                          ^^^^^^^ use of undeclared type `Closure`

error[E0412]: cannot find type `AudioContext` in this scope
  --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:28:14      
   |
27 | pub struct Stream {
   |                  - help: you might be missing a type parameter: `<AudioContext>`
28 |     ctx: Arc<AudioContext>,
   |              ^^^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `Closure` in this scope
  --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:29:46      
   |
29 |     on_ended_closures: Vec<Arc<RwLock<Option<Closure<dyn FnMut()>>>>>,
   |                                              ^^^^^^^ not found in this scope

error[E0412]: cannot find type `Closure` in this scope
   --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:231:58    
    |
231 |         let mut on_ended_closures: Vec<Arc<RwLock<Option<Closure<dyn FnMut()>>>>> = Vec::new();
    |                                                          ^^^^^^^ not found in this scope

error[E0412]: cannot find type `Closure` in this scope
   --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:262:53    
    |
262 |             let on_ended_closure: Arc<RwLock<Option<Closure<dyn FnMut()>>>> =
    |                                                     ^^^^^^^ not found in this scope

error[E0425]: cannot find function `eval` in this scope
   --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:444:43    
    |
444 |     if let Ok(audio_context_is_defined) = eval("typeof AudioContext !== 'undefined'") {
    |                                           ^^^^ not found in this scope

error[E0599]: no method named `clone` found for struct `Arc<_>` in the current scope
   --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:241:34    
    |
241 |             let ctx_handle = ctx.clone();
    |                                  ^^^^^ method not found in `Arc<_>`

error[E0599]: no method named `create_buffer` found for struct `Arc<_>` in the current scope
   --> C:\Users\Zach and Andrea\.cargo\registry\src\github.com-1ecc6299db9ec823\cpal-0.13.5\src\host\webaudio\mod.rs:250:18    
    |
250 |                 .create_buffer(
    |                  ^^^^^^^^^^^^^ method not found in `Arc<_>`
Some errors have detailed explanations: E0412, E0425, E0433, E0463, E0599.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `cpal` due to 13 previous errors
warning: build failed, waiting for other jobs to finish...

Additional information

I would not expect javascript/browser (js-sys, web-sys) interop to be required to compile to wasm.

@schtauffen schtauffen added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Jun 3, 2022
@infmagic2047
Copy link
Contributor

Do you have bevy listed elsewhere in your Cargo.toml? Cargo features are additive, so all places where bevy is added as a dependency should have default-features = false. Audio is controlled by the bevy_audio feature, so it can be turned off.

However, I don't think wasm32-wasi will work right now, as our bevy_asset crate currently forces a web-based implementation for wasm32. You can try to get the filesystem-based implementation work on wasi (and a pull request for this is welcome). Alternatively, if you don't need asset loading, you can use bevy subcrates directly (bevy_ecs + bevy_app + whatever else you need).

@jakobhellermann
Copy link
Contributor

jakobhellermann commented Jun 3, 2022

Audio is indeed behind a feature flag, just one that that is enabled by default: https://github.com/bevyengine/bevy/blob/main/docs/cargo_features.md.
If you run cargo check --no-default-features --target wasm32-wasi it checks without errors.

EDIT: yeah what @infmagic2047 said

@jakobhellermann jakobhellermann added S-User-Error This issue was caused by a mistake in the user's approach P-Compile-Failure A failure to compile Bevy apps and removed S-Needs-Triage This issue needs to be labelled labels Jun 3, 2022
@schtauffen
Copy link
Author

schtauffen commented Jun 6, 2022

I looked at just pulling the pieces I need but wasm_bindgen is utilized throughout the code, such as:

#[cfg(not(target_arch = "wasm32"))]
{
while let Ok(delay) = tick(&mut app, wait) {
if let Some(delay) = delay {
std::thread::sleep(delay);
}
}
}
#[cfg(target_arch = "wasm32")]
{
fn set_timeout(f: &Closure<dyn FnMut()>, dur: Duration) {
web_sys::window()
.unwrap()
.set_timeout_with_callback_and_timeout_and_arguments_0(
f.as_ref().unchecked_ref(),
dur.as_millis() as i32,
)
.expect("Should register `setTimeout`.");
}
let asap = Duration::from_millis(1);
let mut rc = Rc::new(app);
let f = Rc::new(RefCell::new(None));
let g = f.clone();
let c = move || {
let mut app = Rc::get_mut(&mut rc).unwrap();
let delay = tick(&mut app, wait);
match delay {
Ok(delay) => {
set_timeout(f.borrow().as_ref().unwrap(), delay.unwrap_or(asap))
}
Err(_) => {}
}
};
*g.borrow_mut() = Some(Closure::wrap(Box::new(c) as Box<dyn FnMut()>));
set_timeout(g.borrow().as_ref().unwrap(), asap);
};
}

It seems the engine authors equate the wasm32 target with being run in a browser which does not fit my use case. I would like the code to stay lean and mean and not attempt to punt to javascript at all (the code in the not wasm32 branch would suffice for me). I will explore writing patches but worry it is going to be a huge undertaking. Probably less of a burden than writing my own engine, however :P

@expenses
Copy link

expenses commented Jun 6, 2022

You can't use wgpu with wasm32-wasi because it pretty heavily ties into wasm-bindgen (including js-sys and web-sys). See rustwasm/wasm-bindgen#2471.

@pedronasser
Copy link

pedronasser commented Sep 1, 2022

The problem here goes way further than just audio, if I am not mistaken. Like @schtauffen mentioned bevy currently uses wasm-bindgen even on non graphical/audio related functionalities. So for example even if try to build just a bevy App() without any default-features or the default plugins, it will try to build with wasm-bindgen which is currently breaking on wasm32-wasi targets.

@schtauffen
Copy link
Author

To recap: the initial errors were user error but the ticket itself is valid and is related to the use of wasm-bindgen as mentioned by @pedronasser . I initially forked and began working towards a wasm32-wasi friendly version with 0.7 but wasn't successful. I plan to attempt again from scratch with 0.8;

@schtauffen
Copy link
Author

I cannot seem to remove labels, can S-User-Error be removed? @jakobhellermann

@jakobhellermann jakobhellermann removed the S-User-Error This issue was caused by a mistake in the user's approach label Sep 25, 2022
@BrandonDyer64
Copy link
Contributor

This is also a huge issue for my work on the bevy_wasm crate. Bevy generates a huge amount of unnecessary wasm-bindgen calls that I have had to manually stub out. I really think the wasm-bindgen functionality should be gated behind a web or browser feature flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Bug An unexpected or incorrect behavior P-Compile-Failure A failure to compile Bevy apps
Projects
None yet
Development

No branches or pull requests

6 participants