Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo-component cannot find exported function in a world #360

Open
ifsheldon opened this issue Dec 8, 2024 · 5 comments
Open

cargo-component cannot find exported function in a world #360

ifsheldon opened this issue Dec 8, 2024 · 5 comments

Comments

@ifsheldon
Copy link

I am following https://component-model.bytecodealliance.org/language-support/rust.html#importing-an-interface-into-a-command-component to create a command component with cargo-component.

My adder WIT was:

package component:adder;

world adder {
    export add: func(a: s32, b: s32) -> s32;
}

And my WIT for the host is

 package component:host;

 world host {
    import component:adder/add;
 }

I also added this to Cargo.toml

[package.metadata.component.target.dependencies]
"component:adder" = { path = "../guest-adder/wit" }

But when I ran cargo component build, I got

error: failed to create a target world for package `host-command-component` (/Users/zhiqiu/offline_code/opensource/wasi_mindmap/host-command-component/Cargo.toml)

Caused by:
    0: failed to merge local target `/Users/zhiqiu/offline_code/opensource/wasi_mindmap/host-command-component/wit`
    1: interface not found in package
            --> /Users/zhiqiu/offline_code/opensource/wasi_mindmap/host-command-component/wit/host.wit:4:28
             |
           4 |     import component:adder/add;
             |                            ^--

This took me a while to debug, but in the end, I had to write my adder WIT like:

package component:adder;

interface add {
  add: func(a: s32, b: s32) -> s32;
}

world adder {
    export add;
}

So it has add interface exported, which has an add function.

I don't think the behavior of cargo-component is expected, as in a WIT file, a world can just simply export a function.

@sunfishcode
Copy link
Member

Worlds aren't things you can select individual things from to import/export, because world items already have committed to being import or export. You can include the entire contents of one world into another though, like this:

world host {
   include component:adder/adder;
}

Or, you can put the function in an interface, as you figured out.

What WIT doesn't have right now is the ability to have a bare top-level function, which might look like this:

package component:adder;

add: func(a: s32, b: s32) -> s32;

that you could then import or export from a world.

@ifsheldon
Copy link
Author

Thanks! Are there any ongoing proposals for top-level functions?

You can include the entire contents of one world into another though

I wanted to compose two components, plug an adder component into a command line component. As far as I understand, including a WIT world is basically like copying and pasting the imports and exports of the included world, which is not suitable for composing components.

@sunfishcode
Copy link
Member

Thanks! Are there any ongoing proposals for top-level functions?

I'm not aware of any currently.

You can include the entire contents of one world into another though

I wanted to compose two components, plug an adder component into a command line component. As far as I understand, including a WIT world is basically like copying and pasting the imports and exports of the included world, which is not suitable for composing components.

Could you describe this in more detail? Including a WIT world is as you say, but this is often what one wants, when combining two worlds.

@ifsheldon
Copy link
Author

image

I think this screenshot should worth dozens of words. I have an adder component, which is just a library, and I want to test/run it in command line with wasmtime, so I should implement a command line component that imports adder's functionality and run the function.

@macovedj
Copy link
Collaborator

macovedj commented Dec 10, 2024

I think this screenshot should worth dozens of words. I have an adder component, which is just a library, and I want to test/run it in command line with wasmtime, so I should implement a command line component that imports adder's functionality and run the function.

And were you able to make this work? This should work with the wit you provided that uses the interface keyword, as right now this is the syntax that cargo-component expects. If you can share a link to the source code for the components you're authoring, it would probably help with identifying the cause of any issues you're having in your compositions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants