diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig index 31c5d47fcb..8d725cf458 100644 --- a/src/bun.js/node/node_fs.zig +++ b/src/bun.js/node/node_fs.zig @@ -4771,8 +4771,8 @@ pub const NodeFS = struct { break :brk switch (bun.sys.open( path, - bun.O.RDONLY | bun.O.NOCTTY, - 0, + @intFromEnum(args.flag) | bun.O.NOCTTY, + default_permission, )) { .err => |err| return .{ .err = err.withPath(if (args.path == .path) args.path.path.slice() else ""), diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index 315bfb8e0b..5f01425eee 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -597,7 +597,7 @@ describe("mkdirSync", () => { // @ts-expect-error { recursive: "lalala" }, ), - ).toThrow("The \"recursive\" property must be of type boolean, got string"); + ).toThrow('The "recursive" property must be of type boolean, got string'); }); }); @@ -1406,6 +1406,36 @@ describe("readFile", () => { }); }); }); + + it("works with flags", async () => { + const mydir = tempDirWithFiles("fs-read", {}); + console.log(mydir); + + for (const [flag, code] of [ + ["a", "EBADF"], + ["ax", "EBADF"], + ["a+", undefined], + ["as", "EBADF"], + ["as+", undefined], + ["r", "ENOENT"], + ["rs", "ENOENT"], + ["r+", "ENOENT"], + ["rs+", "ENOENT"], + ["w", "EBADF"], + ["wx", "EBADF"], + ["w+", undefined], + ["wx+", undefined], + ]) { + const name = flag!.replace("+", "_plus") + ".txt"; + if (code == null) { + expect(readFileSync(mydir + "/" + name, { encoding: "utf8", flag })).toBe(""); + expect(readFileSync(mydir + "/" + name, { encoding: "utf8" })).toBe(""); + } else { + expect.toThrowWithCode(() => readFileSync(mydir + "/" + name, { encoding: "utf8", flag }), code); + expect.toThrowWithCode(() => readFileSync(mydir + "/" + name, { encoding: "utf8" }), "ENOENT"); + } + } + }); }); describe("writeFileSync", () => {