Skip to content

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

@NicFTK

Description

@NicFTK

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:val@1.0.0).

Relevant Code Structure:

assets/wit/val.wit

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

  package local:val@1.0.0; 
  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:plugins@1.0.0;

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

interface prettify-plugin {
    use local:val/types@1.0.0.{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/types@1.0.0": crate::val::exports::local::val::types // Appears to map already generated types rather than help with discovery
    // }
});

error message

package 'local:val@1.0.0' not found. known packages:
    acme:plugins@1.0.0
     --> \\?\G:\Aduit_Project\Rust_Test_Demo\rust-wasm-plugins-examples\assets\wit\acme-plugins.wit:20:9
      |
   20 |     use local:val/types@1.0.0.{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?

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