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

fail to read a file with deno #321

Open
sigmaSd opened this issue May 31, 2023 · 4 comments
Open

fail to read a file with deno #321

sigmaSd opened this issue May 31, 2023 · 4 comments

Comments

@sigmaSd
Copy link
Contributor

sigmaSd commented May 31, 2023

Steps to reproduce

  • mkdir f; cd f
  • cargo new a
  • echo "hello">file
  • echo 'fn main() {dbg!(std::fs::read_to_string("./file"));}'>a/src/main.rs
  • cargo b --manifest-path a/Cargo.toml --target-dir target --target wasm32-wasi
  • wasmer target/wasm32-wasi/debug/a.wasm --dir . # works, same for wasmtime
import { init, WASI } from "https://deno.land/x/wasm@v1.2.2/wasi.ts";
await init();

const wasi = new WASI({});

const moduleBytes = await Deno.readFile(
  "./target/wasm32-wasi/debug/a.wasm",
);
const module = await WebAssembly.compile(moduleBytes);
wasi.instantiate(module, {});

const exitCode = wasi.start();
const stdout = wasi.getStdoutString();
const stderr = wasi.getStderrString();
console.log(`${stdout}${stderr}(exit code: ${exitCode})`);

fails with

[src/main.rs:1] std::fs::read_to_string("./file") = Err(
    Os {
        code: 44,
        kind: NotFound,
        message: "No such file or directory",
    },
)
(exit code: 0)
@sigmaSd
Copy link
Contributor Author

sigmaSd commented May 31, 2023

deno wasi implementation have its own issue denoland/std#3415

@sigmaSd
Copy link
Contributor Author

sigmaSd commented May 31, 2023

I guess I'm expecting wasi io to map to deno permissions which might not be the case, it might be that I have to pass relevant config here new WASI({})

@sigmaSd
Copy link
Contributor Author

sigmaSd commented May 31, 2023

So thats probably it I need to use

const wasi = new WASI({ preopens: { ".": "." } });

but this fails

error: Uncaught (in promise) Error: Failed to create the WasiState: wasi filesystem creation error: `Could not get metadata for fil
e ".": fd not a directory``
        const ret = new Error(getStringFromWasm0(arg0, arg1));
                    ^
    at imports.wbg.__wbg_new_8d2af00bc1e329ee (https://deno.land/x/wasm@v1.2.2/pkg/wasmer_wasi_js.js:1185:21)
    at <anonymous> (https://deno.land/x/wasm@v1.2.2/pkg/wasmer_wasi_js_bg.wasm:1:19665)
    at new WASI (https://deno.land/x/wasm@v1.2.2/pkg/wasmer_wasi_js.js:632:18)
    at file:///home/mrcool/dev/deno/lab/tmp/xiU/b.ts:4:14
    at eventLoopTick (ext:core/01_core.js:182:11)

using deno std implementation (https://deno.land/std@0.190.0/wasi/snapshot_preview1.ts) works so this maybe is a wasmerjs issue

@jeff-hykin
Copy link

jeff-hykin commented Oct 11, 2024

Here's a temp workaround for deno:

import { WasmFs } from "@wasmer/wasmfs"
// wasmerio uses the deprecated .__proto__ = /*thing*/ to hack the prototype 
Object.defineProperty(Object.getPrototypeOf({}), "__proto__", {
    get() {
        return Object.getPrototypeOf(this)
    },
    set(value) {
        return Object.setPrototypeOf(this, value)
    }
})
const wasmFs = new WasmFs()

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