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

Tolerate duplicate imports when creating components #2063

Open
erikrose opened this issue Feb 21, 2025 · 4 comments
Open

Tolerate duplicate imports when creating components #2063

erikrose opened this issue Feb 21, 2025 · 4 comments

Comments

@erikrose
Copy link
Contributor

Multiple imports of the same name are in-spec for core-wasm modules but not allowed for components. Currently, attempting to componentize a module with non-uniquely named imports yields…

% wasm-tools component new main-with-dupe-imports.wasm -o component-with-dupes.wasm --adapt wasi_snapshot_preview1.wasm
error: failed to encode a component from module

Caused by:
    0: failed to decode world from module
    1: module was not valid
    2: module has duplicate import for `wasi_snapshot_preview1::fd_write`

We should pick a unique import of each name and ignore the rest when componentizing. Since inter-component linking is done solely by name, multiple same-named imports have no way of being distinguished in order to be satisfied individually—even if they had unique types, for example—and so nothing is lost by ignoring them.

@alexcrichton
Copy link
Member

This sounds reasonable to me! The implementation will be somewhat complex/nontrivial though (but certainly manageable). The error comes from around here and the rough shape I think of a solving this would be:

  • Add a new map to ImportMap which maps "old wasm import" to "new name". By default names are all the same, but on conflict new names would be synthesized (e.g. by adding N to the end)
  • Pretend the wasm actually imported foo::barN everywhere else in wit-component to make things eaiser.
  • When the core module is emitted instead consult the import map. If the import map says that there exists some name which was renamed then the binary needs to be translated to rename the import in the import section.

Mutating the module's import section (and just the import section) can be mirrored after what wasm-tools demangle does except by working on the import section. This can be made easier through the Reencode::parse_import function/hook, probably with some other translations as well.

@erikrose
Copy link
Contributor Author

I'll take a swing!

@erikrose
Copy link
Contributor Author

Note to self: also keep in mind that imports can be inlined in function definitions.

@alexcrichton
Copy link
Member

To clarify though @erikrose that's purely sugar in the wasm text format which wit-component doesn't operate over. Once a binary is produced all imports are exclusively in the import section of the binary module.

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

2 participants