wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

Rust: How to Fix wit_bindgen's 'package not found' Error for Same-Directory WIT Files?

Open NicFTK opened this issue 11 months ago • 7 comments

When the wit_bindgen::generate! macro runs, it fails to find the package local:val defined in val.wit and reports an error like "package 'local:val' not found" (or with a version, e.g., local:[email protected]).

Relevant Code Structure:

assets/wit/val.wit

(defines common types and declares the package local:val)

  package local:[email protected]; 
  interface types {

    variant val {
        null,
        blob(blob),
        boolen(bool),
        str(string),
        float(f64),
        int(s64),
        error(string),
    }
}

// 可能还有 world 定义,但接口是关键
world val {
  export types;
}

assets/wit/acme-plugins.wit

(uses types from val.wit via use local:val.{...})

// filepath: assets/wit/acme-plugins.wit
package acme:[email protected];

interface host {
    log: func(message: string);
}

interface prettify-plugin {
    use local:val/[email protected].{val}; 
    prettify: func(content: string) -> val; 
}

world prettify {
    import host;
    export prettify-plugin;
}

crates/plugin/src/bindings.rs

(calls wit_bindgen::generate!)

use crate::val::exports::local::val;

wit_bindgen::generate!({
    path: "../../assets/wit/acme-plugins.wit", // Points to the main WIT file
    default_bindings_module: "crate::bindings",
    pub_export_macro: true,
    // I tried adding 'with' or 'dependencies', but neither seems correct or recognized
    // with: {
    //    "local:val/[email protected]": crate::val::exports::local::val::types // Appears to map already generated types rather than help with discovery
    // }
});

error message

package 'local:[email protected]' not found. known packages:
    acme:[email protected]
     --> \\?\G:\Aduit_Project\Rust_Test_Demo\rust-wasm-plugins-examples\assets\wit\acme-plugins.wit:20:9
      |
   20 |     use local:val/[email protected].{val};
      |         ^--------rustc[Click for full compiler diagnostic](rust-analyzer-diagnostics-view:/diagnostic%20message%20%5B0%5D?0#file%3A%2F%2F%2Fg%253A%2FAduit_Project%2FRust_Test_Demo%2Frust-wasm-plugins-examples%2Fcrates%2Fplugin%2Fsrc%2Fbindings.rs)

Expected Behavior:

I expect wit_bindgen::generate! to automatically resolve dependencies like local:val by searching the directory of the main WIT file (acme-plugins.wit, located at ../../assets/wit/), as long as val.wit exists in the same directory and properly declares its package.

Questions:

Should wit_bindgen::generate! automatically handle WIT dependencies in the same directory?

If yes, what configuration or file structure might I be missing?

If not, how should I configure wit_bindgen::generate! or restructure the project to ensure it finds and uses val.wit?

NicFTK avatar Apr 27 '25 14:04 NicFTK

I've written up some documentation here (I should have done this awhile ago...)

The problem is that when you specify a single file for bindings generation then that's all that's read from the filesystem. If the dependency is located in another file then you either need to (a) use the directory format for WIT instead, (b) specify the packages in a single file with inline package annotations, or (c) specify path: [ ... ] with the two files.

alexcrichton avatar Apr 28 '25 15:04 alexcrichton

Thank you very much. Now it works for me with the following configuration:

wit_bindgen::generate!({
    path: ["../../assets/wit/val.wit","../../assets/wit/acme-plugins.wit",],
    default_bindings_module: "crate::bindings",
    pub_export_macro: true,
    generate_all,
});

NicFTK avatar Apr 29 '25 01:04 NicFTK

Now I want to try using the [with] into its own crate and implement some common methods for it, so that I can reuse these types in the future. Therefore, I want to use to replace the existing bindings, but I encountered an error: package 'local:[email protected]' not found. known packages: wit_bindgen::generate!({


    path:  "../../assets/wit/acme-plugins.wit",
    default_bindings_module: "crate::bindings",
    pub_export_macro: true,
    with: {
        "local:[email protected]": crate::val::exports::local::val,
    }
});

Image

I searched some documentation but did not find a similar issue. Am I using it incorrectly somewhere?

NicFTK avatar Apr 29 '25 02:04 NicFTK

The error message highlights path: "..." which is different from the invocation that you said worked, which was using path: [...], are you sure that the path there is correct? The error message looks like it's about WIT discovery/parsing, unrelated to with.

alexcrichton avatar Apr 29 '25 14:04 alexcrichton

Initially, I assumed that using with would directly replace the modules in WIT with the ones specified in the with configuration, without continuing to search for existing WIT files. After modifying my code as shown below

wit_bindgen::generate!({

    world:"acme:plugins/prettify",
    path: ["../../assets/wit/val.wit","../../assets/wit/acme-plugins.wit",],

    default_bindings_module: "crate::bindings",
    pub_export_macro: true,
    with: {
        "local:val/[email protected]": crate::val::exports::local::val::types, 
    }
});

the error has been resolved. I will test my code further to ensure everything works as expected before closing this issue. Additionally, I plan to document the issues I encountered and their solutions to serve as a reference for other beginners. Thank you for your patience and support!

NicFTK avatar Apr 30 '25 00:04 NicFTK

Sounds good, and thanks! Docs are always welcome :)

And yeah WIT resolution happens at a different layer than with, the WIT resolution all happens in isolation and with only affects the generated bindings after the WIT was parsed.

alexcrichton avatar Apr 30 '25 14:04 alexcrichton

Finally I have some free time to finish my demo. I had no issues so far when working with wit-bindgen, but I ran into a problem binding the same WIT to Wasmtime. The main issue is that I want to reuse the structs defined in the [val] module, but right now I’m not sure how to proceed to fix the error.

Image

here is my test project https://github.com/NicFTK/Rust_Wasm_Plugin_Example

NicFTK avatar Jun 24 '25 10:06 NicFTK