bytecodealliance / componentize-py

Apache License 2.0
130 stars 13 forks source link

Create more actionable error for disallowed name for a Python module #78

Open kate-goldenring opened 4 months ago

kate-goldenring commented 4 months ago

Error

I ran into the following error:

$ componentize-py --wit-path add.wit --world example componentize example -o add.wasm
Traceback (most recent call last):
  File "/opt/homebrew/bin/componentize-py", line 8, in <module>
    sys.exit(script())
             ^^^^^^^^
AssertionError:

Caused by:
    TypeError: Can't instantiate abstract class Example without an implementation for abstract method 'add'

Repro

My set up was the following:

// WIT
package example:component;

world example {
    export add: func(x: s32, y: s32) -> s32;
}

App:

$ cat<<EOT >> example.py
import example

class Example(example.Example):
    def add(self, x: int, y: int) -> int:
        return x + y
EOT

Now when i go to componentize I get the TypeError:

$ componentize-py --wit-path /path/to/examples/example-host/add.wit --world example componentize example -o add.wasm
Traceback (most recent call last):
...

Workaround

@dicej pointed out that the issue is that you cannot name the Python module (example.py) the same as the world name (example), since componentize-py will generate a module using the world name and only use one of them. Renaming example.py to app.py resolved the issue.

The error thrown should be more descriptive and actionable. It was not clear from the TypeError how to resolve the issue.