Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/js/node/wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ var require_wasi = __commonJS({
path_open: wrap(
(dirfd, _dirflags, pathPtr, pathLen, oflags, fsRightsBase, fsRightsInheriting, fsFlags, fdPtr) => {
try {
CHECK_FD(dirfd, constants_1.WASI_RIGHT_PATH_OPEN);
const stats = CHECK_FD(dirfd, constants_1.WASI_RIGHT_PATH_OPEN);
fsRightsBase = BigInt(fsRightsBase);
fsRightsInheriting = BigInt(fsRightsInheriting);
const read =
Expand Down Expand Up @@ -1512,7 +1512,7 @@ var require_wasi = __commonJS({
if (p.startsWith("proc/")) {
throw new types_1.WASIError(constants_1.WASI_EBADF);
}
const fullUnresolved = path.resolve(p);
const fullUnresolved = path.resolve(stats.path, p);
let full;
try {
full = fs.realpathSync(fullUnresolved);
Expand Down
Binary file added test/js/bun/wasm/read-file.wasm
Binary file not shown.
42 changes: 41 additions & 1 deletion test/js/bun/wasm/wasi.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spawnSync } from "bun";
import { expect, it } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { bunEnv, bunExe, tempDirWithFiles } from "harness";

it("Should support printing 'hello world'", () => {
const { stdout, stderr, exitCode } = spawnSync({
Expand All @@ -20,3 +20,43 @@ it("Should support printing 'hello world'", () => {
exitCode: 0,
});
});

it("path_open should resolve paths against preopens, not cwd", () => {
const tmp = tempDirWithFiles("bun-wasi-test", {
"input.txt": "hello from preopens\n",
});
const testFile = `${tmp}/input.txt`;

// read-file.wasm: minimal Rust WASI binary (wasm32-wasip1) that reads a file
// path from args and prints its contents to stdout.
//
// Built with:
// cargo init --name read-file-wasi
// # src/main.rs:
// # use std::{env, fs, process};
// # fn main() {
// # let args: Vec<String> = env::args().collect();
// # if args.len() < 2 { eprintln!("usage: read-file <path>"); process::exit(1); }
// # match fs::read_to_string(&args[1]) {
// # Ok(c) => print!("{}", c),
// # Err(e) => { eprintln!("error: {}: {}", args[1], e); process::exit(1); }
// # }
// # }
// cargo build --target wasm32-wasip1 --release
const { stdout, stderr, exitCode } = spawnSync({
cmd: [bunExe(), import.meta.dir + "/read-file.wasm", testFile],
stdout: "pipe",
stderr: "pipe",
env: bunEnv,
});

expect({
stdout: stdout.toString(),
stderr: stderr.toString(),
exitCode: exitCode,
}).toEqual({
stdout: "hello from preopens\n",
stderr: "",
exitCode: 0,
});
});