diff --git a/packages/bun-internal-test/src/banned.json b/packages/bun-internal-test/src/banned.json index 5ec3a57e08..dcb4f3c8dc 100644 --- a/packages/bun-internal-test/src/banned.json +++ b/packages/bun-internal-test/src/banned.json @@ -8,5 +8,6 @@ "std.mem.indexOfAny": "Use bun.strings.indexAny or bun.strings.indexAnyComptime", "undefined != ": "This is by definition Undefined Behavior.", "undefined == ": "This is by definition Undefined Behavior.", + "bun.toFD(std.fs.cwd().fd)": "Use bun.FD.cwd()", "": "" } diff --git a/src/StandaloneModuleGraph.zig b/src/StandaloneModuleGraph.zig index f99906cf51..64e4d93874 100644 --- a/src/StandaloneModuleGraph.zig +++ b/src/StandaloneModuleGraph.zig @@ -563,7 +563,7 @@ pub const StandaloneModuleGraph = struct { bun.C.moveFileZWithHandle( fd, - bun.toFD(std.fs.cwd().fd), + bun.FD.cwd(), bun.sliceTo(&(try std.os.toPosixPath(temp_location)), 0), bun.toFD(root_dir.fd), bun.sliceTo(&(try std.os.toPosixPath(std.fs.path.basename(outfile))), 0), diff --git a/src/bun.js/RuntimeTranspilerCache.zig b/src/bun.js/RuntimeTranspilerCache.zig index d6dff34efb..06e8ac023f 100644 --- a/src/bun.js/RuntimeTranspilerCache.zig +++ b/src/bun.js/RuntimeTranspilerCache.zig @@ -548,10 +548,10 @@ pub const RuntimeTranspilerCache = struct { break :brk try bun.toLibUVOwnedFD(dir.fd); } - break :brk bun.toFD(std.fs.cwd().fd); + break :brk bun.FD.cwd(); }; defer { - if (cache_dir_fd != bun.toFD(std.fs.cwd().fd)) _ = bun.sys.close(cache_dir_fd); + if (cache_dir_fd != bun.FD.cwd()) _ = bun.sys.close(cache_dir_fd); } try Entry.save( diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index 44147932d3..d2bfc5d3cd 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -826,7 +826,7 @@ pub fn getMain( } const fd = bun.sys.openatA( - if (comptime Environment.isWindows) bun.invalid_fd else bun.toFD(std.fs.cwd().fd), + if (comptime Environment.isWindows) bun.invalid_fd else bun.FD.cwd(), vm.main, // Open with the minimum permissions necessary for resolving the file path. diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig index 1ac5b31702..9e48f4fd22 100644 --- a/src/bun.js/node/node_fs.zig +++ b/src/bun.js/node/node_fs.zig @@ -2867,7 +2867,7 @@ pub const Arguments = struct { .flag = flag, .mode = mode, .data = data, - .dirfd = bun.toFD(std.fs.cwd().fd), + .dirfd = bun.FD.cwd(), }; } }; @@ -4803,7 +4803,7 @@ pub const NodeFS = struct { ) Maybe(void) { const flags = os.O.DIRECTORY | os.O.RDONLY; - const atfd = if (comptime is_root) bun.toFD(std.fs.cwd().fd) else async_task.root_fd; + const atfd = if (comptime is_root) bun.FD.cwd() else async_task.root_fd; const fd = switch (switch (Environment.os) { else => Syscall.openat(atfd, basename, flags, 0), // windows bun.sys.open does not pass iterable=true, @@ -4953,7 +4953,7 @@ pub const NodeFS = struct { } const flags = os.O.DIRECTORY | os.O.RDONLY; - const fd = switch (Syscall.openat(if (root_fd == bun.invalid_fd) bun.toFD(std.fs.cwd().fd) else root_fd, basename, flags, 0)) { + const fd = switch (Syscall.openat(if (root_fd == bun.invalid_fd) bun.FD.cwd() else root_fd, basename, flags, 0)) { .err => |err| { if (root_fd == bun.invalid_fd) { return .{ @@ -5101,7 +5101,7 @@ pub const NodeFS = struct { const fd = switch (switch (Environment.os) { else => Syscall.open(path, flags, 0), // windows bun.sys.open does not pass iterable=true, - .windows => bun.sys.openDirAtWindowsA(bun.toFD(std.fs.cwd().fd), path, .{ .iterable = true, .read_only = true }), + .windows => bun.sys.openDirAtWindowsA(bun.FD.cwd(), path, .{ .iterable = true, .read_only = true }), }) { .err => |err| return .{ .err = err.withPath(args.path.slice()), @@ -6555,7 +6555,7 @@ pub const NodeFS = struct { } const open_flags = os.O.DIRECTORY | os.O.RDONLY; - const fd = switch (Syscall.openatOSPath(bun.toFD(std.fs.cwd().fd), src, open_flags, 0)) { + const fd = switch (Syscall.openatOSPath(bun.FD.cwd(), src, open_flags, 0)) { .err => |err| { task.finishConcurrently(.{ .err = err.withPath(this.osPathIntoSyncErrorBuf(src)) }); return false; diff --git a/src/bun.js/node/path_watcher.zig b/src/bun.js/node/path_watcher.zig index 7b09dc0f4f..91d11a29f8 100644 --- a/src/bun.js/node/path_watcher.zig +++ b/src/bun.js/node/path_watcher.zig @@ -92,7 +92,7 @@ pub const PathWatcherManager = struct { switch (switch (Environment.os) { else => bun.sys.open(path, std.os.O.DIRECTORY | std.os.O.RDONLY, 0), // windows bun.sys.open does not pass iterable=true, - .windows => bun.sys.openDirAtWindowsA(bun.toFD(std.fs.cwd().fd), path, .{ .iterable = true, .read_only = true }), + .windows => bun.sys.openDirAtWindowsA(bun.FD.cwd(), path, .{ .iterable = true, .read_only = true }), }) { .err => |e| { if (e.errno == @intFromEnum(bun.C.E.NOTDIR)) { diff --git a/src/bun.zig b/src/bun.zig index 6ab67abb7a..536512da2a 100644 --- a/src/bun.zig +++ b/src/bun.zig @@ -68,6 +68,7 @@ else if (Environment.isWindows) else std.os.fd_t; +pub const FD = FileDescriptor; pub const FileDescriptor = enum(FileDescriptorInt) { /// Zero is used in old filesystem code to indicate "no file descriptor" /// This is problematic because on POSIX, this is ambiguous with stdin being 0. diff --git a/src/cli/bunx_command.zig b/src/cli/bunx_command.zig index f0041dbd98..9803cabf0d 100644 --- a/src/cli/bunx_command.zig +++ b/src/cli/bunx_command.zig @@ -148,7 +148,7 @@ pub const BunxCommand = struct { bun.pathLiteral("{s}/package.json"), .{tempdir_name}, ) catch unreachable; - const target_package_json_fd = bun.sys.openat(bun.toFD(std.fs.cwd().fd), subpath_z, std.os.O.RDONLY, 0).unwrap() catch return error.NeedToInstall; + const target_package_json_fd = bun.sys.openat(bun.FD.cwd(), subpath_z, std.os.O.RDONLY, 0).unwrap() catch return error.NeedToInstall; const target_package_json = bun.sys.File{ .handle = target_package_json_fd }; const is_stale = is_stale: { @@ -186,7 +186,7 @@ pub const BunxCommand = struct { .{ tempdir_name, package_name }, ) catch unreachable; - return try getBinNameFromSubpath(bundler, bun.toFD(std.fs.cwd().fd), subpath_z); + return try getBinNameFromSubpath(bundler, bun.FD.cwd(), subpath_z); } /// Check the enclosing package.json for a matching "bin" diff --git a/src/fs.zig b/src/fs.zig index f686855036..8fa1bcf39f 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -683,7 +683,7 @@ pub const FileSystem = struct { bun.assert(this.fd != bun.invalid_fd); bun.assert(this.dir_fd != bun.invalid_fd); - try C.moveFileZWithHandle(this.fd, this.dir_fd, bun.sliceTo(from_name, 0), bun.toFD(std.fs.cwd().fd), bun.sliceTo(name, 0)); + try C.moveFileZWithHandle(this.fd, this.dir_fd, bun.sliceTo(from_name, 0), bun.FD.cwd(), bun.sliceTo(name, 0)); this.close(); } diff --git a/src/install/resolvers/folder_resolver.zig b/src/install/resolvers/folder_resolver.zig index a0cb160d0c..d5f7743d02 100644 --- a/src/install/resolvers/folder_resolver.zig +++ b/src/install/resolvers/folder_resolver.zig @@ -171,7 +171,7 @@ pub const FolderResolution = union(Tag) { defer Npm.Registry.BodyPool.release(body); const source = brk: { - var file = bun.sys.File.from(try bun.sys.openatA(bun.toFD(std.fs.cwd().fd), abs, std.os.O.RDONLY, 0).unwrap()); + var file = bun.sys.File.from(try bun.sys.openatA(bun.FD.cwd(), abs, std.os.O.RDONLY, 0).unwrap()); defer file.close(); {