second-state / wasm-learning

Building Rust functions for Node.js to take advantage of Rust's performance, WebAssembly's security and portability, and JavaScript's ease-of-use. Demo code and recipes.
https://www.secondstate.io/articles/getting-started-with-rust-function/
485 stars 102 forks source link

Add `rustup target add wasm32-wasi` into the `wasm-learning/cli/hello/` example #38

Closed TieWay59 closed 2 years ago

TieWay59 commented 2 years ago

The hello file does not mention rustup target add wasm32-wasi before building as a beginner example. I believe it is better for newcomers.

$ rustup target add wasm32-wasi
$ cargo build --target wasm32-wasi

https://github.com/second-state/wasm-learning/blob/191de35732458d017f3c26b31f9df02aba822c45/cli/hello/README.md?plain=1#L11-L16

The error log will be very confusing if someone forgets to add the target before building for the first time.

root@TW59:~/wasm-learning/cli/hello# cargo build --target wasm32-wasi
   Compiling hello v0.1.0 (/root/wasm-learning/cli/hello)
error[E0463]: can't find crate for `std`
  |
  = note: the `wasm32-wasi` target may not be installed
  = help: consider downloading the target with `rustup target add wasm32-wasi`

error[E0463]: can't find crate for `std`
 --> src/main.rs:1:5
  |
1 | use std::env;
  |     ^^^ can't find crate
  |
  = note: the `wasm32-wasi` target may not be installed
  = help: consider downloading the target with `rustup target add wasm32-wasi`

error: cannot find macro `println` in this scope
 --> src/main.rs:6:5
  |
6 |     println!("{}", argument);
  |     ^^^^^^^

error: cannot find macro `println` in this scope
 --> src/main.rs:4:3
  |
4 |   println!("hello");
  |   ^^^^^^^

error[E0433]: failed to resolve: use of undeclared crate or module `env`
 --> src/main.rs:5:19
  |
5 |   for argument in env::args().skip(1) {
  |                   ^^^ use of undeclared crate or module `env`

error: requires `sized` lang_item

Some errors have detailed explanations: E0433, E0463.
For more information about an error, try `rustc --explain E0433`.
error: could not compile `hello` due to 6 previous errors

Reference:

WASI-tutorial.md this file gives an shell example with add target.

juntao commented 2 years ago

Can you send a PR to fix this? :) Thanks!

TieWay59 commented 2 years ago

Can you send a PR to fix this? :) Thanks!

NP. I opened this issue to track the discussion, I'll close it once it's merged.