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

Don't try to compile examples if they have #![ cfg(not( target_arch = "wasm32" )) ] #666

Open
najamelan opened this issue Jun 18, 2019 · 2 comments

Comments

@najamelan
Copy link

najamelan commented Jun 18, 2019

🐛 Bug description

When supporting both wasm and other targets in a crate, I found it not obvious how to deal with tests and examples. It turned out that adding #![ cfg(not( target_arch = "wasm32" )) ] to files for integration tests that aren't wasm keeps wasm-pack test from trying to run them.

However, when you have an example in the examples folder that isn't wasm, wasm-pack test will still try to compile it, and even when putting a target based gate at the top. The result is that #![ cfg(not( target_arch = "wasm32" )) ] on top of the file will break tests because:

error[E0601]: `main` function not found in crate `localpool`

I haven't found a reasonable workaround...

🤔 Expected Behavior

Have a convenient way of supporting both wasm and non wasm targets in the same crate.
Document how to deal with tests and examples in this regard (in the book)

👟 Steps to reproduce

Create an example in examples folder that isn't meant for wasm target. Run unit tests with wasm-pack test

🌍 Your environment

linux
wasm-pack version: wasm-pack 0.7.0
rustc version: rustc 1.37.0-nightly (02564de47 2019-06-10)

@najamelan
Copy link
Author

Ok, the workaround I found is to put a gate on every single command that is toplevel and to add a dummy main at the bottom:

#[ cfg( target_arch = "wasm32" ) ]
//
fn main(){}

It's pretty ugly, especially for examples which are a form of documentation.

@najamelan
Copy link
Author

I have since filed an issue with cargo, as I think cargo test should not consider these examples on the wrong target. rust-lang/cargo#7233

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants