Add a Rust Crate Dependency
Important constraints
- The compilation target is
wasm32-wasip2— only crates that support this target will work. - Crates requiring unsupported OS facilities may fail to compile or fail at runtime.
- A generic
wasm32-wasicompatibility claim is insufficient; verify the exact features used againstwasm32-wasip2and exercise them in Golem. - Pure Rust is not by itself proof of compatibility. Platform-specific C libraries, threading, sockets, and memory-mapping APIs require particular scrutiny.
Steps
-
Add the dependency to
Cargo.tomlIn the component’s
Cargo.toml(not a workspaceCargo.toml), add the crate under[dependencies]:[dependencies] my-crate = "1.0"If the project has a Cargo workspace with
[workspace.dependencies], add the version there and reference it withmy-crate = { workspace = true }in the component crate. -
Build to verify
golem build --yesDo NOT run
cargo builddirectly — always usegolem build. -
If the build fails
- Check the error for unsupported target features or missing native dependencies.
- Try enabling a
wasmorwasifeature flag if the crate provides one. - Look for an alternative crate that supports WASM.
Already available crates
These crates are already in the project’s Cargo.toml — do NOT add them again:
golem-rust— Golem agent framework, durability, transactionswstd— WASI standard library (HTTP client, async I/O)log— loggingserde/serde_json— serialization
HTTP and networking
Prefer wstd::http for outgoing HTTP requests. Compile support for an API does not guarantee that
the runtime grants the required network capability, so test the actual operation in Golem.
AI / LLM features
To add AI capabilities, See the golem-add-llm-rust guide. The published golem-ai release and the current WASIp2-compatible source do not currently use the same API, so do not guess a crate version or copy an older example.