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

Rust: two `generate!` macros in same module gives compilation error

Open itowlson opened this issue 2 months ago • 1 comments

Consider a Rust file with two generate! macros:

wit_bindgen::generate!({
    inline: r#"
    package test:test-useful;
    world t {
        import useful:thingy/[email protected];
    }
    "#,
    path: "useful_thingy_0_0_1.wasm",
    world: "test:test-useful/t",
    generate_all
});

wit_bindgen::generate!({
    inline: r#"
    package test:test-useless;
    world t {
        import useless:feature/[email protected];
    }
    "#,
    path: "useless_feature_0_0_1.wasm",
    world: "test:test-useless/t",
    generate_all
});

When I build this, I get redefinition errors:

error[E0428]: the name `__WIT_BINDGEN_COMPONENT_TYPE` is defined multiple times
  --> /home/ivan/testing/composy-build-script-madness/comp-consumer-build-rs-test/target/wasm32-wasip2/release/build/comp-consumer-build-rs-test-ad279f1832015f79/out/biscuits.rs:18:1
   |
3  | wit_bindgen::generate!({
   | ----------------------- previous definition of the value `__WIT_BINDGEN_COMPONENT_TYPE` here
...
18 | wit_bindgen::generate!({
   | ^^^^^^^^^^^^^^^^^^^^^^^ `__WIT_BINDGEN_COMPONENT_TYPE` redefined here
   |
   = note: `__WIT_BINDGEN_COMPONENT_TYPE` must be defined only once in the value namespace of this module
   = note: this error originates in the macro `wit_bindgen::generate` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0428]: the name `__link_custom_section_describing_imports` is defined multiple times
  --> /home/ivan/testing/composy-build-script-madness/comp-consumer-build-rs-test/target/wasm32-wasip2/release/build/comp-consumer-build-rs-test-ad279f1832015f79/out/biscuits.rs:18:1
   |
3  | wit_bindgen::generate!({
   | ----------------------- previous definition of the value `__link_custom_section_describing_imports` here
...
18 | wit_bindgen::generate!({
   | ^^^^^^^^^^^^^^^^^^^^^^^ `__link_custom_section_describing_imports` redefined here
   |
   = note: `__link_custom_section_describing_imports` must be defined only once in the value namespace of this module
   = note: this error originates in the macro `wit_bindgen::generate` (in Nightly builds, run with -Z macro-backtrace for more info)

I can work around this by putting the generate! macros in different modules, but this adds to the already very lengthy prefixes for the functions and types in the imported interfaces.

It would be nice if I could have multiple generate! macros in the same module.

Please let me know if you need more details or a full repro!

itowlson avatar Oct 02 '25 19:10 itowlson

This ought to be fixable if we make a unique lexical name for the component type custom section, probably based on the world name.

pchickey avatar Oct 03 '25 16:10 pchickey