I don't remember offhand, but I think everything is driven by the WorldGenerator::generate funciton in crates/core/src/lib.rs, which first generates code for all the imports, then for all the exports. Since imports can't possibly refer to exported types, any aliases we generate for them must refer to the imported resource type. On the other hand, exports can refer to imported types, and I don't recall how we know which one is of interest when e.g. generating code for an alias that refers to a resource which is both imported and exported.
@Alex Crichton has explained this to me about five times already; maybe I can do it a sixth time?
ah yeah so the way this is handled in the Rust generator is subtly generating things in the "right order", more-or-less there's a map<TypeId, YourGeneratorThing> somewhere and that's populated as types are generated.
Imports are generated in topo-order and then exports, so that way everything should "just work" if you resolve aliases by "just look at the map"
basically things get really tricky if you try to resolve aliases or references in anything other than a trivial manner
BTW, I've always wished wit-parser would assign different TypeIds to the imported and exported version of a resource, which would make this easier to reason about. Not so much that I've actually done anything about it, of course.
there's also https://github.com/bytecodealliance/wasm-tools/issues/1497 which is a long-term aspiration here
Very helpful, thanks both!
Scott Waye has marked this topic as resolved.
Last updated: Dec 06 2025 at 06:05 UTC