In Rust 1.99, which wasm32 targets suit a browser image filter and a WASI command-line tool, and why does `--target wasm32-wasi` fail?
answer
- no host imports vs syscall imports
- core module vs component
- a rename finished in 1.84
- files only inside preopened directories
basics
~20 sBrowsers use wasm32-unknown-unknown, which assumes no host APIs. WASI tools use wasm32-wasip1, a core module calling WASI preview 1, or wasm32-wasip2, a component (tier 2 since 1.82). The old wasm32-wasi name was removed in Rust 1.84.
solid answer
~40 sThe browser build targets `wasm32-unknown-unknown`: its std imports nothing from the host, std compiles but OS-backed parts do nothing or fail (`println!` prints nothing, `std::fs` returns errors), and all host access goes through JavaScript imports such as those wasm-bindgen generates. The command-line build targets WASI. `wasm32-wasip1` emits a core module importing `wasi_snapshot_preview1` functions for arguments, files and stdout; `wasm32-wasip2` emits a component on WASI 0.2 and the Component Model, so the runtime must support components. Either way the program can open files only under directories the host preopens. `--target wasm32-wasi` fails because the target was renamed: `wasm32-wasip1` arrived in 1.78, 1.81 warned on the old name, and 1.84 removed it, so rustc reports it cannot find the target specification and suggests `wasm32-wasip1`.
code
rust · 27 linesuse std::{env, fs, process::ExitCode};
fn grayscale(pixels: &mut [u8]) {
for px in pixels.chunks_exact_mut(4) {
let y = ((px[0] as u32 * 77 + px[1] as u32 * 150 + px[2] as u32 * 29) >> 8) as u8;
px[..3].fill(y);
}
}
fn main() -> ExitCode {
let args: Vec<String> = env::args().collect();
let [_, input, output] = args.as_slice() else {
eprintln!("usage: grey <input.rgba> <output.rgba>");
return ExitCode::FAILURE;
};
let mut pixels = match fs::read(input) {
Ok(bytes) => bytes,
Err(e) => {
eprintln!("cannot read {input}: {e}");
return ExitCode::FAILURE;
}
};
grayscale(&mut pixels);
fs::write(output, &pixels).expect("write output");
println!("wrote {} pixels to {output}", pixels.len() / 4);
ExitCode::SUCCESS
}go deeper
Recall the pairing: wasm32-unknown-unknown for the browser, a WASI target for a command-line program. Know that wasm32-wasi is the old name of wasm32-wasip1.
Explain what each target gives std: nothing on the browser target, WASI preview 1 imports on wasip1, a component on wasip2. Date the rename: 1.78 added the new name, 1.84 removed the old one.
Show how the targets bite in practice: a CI script still on wasm32-wasi, a component a runtime cannot load, a tool failing because the host preopened no directory, and wasm-bindgen exports missing from a WASI build.
Weigh where the WASI build should sit for a team: preview 1 runs in more embedders today, the component target is where WASI standardisation is going, and the choice fixes which runtimes you support.
## Three targets, three kinds of host A **target triple** names what rustc compiles for. Rust 1.99 lists nine wasm targets, but an image filter that runs in a browser and as a command-line tool needs only three of them. How a triple is configured in Cargo, with linkers and rustflags, is Cargo's subject; here the question is what each target lets the code do. | Target | Tier (1.99) | Output | Host it expects | |---|---|---|---| | `wasm32-unknown-unknown` | 2 | core module | anything; imports nothing for std | | `wasm32-wasip1` | 2 | core module | a WASI preview 1 runtime | | `wasm32-wasip2` | 2 (since 1.82) | component | a runtime that runs WASI 0.2 components | ## wasm32-unknown-unknown: std with nothing behind it This is the **browser** target and the one wasm-bindgen uses. The platform documentation calls it the minimal WebAssembly target: std does not import any host function. `core` and `alloc` work fully, and so do `Vec`, `String` and `HashMap` (unrandomised). Anything that needs an operating system is a stub: - `println!` does nothing; - `std::fs` always returns errors; - `std::thread::spawn` panics; - there is no way to override these from inside std. The page reaches the outside world through JavaScript imports instead: wasm-bindgen for functions, js-sys and web-sys for JavaScript built-ins and browser APIs. ## wasm32-wasip1: a core module with syscall imports For the **command-line tool**, WASI gives std a set of system calls. A `wasm32-wasip1` build is a core module whose imports come from `wasi_snapshot_preview1`; the grayscale tool imports `args_get`, `path_open`, `fd_read`, `fd_write` and `proc_exit`, among others. `std::env::args`, `std::fs::read` and `println!` all work because std calls those functions. WASI is **capability-based**: a path opens only inside a directory the host **preopened** for the module. Run under Node's `node:wasi` with a host directory mapped to `/sandbox`, the tool reads and writes there; with no preopen, the same `fs::read` fails with "No such file or directory (os error 44)". Threads are still unavailable (spawning returns an error); `wasm32-wasip1-threads` is a separate target for that. ## wasm32-wasip2: a component WASI 0.2 (released January 2024) rebased WASI on the **Component Model**. A `wasm32-wasip2` build is a **component**, not a core module: its header carries a different version field, so an engine that only understands core modules rejects it (Node's `WebAssembly.Module` reports "expected version 01 00 00 00, found 0d 00 01 00"). The runtime must run components; the platform page names Wasmtime 17 and later. `wasm32-wasip1` remains for compatibility with preview 1 runtimes, whose API no longer changes. ## The rename, release by release 1. **Rust 1.78** added `wasm32-wasip1` (tier 2) and `wasm32-wasip2` (tier 3). 2. **Rust 1.81** made every use of `wasm32-wasi` warn and ask for `wasm32-wasip1`; the two were the same target. 3. **Rust 1.82** promoted `wasm32-wasip2` to tier 2. 4. **Rust 1.84** removed `wasm32-wasi`. Since then rustc stops with "error loading target specification: could not find specification for target "wasm32-wasi"" and suggests `wasm32-wasip1`. Old CI scripts and blog posts still say `wasm32-wasi`; on a current toolchain they fail at the first `cargo build`. ## Telling the builds apart A build's imports reveal its target at a glance, which helps when a CI artifact is in doubt: - a `wasm32-unknown-unknown` module built from std alone imports **nothing**; with wasm-bindgen it imports from `__wbindgen_placeholder__` until the CLI rewrites it; - a `wasm32-wasip1` module imports from **`wasi_snapshot_preview1`**; - a `wasm32-wasip2` file is a **component**, recognisable by its header before any import is read. Node's `WebAssembly.Module.imports` lists a core module's imports; any WebAssembly inspection tool shows the same. Running the wrong file in the wrong host fails fast: a WASI module in a page has no `wasi_snapshot_preview1` to link, and a component in a core-module engine fails to compile at all. ## One crate, two builds Keep the filter itself target-neutral, a function over `&mut [u8]`, and wrap it twice. Code can tell the targets apart with `#[cfg]` (`target_os = "unknown"` for the browser target, `target_os = "wasi"` with `target_env = "p1"` or `"p2"` for WASI); the mechanics of `cfg` belong to conditional compilation. Two observations from building one wasm-bindgen crate for both: - for `wasm32-wasip1`, wasm-bindgen 0.2.129 exported none of the `#[wasm_bindgen]` functions, because its generated exports are compiled only for non-WASI wasm targets; - so the browser bindings live in the `cdylib`, and the WASI tool is a binary with an ordinary `fn main`.
- What changes when the WASI tool moves from `wasm32-wasip1` to `wasm32-wasip2`?The output becomes a component instead of a core module, built on WASI 0.2 and the Component Model, so the runtime must support components; a runtime that only loads core modules rejects the file. The platform documentation describes `wasm32-wasip1` as kept for compatibility with the preview 1 syscalls, which receive no new APIs, while current WASI standardisation targets the component-based version.
- Why does the tool's `fs::read("/sandbox/in.rgba")` fail with "No such file or directory" under a runtime that was given no directory?WASI grants file access per directory: the host preopens directories and the module can resolve paths only beneath them. With nothing preopened there is no directory that `/sandbox` could resolve against, so std's `fs::read` gets an error from the WASI call and the tool reports it. The fix is on the host side: preopen the directory when starting the runtime, or pass it in the `preopens` option when embedding, as Node's `node:wasi` accepts.
- Can the browser build and the WASI build share one crate?Yes, if the filter logic is target-neutral. Built for `wasm32-wasip1`, wasm-bindgen 0.2.129 exported none of the `#[wasm_bindgen]` functions, so the browser bindings belong in the `cdylib` built for `wasm32-unknown-unknown` and the WASI tool in a binary target with `fn main`. Target-specific code is selected with `cfg` on `target_os` and `target_env`.
- Browser builds should target `wasm32-wasip1`, since WASI is how WebAssembly talks to browsers.
- `wasm32-wasi` still builds on current stable, only with a deprecation warning.
- `wasm32-wasip2` produces the same kind of core module as `wasm32-wasip1`, with newer imports.
- A WASI program can open any host file by its absolute path.
- On `wasm32-unknown-unknown`, `println!` writes to the browser console.
Assumes Rust 1.99: wasm32-wasip1 was added in 1.78, wasm32-wasip2 became tier 2 in 1.82, and the wasm32-wasi name was removed in 1.84.