Skip to content

Rust and Wasm problems when trying to access a file #81703

Closed
@AndreaEsposit

Description

@AndreaEsposit

I have been trying to create/read/edit a file though a WebAssembly module originally written in Rust, but it is not working.

The code looks like this(src/lib.rs):

#[no_mangle]
pub fn write() -> Result<(), io::Error> {
    let mut file = File::create("./foo.txt")?;
    file.write_all(b"Hello, world!")?;
    Ok(())
}
#[no_mangle]
pub extern "C" fn hello() -> (){
    let err = write();
    match err {
        Ok(()) => print!("Managed"),
        Err(_e) => print!("Didn't manage");
    }
}

This is my cargo.toml:

[package]
name = "rust_write"
version = "0.1.0"
edition ="2018"

[lib]
crate-type = ["cdylib"]

I am using rustc 1.51.0-nightly, and I compiled the file with this command cargo build --target wasm32-wasi.
The Wa. module will not work either with wasmtime CLI ( wasmtime --dir=. --invoke hello rust_write.wasm ) or embedded, however if I remove the crate-type = ["cdylib"] from cargo.toml and write the exact same hello function as a main function in ````src/main.rs```, then it works without problems.

Could it be that the issue is that _start is not called when you directly --invoke the hello function.

Am I intended to not be able to access files if I am using a WASI library, or is it a bug?
Am I just straight up doing something wrong when I compile?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions