Skip to content

Unexpected behavior when following the guide to create a component that requires wasi:http with Rust #305

@ThorstenHans

Description

@ThorstenHans

I was following this guide available at https://component-model.bytecodealliance.org/language-support/rust.html to build a simple component in Rust.

My intention was to export an interface providing a single operation and to import wasi:[email protected]. I updated tooling according to the introduction section of bespoke guide.

Environment

Component Version
Host OS darwin 24.6.0
Rust 1.86.0
Cargo Component 0.21.1
wasm-tools 1.236.1
wkg 0.12.0

What I did (steps to reproduce)

I followed the instructions in the guide:

# Create a new component
cargo component new foo --lib && cd foo

I added my WIT like:

package lorem:[email protected];

interface foo {
    foo: func(a: u32, b: u32) -> result<u32>;
}

world fooer {
    export foo;
    import wasi:http/[email protected];
    import wasi:http/[email protected];
}

To my surprise, the documentation mentioned this:

This section is about importing custom WIT interfaces from library components. By default, cargo-component imports any required WASI interfaces for us without needing to explicitly declare them.

(See this deep-link: https://component-model.bytecodealliance.org/language-support/rust.html#importing-an-interface)

So, naive me quickly updates the package reference in Cargo.toml to lorem:fooer and tries a cargo component build which ends up presenting this error:

error: failed to create a target world for package `foo` (/Users/thorsten/dev/foo/Cargo.toml)

Caused by:
    0: failed to merge local target `/Users/thorsten/dev/foo/wit`
    1: package 'wasi:[email protected]' not found. known packages:
           lorem:[email protected]

            --> /Users/thorsten/dev/foo/wit/world.wit:9:12
             |
           9 |     import wasi:http/[email protected];
             |            ^--------

Looking through the issues of the component-docs repo, I discovered this issue comment referencing wkg wit fetch which pulled all necessary dependencies:

tree wit                                                                                                                                                
wit
├── deps
│   ├── wasi-cli-0.2.3
│   │   └── package.wit
│   ├── wasi-clocks-0.2.3
│   │   └── package.wit
│   ├── wasi-http-0.2.3
│   │   └── package.wit
│   ├── wasi-io-0.2.3
│   │   └── package.wit
│   └── wasi-random-0.2.3
│       └── package.wit
└── world.wit

Running another cargo component build still showed the same error message.

So, I manually added the dependencies in Cargo.toml and tried cargo component build again. However, the error remained the same.

IMO, this is way to hard, especially because the docs mention that it should just work ™️

Is there anything I did wrong on my end?

I totally agree with the statement in the docs, building a new component that relies on WASI APIs should just work and not require any "tinkering"

How I ended up generating a valid WIT world

What worked for me was using wams-tools component wit to generate a working, holistic new WIT world.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions