aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bun.js/fs.exports.js10
-rw-r--r--src/bun.js/node/node_fs.zig18
-rw-r--r--test/js/node/fs/fs.test.ts17
3 files changed, 36 insertions, 9 deletions
diff --git a/src/bun.js/fs.exports.js b/src/bun.js/fs.exports.js
index dfb1947e7..df2fbf382 100644
--- a/src/bun.js/fs.exports.js
+++ b/src/bun.js/fs.exports.js
@@ -157,9 +157,15 @@ export var access = function access(...args) {
function callbackify(fsFunction, args) {
try {
const result = fsFunction.apply(fs, args.slice(0, args.length - 1));
- queueMicrotask(() => args[args.length - 1](null, result));
+ const callback = args[args.length - 1];
+ if (typeof callback === "function") {
+ queueMicrotask(() => callback(null, result));
+ }
} catch (e) {
- queueMicrotask(() => args[args.length - 1](e));
+ const callback = args[args.length - 1];
+ if (typeof callback === "function") {
+ queueMicrotask(() => callback(e));
+ }
}
}
diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig
index fe62e77ec..f7300f28e 100644
--- a/src/bun.js/node/node_fs.zig
+++ b/src/bun.js/node/node_fs.zig
@@ -3028,15 +3028,19 @@ pub const NodeFS = struct {
prefix_buf[len..][0..6].* = "XXXXXX".*;
prefix_buf[len..][6] = 0;
+ // The mkdtemp() function returns a pointer to the modified template
+ // string on success, and NULL on failure, in which case errno is set to
+ // indicate the error
+
const rc = C.mkdtemp(prefix_buf);
- switch (std.c.getErrno(@ptrToInt(rc))) {
- .SUCCESS => {},
- else => |errno| return .{ .err = Syscall.Error{ .errno = @truncate(Syscall.Error.Int, @enumToInt(errno)), .syscall = .mkdtemp } },
+ if (rc) |ptr| {
+ return .{
+ .result = JSC.ZigString.dupeForJS(bun.sliceTo(ptr, 0), bun.default_allocator) catch unreachable,
+ };
}
-
- return .{
- .result = JSC.ZigString.dupeForJS(bun.sliceTo(rc.?, 0), bun.default_allocator) catch unreachable,
- };
+ // std.c.getErrno(rc) returns SUCCESS if rc is null so we call std.c._errno() directly
+ const errno = @intToEnum(std.c.E, std.c._errno().*);
+ return .{ .err = Syscall.Error{ .errno = @truncate(Syscall.Error.Int, @enumToInt(errno)), .syscall = .mkdtemp } };
}
pub fn open(this: *NodeFS, args: Arguments.Open, comptime flavor: Flavor) Maybe(Return.Open) {
switch (comptime flavor) {
diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts
index 83739837c..e39db9ecf 100644
--- a/test/js/node/fs/fs.test.ts
+++ b/test/js/node/fs/fs.test.ts
@@ -22,6 +22,7 @@ import fs, {
promises,
unlinkSync,
mkdtempSync,
+ mkdtemp,
constants,
Dirent,
Stats,
@@ -158,6 +159,22 @@ it("mkdtempSync() empty name", () => {
expect(existsSync(tempdir)).toBe(false);
});
+it("mkdtempSync() non-exist dir #2568", () => {
+ try {
+ expect(mkdtempSync("/tmp/hello/world")).toBeFalsy();
+ } catch (err: any) {
+ expect(err?.errno).toBe(-2);
+ }
+});
+
+it("mkdtemp() non-exist dir #2568", done => {
+ mkdtemp("/tmp/hello/world", (err, folder) => {
+ expect(err?.errno).toBe(-2);
+ expect(folder).toBeUndefined();
+ done();
+ });
+});
+
it("readdirSync on import.meta.dir with trailing slash", () => {
const dirs = readdirSync(import.meta.dir + "/");
expect(dirs.length > 0).toBe(true);