Closed ufoscout closed 2 years ago
You have already found the correct answer. We can't really know if you want to build for JS wasm or not the way feature flags are currently set up.
The correct(?) future answer for this would be a proper wasm32-emscripten-unknown
target, I believe.
Hi @Fishrock123
I don't know exactly what you mean by "JS wasm", I simply tried to build to a cargo wasm target with the surf wasm-client
feature enabled.
Is there another way to build to wasm that I am missing?
Please refer to the docs sections from getrandom which is linked in the error message, since it explains this: https://docs.rs/getrandom/0.2.3/getrandom/#webassembly-support
tl;dr: Yes there are multiple possible wasm targets, and wasm32-unknown-unknown
is (as per it's name) ambiguous.
Hi,
I'm facing the same issue as @ufoscout.
When I explicitly add the js
feature for getrandom
I'm getting the following error:
error: cyclic package dependency: package `ahash v0.7.4` depends on itself. Cycle:
package `ahash v0.7.4`
... which is depended on by `hashbrown v0.11.2`
... which is depended on by `indexmap v1.7.0`
... which is depended on by `serde_json v1.0.64`
... which is depended on by `wasm-bindgen v0.2.74`
... which is depended on by `js-sys v0.3.51`
... which is depended on by `getrandom v0.2.3`
... which is depended on by `ahash v0.7.4`
I'm pretty new to Rust. This might seem obvious but do you know how to troubleshoot and resolve this?
The documentation mentions this:
wasm-client: use window.fetch as the HTTP backend.
When I read this, I'm assuming that surf
supports wasm32-unknown-unknown
out-of-the-box as it seems to be a pretty popular target in other libraries that advertise WASM support in JavaScript runtimes. I didn't expect having to dive into the documentation of a transitive dependency to make it work.
One can expect an increase of isomorphic Rust usages in the future. Being able to easily use surf
both server-side and client-side would be greatly beneficial for the library. I believe having a clear, frictionless, path to use the wasm-client
with wasm32-unknown-unknown
would definitely help with that.
I suppose that the
js
feature ofgetrandom
should be enabled by default if thewasm-client
is used
@Fishrock123 I agree with this proposition. My questions are:
surf
to do this by default?wasm32-unknown-unknown
out-of-the-box?Thank you!
If you care about this please tell your wasm packing tool to switch to wasm32-unknown-emscripten
.
Surf could introduce a wasm-web-client
feature or something to cover this up but it really is not this library’s problem, IMO.
(By “could” I mean I’ll accept a PR for something like that if someone makes one.)
If you care about this please tell your wasm packing tool to switch to
wasm32-unknown-emscripten
.Surf could introduce a
wasm-web-client
feature or something to cover this up but it really is not this library’s problem, IMO.
Sorry - but isn't that what the feature "wasm-client" should mean? Shouldn't the proper form of get-random be selected if compiling for WASM? It's super confusing to have a "wasm-client" feature that doesn't compile on wasm... I think this feature is broken.
Edit, yeah, it should work, there's a crate called "wasm-test" in the workspace.
@ufoscout
the secret to getting it to work is to pin getrandom to the "js" feature
[dependencies.getrandom]
version = "0.2"
features = ["js"]
This is a thing that can be done within surf, so I'll make a pr to fix this.
I was not involved with the development of the WASM feature within Surf and I don't know the full original intentions.
Non-web wasm runtimes do exist, particularly ones based on WASI, but there could potentially be others as well.
(Perhaps someone could confirm if it it is even possible to compile Surf to WASI at all. If not, then we might as well just support web-only WASM for now.)
(Perhaps someone could confirm if it it is even possible to compile Surf to WASI at all. If not, then we might as well just support web-only WASM for now.)
I believe when compiling for WASI you target wasm32-wasi
. We could either target-select on wasi, or enable "wasi" as a feature in surf. WASI essentially uses the same sys-calls as regular code, so I think it might work fine without selecting the wasm-client.
I'm not sure what WASI people use for making web requests today.
I just tried compiling surf for wasi and the socket2 dependency fails because it doesn't support wasi as a target. Today at least, surf is not supported on WASI.
I believe this was fixed in #316 and should be released in a 2.3.0 soonish
I created a new empty cargo project with this
Cargo.toml
file:Trying to compile it with target
wasm32-unknown-unknown
fails with:The problem is fixed by adding this to the
Cargo.toml
file:I suppose that the
js
feature ofgetrandom
should be enabled by default if thewasm-client
is used