cargo-component
cargo-component copied to clipboard
Build Failure with Inline WIT with `wit-bindgen`
Description
There's an inconsistency when building a project with inline WIT definitions using cargo component build
. It fails under cargo component
but succeeds when manually building with cargo
and wasm-tools
with wit-bindgen
given the following lib.rs
:
wit_bindgen::generate!({
inline: r#"
package component:guest;
world example {
export example: interface {
hello-world: func() -> string;
}
}
"#,
exports: {
"example": Component
}
});
use exports::example::Guest;
struct Component;
impl Guest for Component {
fn hello_world() -> String {
"Hello, World!".to_string()
}
}
Reproduction Steps
Manual Build with Cargo and wasm-tools
- Build for
wasm32-wasi
target:cargo build --package guest --target wasm32-wasi
- Create the component with wasm-tools:
wasm-tools component new ./target/wasm32-wasi/debug/guest.wasm -o ./target/wasm32-wasi/debug/guest.wasm --adapt ./wasi_snapshot_preview1.wasm
- Run the host package and observe successful
Hello, World!
message:cargo run --package host
Build with Cargo Component
- Clean previous builds:
cargo clean
- Build with
cargo component
:cargo component build --package guest
- Observe failure when running the host package:
cargo run --package host
Expected Behavior
Both build methods should successfully compile and run the project, displaying Hello, World!
regardless if we use a wit
directory with cargo_component_bindings
or wit-bindgen
.
Actual Behavior
The build succeeds with manual steps but fails with cargo component
.
Additional Information
Repository Link: GitHub Repository
Is this intended behaviour?
I think it would be ideal if we can just use cargo component with wit-bindgen as an easy way of building wasm components, though it seems like we need to stick to the structure of having a wit
directory along side our src
directory.