Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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.
47 changes: 47 additions & 0 deletions test/js/bun/wasm/wasi.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { spawnSync } from "bun";
import { expect, it } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { mkdtempSync, writeFileSync, rmSync } from "fs";
import { join } from "path";
import { tmpdir } from "os";
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

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

it("path_open should resolve paths against preopens, not cwd", () => {
// Create a temp file to read via WASI
const tmp = mkdtempSync(join(tmpdir(), "bun-wasi-test-"));
const testFile = join(tmp, "input.txt");
writeFileSync(testFile, "hello from preopens\n");

try {
// read-file.wasm: minimal Rust WASI binary that reads a file path from args
// and prints its contents to stdout.
//
// Built from:
// cargo new --name read-file-wasi 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,
});
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});