cargo-component icon indicating copy to clipboard operation
cargo-component copied to clipboard

Build Failure with Inline WIT with `wit-bindgen`

Open tqwewe opened this issue 1 year ago • 6 comments

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

  1. Build for wasm32-wasi target:
    cargo build --package guest --target wasm32-wasi
    
  2. 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
    
  3. Run the host package and observe successful Hello, World! message:
    cargo run --package host
    

Build with Cargo Component

  1. Clean previous builds:
    cargo clean
    
  2. Build with cargo component:
    cargo component build --package guest
    
  3. 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.

tqwewe avatar Nov 09 '23 06:11 tqwewe