devashishdxt / tonic-web-wasm-client

Other
104 stars 28 forks source link

Question: how to compile proto without tonic's transport #2

Closed rcarcasses closed 2 years ago

rcarcasses commented 2 years ago

Hi, thanks for sharing your hard work. I'm trying to compile/generate tonic's code with the transport feature disabled but no success so far. Concretely I have modified my Cargo.toml to conditionally compile the protos according to the target:

...
[build-dependencies]
prost-build = "0.10.4"

[target.'cfg(not(target_arch = "wasm32"))'.build-dependencies]
tonic-build = "0.7"

[target.'cfg(target_arch = "wasm32")'.build-dependencies]
tonic-build = { version = "0.7", default-features = false, features = ["prost"]}

[dependencies]
prost = { version = "0.10" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
tonic = "0.7"

[target.'cfg(target_arch = "wasm32")'.dependencies]
tonic = { version = "0.7.2", default-features = false, features = ["codegen", "prost"]}

With this cargo build runs (I have a build.rs with some little configuration as expected), but when I try to target wasm with cargo build --lib --target wasm32-unknown-unknown it fails miserably.

Can you provide some hint please? Appreciated!

devashishdxt commented 2 years ago

Dependencies look fine. Can you share your github repository?

devashishdxt commented 2 years ago

Or if you want a real world working example of tonic-web-wasm-client, you can refer to this: https://github.com/devashishdxt/stag.

rcarcasses commented 2 years ago

Hey @devashishdxt thanks for your quick reply, appreciated! Let me study your https://github.com/devashishdxt/stag repo first, I'll come back after.

rcarcasses commented 2 years ago

Hi @devashishdxt, thanks again for your support. After inspecting your shared repo I found what was causing the problem on my side: basically the line:

[target.'cfg(not(target_arch = "wasm32"))'.build-dependencies]
tonic-build = "0.7"

caused the issue. The thing is, at build time, even if we are targeting wasm, target_arch wasn't equal to wasm32 but to my local arch in fact. The solution is to turn on/off the needed features as it is done in your shared repo.