wasmerio / wasmer

🚀 The leading Wasm Runtime supporting WASIX, WASI and Emscripten
https://wasmer.io
MIT License
18.44k stars 789 forks source link

Passing a filename containing hyphens to the `wasmer create-exe` command will report `cannot get prefix for atom` error. #3834

Open tt4g opened 1 year ago

tt4g commented 1 year ago

Describe the bug

Versions:

$ wasmer -vV
wasmer 3.2.1 (74fe894 2023-04-21)
binary: wasmer-cli
commit-hash: 74fe894c075351cc40ca5e408bd9a94298a1e459
commit-date: 2023-04-21
host: x86_64-unknown-linux-gnu
compiler: singlepass,cranelift,llvm

$ rustc -vV
rustc 1.69.0 (84c898d65 2023-04-16)
binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: x86_64-unknown-linux-gnu
release: 1.69.0
LLVM version: 15.0.7

I am trying to build .wasm with a very simple Rust project hello-world and generate an executable with wasmer create-exe. However, the wasmer create-exe command always fails.

src/main.rs

fn main() {
    println!("Hello, Wasm!");
}
$ cargo build --target wasm32-wasi
$ wasmer target/wasm32-wasi/debug/hello-wasm.wasm
Hello, Wasm!

$ wasmer create-exe -o ./hello-wasm target/wasm32-wasi/debug/hello-wasm.wasm
Compiler: cranelift
Target: x86_64-unknown-linux-gnu
Using path `/.wasmer/cache/wasmer-linux-amd64/lib/libwasmer-headless.a` as libwasmer path.
error: cannot get prefix for atom hello-world

I found the corresponding Rust code from the error message.

https://github.com/wasmerio/wasmer/blob/74fe894c075351cc40ca5e408bd9a94298a1e459/lib/cli/src/commands/create_exe.rs#L960-L964

I noticed a difference in the way the atom_name is generated.

Statement let atom_name = &atom.atom; was generated by prepare_directory_from_single_wasm_file(). The app_name variable is expected to be assigned hello-world (since the filename is taken from hello-world.wasm).

https://github.com/wasmerio/wasmer/blob/74fe894c075351cc40ca5e408bd9a94298a1e459/lib/cli/src/commands/create_exe.rs#L865-L907

On the other hand, prefixes variables are generated by PrefixMapCompilation::from_input(), which formats the name corresponding to atom_name with normalize_atom_name().

https://github.com/wasmerio/wasmer/blob/74fe894c075351cc40ca5e408bd9a94298a1e459/lib/cli/src/commands/create_exe.rs#L496-L517

normalize_atom_name() function is a simple function that converts - to _. So I renamed the file and the command succeeded.

$ mv target/wasm32-wasi/debug/hello-world.wasm target/wasm32-wasi/debug/hello_world.wasm
$ wasmer create-exe target/wasm32-wasi/debug/hello_world.wasm -o ./hello_world
Compiler: cranelift
Target: x86_64-unknown-linux-gnu
Using path `/.wasmer/cache/wasmer-linux-amd64/lib/libwasmer-headless.a` as libwasmer path.
✔ Native executable compiled successfully to `./hello_world`.
$ ./hello_world 
Hello, world!

Steps to reproduce

Expected behavior

Actual behavior

Additional context

I am not familiar with WebAssembly, so I don't know if there is a "no hyphen in filenames" rule. Is this my mistake? Or is it a bug in wasmer create-exe?

ptitSeb commented 1 year ago

It's a bug on create-exe. Thank you for openning the ticket. Not sure when we will fix this, in the mean time, we should add a document entry to warn about this bug.

stale[bot] commented 4 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Ev1lT3rm1nal commented 3 months ago

Any update?