nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.45k stars 278 forks source link

WASI preview1 function not implemented for directory entry #4231

Closed cataggar closed 2 months ago

cataggar commented 12 months ago

Details

I'm trying to run a WASI application from Node.js v20.5.1 and listing directory entries is not working. I added it in preopens. The check to see if it exists if it is a directory works.

PS C:\Users\cataggar\configur> node main.js -- /ev2
(node:142492) ExperimentalWarning: WASI is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
source: /ev2
exists: true
is_dir: true
# of dir entries: 1
dir entry: Err(Os { code: 52, kind: Unsupported, message: "Function not implemented" })

The same WASI works using Wasmtime.

PS C:\Users\cataggar\configur> wasmtime run --dir C:\Users\cataggar\ev2 target\wasm32-wasi\release\configur.wasm -- C:\Users\cataggar\ev2
source: C:\Users\cataggar\ev2
exists: true
is_dir: true
# of dir entries: 4
dir entry: Ok(DirEntry("C:\\Users\\cataggar\\ev2/environments"))
dir entry: Ok(DirEntry("C:\\Users\\cataggar\\ev2/flags.yml"))
dir entry: Ok(DirEntry("C:\\Users\\cataggar\\ev2/README.md"))
dir entry: Ok(DirEntry("C:\\Users\\cataggar\\ev2/rules.yml"))

Node.js version

v20.5.1 winget upgrade --id OpenJS.NodeJS

Example code

Full project is in the nodejs branch here. https://github.com/cataggar/configur/tree/nodejs

cargo install cargo-wasi cargo wasi build --release node main.js -- /ev2

You will need to change the hardcoded preopens path from main.js:

// https://nodejs.org/api/wasi.html

import { readFile } from "node:fs/promises";
import { WASI } from "wasi";
import { argv, env } from "node:process";

// get the index for the first argument that ends in main.js
const index = argv.findIndex((arg) => arg.endsWith("main.js"));
const args = argv.slice(index + 1);

const wasi = new WASI({
  version: "preview1",
  args,
  env,
  preopens: {
    "/ev2": "C:/Users/cataggar/ev2",
  },
});

const wasm = await WebAssembly.compile(
  await readFile(
    new URL("target/wasm32-wasi/release/configur.wasm", import.meta.url)
  )
);
const instance = await WebAssembly.instantiate(wasm, wasi.getImportObject());

wasi.start(instance);

main.rs is pretty straight forward:

use anyhow::Result;
use clap::Parser;

#[derive(Parser)]
#[command(version)]
struct Cli {
    source: String,
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    let source = &cli.source;
    println!("source: {source}");
    let path = std::path::Path::new(&cli.source);
    let exists = path.exists();
    println!("exists: {exists}");
    let is_dir = path.is_dir();
    println!("is_dir: {is_dir}");
    let paths = path.read_dir();
    match paths {
        Ok(paths) => {
            let dir_entries = paths.into_iter().collect::<Vec<_>>();
            println!("# of dir entries: {}", dir_entries.len());
            for path in dir_entries {
                println!("dir entry: {path:?}");
            }
        }
        Err(e) => println!("{:?}", e),
    }
    Ok(())
}

Operating system

Windows 11 22H2

Scope

WASI preview1

Module and version

WASI

preveen-stack commented 12 months ago

cc @nodejs/wasi

devsnek commented 12 months ago

@cjihrig do you remember why uvwasi_fd_readdir is stubbed out on windows?

cataggar commented 12 months ago

Confirming that it does work on Linux using WSL2 Debian with "/ev2": "/mnt/c/Users/cataggar/ev2",. The problem is limited to Windows.

/mnt/c/Users/cataggar/configur> node main.js -- /ev2 
ExperimentalWarning: WASI is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
source: /ev2
exists: true
is_dir: true
# of dir entries: 4
dir entry: Ok(DirEntry("/ev2/environments"))
dir entry: Ok(DirEntry("/ev2/flags.yml"))
dir entry: Ok(DirEntry("/ev2/README.md"))
dir entry: Ok(DirEntry("/ev2/rules.yml"))
cjihrig commented 12 months ago

@devsnek I'm not 100% sure anymore, but I believe there was an issue with supporting the cookie input properly on Windows.

github-actions[bot] commented 3 months ago

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] commented 2 months ago

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.