authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-08 13:46:29-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 22:15:08-08:00
log264d714321d3e5f1f189af393e1fb24d101a7e91
treecb637a9c8c8565790db5b8cbcd1e960f88523da1
parentf53248a40936ebc9aaf75ddbd16e67ebec05ab84

update all openDir() sites to accept io instance


21 files changed, 107 insertions(+), 92 deletions(-)

lib/compiler/aro/aro/Driver/Filesystem.zig+2-2
......@@ -223,9 +223,9 @@ pub const Filesystem = union(enum) {
223223 };
224224 }
225225
226 pub fn openDir(fs: Filesystem, dir_name: []const u8) std.Io.Dir.OpenError!Dir {
226 pub fn openDir(fs: Filesystem, io: Io, dir_name: []const u8) std.Io.Dir.OpenError!Dir {
227227 return switch (fs) {
228 .real => |cwd| .{ .dir = try cwd.openDir(dir_name, .{ .access_sub_paths = false, .iterate = true }) },
228 .real => |cwd| .{ .dir = try cwd.openDir(io, dir_name, .{ .access_sub_paths = false, .iterate = true }) },
229229 .fake => |entries| .{ .fake = .{ .entries = entries, .path = dir_name } },
230230 };
231231 }
lib/compiler/aro/aro/Toolchain.zig+1-1
......@@ -509,7 +509,7 @@ pub fn addBuiltinIncludeDir(tc: *const Toolchain) !void {
509509 }
510510 var search_path = d.aro_name;
511511 while (std.fs.path.dirname(search_path)) |dirname| : (search_path = dirname) {
512 var base_dir = d.comp.cwd.openDir(dirname, .{}) catch continue;
512 var base_dir = d.comp.cwd.openDir(io, dirname, .{}) catch continue;
513513 defer base_dir.close(io);
514514
515515 base_dir.access("include/stddef.h", .{}) catch continue;
lib/compiler/resinator/compile.zig+7-7
......@@ -106,13 +106,13 @@ pub fn compile(allocator: Allocator, io: Io, source: []const u8, writer: *std.Io
106106 // If dirname returns null, then the root path will be the same as
107107 // the cwd so we don't need to add it as a distinct search path.
108108 if (std.fs.path.dirname(root_path)) |root_dir_path| {
109 var root_dir = try options.cwd.openDir(root_dir_path, .{});
109 var root_dir = try options.cwd.openDir(io, root_dir_path, .{});
110110 errdefer root_dir.close(io);
111111 try search_dirs.append(allocator, .{ .dir = root_dir, .path = try allocator.dupe(u8, root_dir_path) });
112112 }
113113 }
114114 // Re-open the passed in cwd since we want to be able to close it (Io.Dir.cwd() shouldn't be closed)
115 const cwd_dir = options.cwd.openDir(".", .{}) catch |err| {
115 const cwd_dir = options.cwd.openDir(io, ".", .{}) catch |err| {
116116 try options.diagnostics.append(.{
117117 .err = .failed_to_open_cwd,
118118 .token = .{
......@@ -132,7 +132,7 @@ pub fn compile(allocator: Allocator, io: Io, source: []const u8, writer: *std.Io
132132 };
133133 try search_dirs.append(allocator, .{ .dir = cwd_dir, .path = null });
134134 for (options.extra_include_paths) |extra_include_path| {
135 var dir = openSearchPathDir(options.cwd, extra_include_path) catch {
135 var dir = openSearchPathDir(options.cwd, io, extra_include_path) catch {
136136 // TODO: maybe a warning that the search path is skipped?
137137 continue;
138138 };
......@@ -140,7 +140,7 @@ pub fn compile(allocator: Allocator, io: Io, source: []const u8, writer: *std.Io
140140 try search_dirs.append(allocator, .{ .dir = dir, .path = try allocator.dupe(u8, extra_include_path) });
141141 }
142142 for (options.system_include_paths) |system_include_path| {
143 var dir = openSearchPathDir(options.cwd, system_include_path) catch {
143 var dir = openSearchPathDir(options.cwd, io, system_include_path) catch {
144144 // TODO: maybe a warning that the search path is skipped?
145145 continue;
146146 };
......@@ -159,7 +159,7 @@ pub fn compile(allocator: Allocator, io: Io, source: []const u8, writer: *std.Io
159159 };
160160 var it = std.mem.tokenizeScalar(u8, INCLUDE, delimiter);
161161 while (it.next()) |search_path| {
162 var dir = openSearchPathDir(options.cwd, search_path) catch continue;
162 var dir = openSearchPathDir(options.cwd, io, search_path) catch continue;
163163 errdefer dir.close(io);
164164 try search_dirs.append(allocator, .{ .dir = dir, .path = try allocator.dupe(u8, search_path) });
165165 }
......@@ -2896,11 +2896,11 @@ pub const Compiler = struct {
28962896
28972897pub const OpenSearchPathError = std.Io.Dir.OpenError;
28982898
2899fn openSearchPathDir(dir: std.Io.Dir, path: []const u8) OpenSearchPathError!std.Io.Dir {
2899fn openSearchPathDir(dir: std.Io.Dir, io: Io, path: []const u8) OpenSearchPathError!std.Io.Dir {
29002900 // Validate the search path to avoid possible unreachable on invalid paths,
29012901 // see https://github.com/ziglang/zig/issues/15607 for why this is currently necessary.
29022902 try validateSearchPath(path);
2903 return dir.openDir(path, .{});
2903 return dir.openDir(io, path, .{});
29042904}
29052905
29062906/// Very crude attempt at validating a path. This is imperfect
lib/compiler/std-docs.zig+2-2
......@@ -40,7 +40,7 @@ pub fn main() !void {
4040 const zig_exe_path = argv.next().?;
4141 const global_cache_path = argv.next().?;
4242
43 var lib_dir = try Io.Dir.cwd().openDir(zig_lib_directory, .{});
43 var lib_dir = try Io.Dir.cwd().openDir(io, zig_lib_directory, .{});
4444 defer lib_dir.close(io);
4545
4646 var listen_port: u16 = 0;
......@@ -206,7 +206,7 @@ fn serveSourcesTar(request: *std.http.Server.Request, context: *Context) !void {
206206 },
207207 });
208208
209 var std_dir = try context.lib_dir.openDir("std", .{ .iterate = true });
209 var std_dir = try context.lib_dir.openDir(io, "std", .{ .iterate = true });
210210 defer std_dir.close(io);
211211
212212 var walker = try std_dir.walk(gpa);
lib/std/Build.zig+2-1
......@@ -2184,6 +2184,7 @@ fn dependencyInner(
21842184 pkg_deps: AvailableDeps,
21852185 args: anytype,
21862186) *Dependency {
2187 const io = b.graph.io;
21872188 const user_input_options = userInputOptionsFromArgs(b.allocator, args);
21882189 if (b.graph.dependency_cache.getContext(.{
21892190 .build_root_string = build_root_string,
......@@ -2193,7 +2194,7 @@ fn dependencyInner(
21932194
21942195 const build_root: std.Build.Cache.Directory = .{
21952196 .path = build_root_string,
2196 .handle = Io.Dir.cwd().openDir(build_root_string, .{}) catch |err| {
2197 .handle = Io.Dir.cwd().openDir(io, build_root_string, .{}) catch |err| {
21972198 std.debug.print("unable to open '{s}': {s}\n", .{
21982199 build_root_string, @errorName(err),
21992200 });
lib/std/Build/Cache/Path.zig+2-1
......@@ -71,6 +71,7 @@ pub fn openFile(p: Path, io: Io, sub_path: []const u8, flags: Io.File.OpenFlags)
7171
7272pub fn openDir(
7373 p: Path,
74 io: Io,
7475 sub_path: []const u8,
7576 args: Io.Dir.OpenOptions,
7677) Io.Dir.OpenError!Io.Dir {
......@@ -80,7 +81,7 @@ pub fn openDir(
8081 p.sub_path, sub_path,
8182 }) catch return error.NameTooLong;
8283 };
83 return p.root_dir.handle.openDir(joined_path, args);
84 return p.root_dir.handle.openDir(io, joined_path, args);
8485}
8586
8687pub fn makeOpenPath(p: Path, sub_path: []const u8, opts: Io.Dir.OpenOptions) !Io.Dir {
lib/std/Build/Step/InstallArtifact.zig+1-1
......@@ -164,7 +164,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
164164 const src_dir_path = dir.source.getPath3(b, step);
165165 const full_h_prefix = b.getInstallPath(h_dir, dir.dest_rel_path);
166166
167 var src_dir = src_dir_path.root_dir.handle.openDir(src_dir_path.subPathOrDot(), .{ .iterate = true }) catch |err| {
167 var src_dir = src_dir_path.root_dir.handle.openDir(io, src_dir_path.subPathOrDot(), .{ .iterate = true }) catch |err| {
168168 return step.fail("unable to open source directory '{f}': {s}", .{
169169 src_dir_path, @errorName(err),
170170 });
lib/std/Build/Step/WriteFile.zig+1-1
......@@ -218,7 +218,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
218218 const need_derived_inputs = try step.addDirectoryWatchInput(dir.source);
219219 const src_dir_path = dir.source.getPath3(b, step);
220220
221 var src_dir = src_dir_path.root_dir.handle.openDir(src_dir_path.subPathOrDot(), .{ .iterate = true }) catch |err| {
221 var src_dir = src_dir_path.root_dir.handle.openDir(io, src_dir_path.subPathOrDot(), .{ .iterate = true }) catch |err| {
222222 return step.fail("unable to open source directory '{f}': {s}", .{
223223 src_dir_path, @errorName(err),
224224 });
lib/std/Io/Dir.zig+5-5
......@@ -234,7 +234,7 @@ pub const SelectiveWalker = struct {
234234 return;
235235 }
236236
237 var new_dir = entry.dir.openDir(entry.basename, .{ .iterate = true }) catch |err| {
237 var new_dir = entry.dir.openDir(io, entry.basename, .{ .iterate = true }) catch |err| {
238238 switch (err) {
239239 error.NameTooLong => unreachable,
240240 else => |e| return e,
......@@ -1326,7 +1326,7 @@ pub fn deleteTree(dir: Dir, io: Io, sub_path: []const u8) DeleteTreeError!void {
13261326 var treat_as_dir = true;
13271327 handle_entry: while (true) {
13281328 if (treat_as_dir) {
1329 break :iterable_dir parent_dir.openDir(name, .{
1329 break :iterable_dir parent_dir.openDir(io, name, .{
13301330 .follow_symlinks = false,
13311331 .iterate = true,
13321332 }) catch |err| switch (err) {
......@@ -1430,7 +1430,7 @@ fn deleteTreeMinStackSizeWithKindHint(parent: Dir, io: Io, sub_path: []const u8,
14301430 var treat_as_dir = entry.kind == .directory;
14311431 handle_entry: while (true) {
14321432 if (treat_as_dir) {
1433 const new_dir = dir.openDir(entry.name, .{
1433 const new_dir = dir.openDir(io, entry.name, .{
14341434 .follow_symlinks = false,
14351435 .iterate = true,
14361436 }) catch |err| switch (err) {
......@@ -1520,14 +1520,14 @@ fn deleteTreeMinStackSizeWithKindHint(parent: Dir, io: Io, sub_path: []const u8,
15201520}
15211521
15221522/// On successful delete, returns null.
1523fn deleteTreeOpenInitialSubpath(dir: Dir, sub_path: []const u8, kind_hint: File.Kind) !?Dir {
1523fn deleteTreeOpenInitialSubpath(dir: Dir, io: Io, sub_path: []const u8, kind_hint: File.Kind) !?Dir {
15241524 return iterable_dir: {
15251525 // Treat as a file by default
15261526 var treat_as_dir = kind_hint == .directory;
15271527
15281528 handle_entry: while (true) {
15291529 if (treat_as_dir) {
1530 break :iterable_dir dir.openDir(sub_path, .{
1530 break :iterable_dir dir.openDir(io, sub_path, .{
15311531 .follow_symlinks = false,
15321532 .iterate = true,
15331533 }) catch |err| switch (err) {
lib/std/crypto/Certificate/Bundle.zig+1-1
......@@ -180,7 +180,7 @@ pub fn addCertsFromDirPath(
180180 dir: Io.Dir,
181181 sub_dir_path: []const u8,
182182) AddCertsFromDirPathError!void {
183 var iterable_dir = try dir.openDir(sub_dir_path, .{ .iterate = true });
183 var iterable_dir = try dir.openDir(io, sub_dir_path, .{ .iterate = true });
184184 defer iterable_dir.close(io);
185185 return addCertsFromDir(cb, gpa, io, iterable_dir);
186186}
lib/std/crypto/codecs/asn1/test.zig+1-1
......@@ -73,7 +73,7 @@ test AllTypes {
7373 try std.testing.expectEqualSlices(u8, encoded, buf);
7474
7575 // Use this to update test file.
76 // const dir = try Io.Dir.cwd().openDir("lib/std/crypto/asn1", .{});
76 // const dir = try Io.Dir.cwd().openDir(io, "lib/std/crypto/asn1", .{});
7777 // var file = try dir.createFile(io, path, .{});
7878 // defer file.close(io);
7979 // try file.writeAll(buf);
lib/std/dynamic_library.zig+3-3
......@@ -160,9 +160,9 @@ pub const ElfDynLib = struct {
160160 fn openPath(path: []const u8, io: Io) !Io.Dir {
161161 if (path.len == 0) return error.NotDir;
162162 var parts = std.mem.tokenizeScalar(u8, path, '/');
163 var parent = if (path[0] == '/') try Io.Dir.cwd().openDir("/", .{}) else Io.Dir.cwd();
163 var parent = if (path[0] == '/') try Io.Dir.cwd().openDir(io, "/", .{}) else Io.Dir.cwd();
164164 while (parts.next()) |part| {
165 const child = try parent.openDir(part, .{});
165 const child = try parent.openDir(io, part, .{});
166166 parent.close(io);
167167 parent = child;
168168 }
......@@ -184,7 +184,7 @@ pub const ElfDynLib = struct {
184184 }
185185
186186 fn resolveFromParent(io: Io, dir_path: []const u8, file_name: []const u8) ?posix.fd_t {
187 var dir = Io.Dir.cwd().openDir(dir_path, .{}) catch return null;
187 var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch return null;
188188 defer dir.close(io);
189189 return posix.openat(dir.handle, file_name, .{
190190 .ACCMODE = .RDONLY,
lib/std/fs/test.zig+28-26
......@@ -367,7 +367,7 @@ test "openDir" {
367367
368368 for ([_][]const u8{ "", ".", ".." }) |sub_path| {
369369 const dir_path = try fs.path.join(allocator, &.{ subdir_path, sub_path });
370 var dir = try ctx.dir.openDir(dir_path, .{});
370 var dir = try ctx.dir.openDir(io, dir_path, .{});
371371 defer dir.close(io);
372372 }
373373 }
......@@ -448,7 +448,7 @@ test "openDirAbsolute" {
448448test "openDir cwd parent '..'" {
449449 const io = testing.io;
450450
451 var dir = Io.Dir.cwd().openDir("..", .{}) catch |err| {
451 var dir = Io.Dir.cwd().openDir(io, "..", .{}) catch |err| {
452452 if (native_os == .wasi and err == error.PermissionDenied) {
453453 return; // This is okay. WASI disallows escaping from the fs sandbox
454454 }
......@@ -471,7 +471,7 @@ test "openDir non-cwd parent '..'" {
471471 var subdir = try tmp.dir.makeOpenPath("subdir", .{});
472472 defer subdir.close(io);
473473
474 var dir = try subdir.openDir("..", .{});
474 var dir = try subdir.openDir(io, "..", .{});
475475 defer dir.close(io);
476476
477477 const expected_path = try tmp.dir.realpathAlloc(testing.allocator, ".");
......@@ -839,7 +839,7 @@ test "directory operations on files" {
839839 file.close(io);
840840
841841 try testing.expectError(error.PathAlreadyExists, ctx.dir.makeDir(test_file_name));
842 try testing.expectError(error.NotDir, ctx.dir.openDir(test_file_name, .{}));
842 try testing.expectError(error.NotDir, ctx.dir.openDir(io, test_file_name, .{}));
843843 try testing.expectError(error.NotDir, ctx.dir.deleteDir(test_file_name));
844844
845845 if (ctx.path_type == .absolute and comptime PathType.absolute.isSupported(builtin.os)) {
......@@ -902,7 +902,7 @@ test "file operations on directories" {
902902 }
903903
904904 // ensure the directory still exists as a sanity check
905 var dir = try ctx.dir.openDir(test_dir_name, .{});
905 var dir = try ctx.dir.openDir(io, test_dir_name, .{});
906906 dir.close(io);
907907 }
908908 }.impl);
......@@ -918,7 +918,7 @@ test "makeOpenPath parent dirs do not exist" {
918918 dir.close(io);
919919
920920 // double check that the full directory structure was created
921 var dir_verification = try tmp_dir.dir.openDir("root_dir/parent_dir/some_dir", .{});
921 var dir_verification = try tmp_dir.dir.openDir(io, "root_dir/parent_dir/some_dir", .{});
922922 dir_verification.close(io);
923923}
924924
......@@ -1005,8 +1005,8 @@ test "Dir.rename directories" {
10051005 try ctx.dir.rename(test_dir_path, test_dir_renamed_path);
10061006
10071007 // Ensure the directory was renamed
1008 try testing.expectError(error.FileNotFound, ctx.dir.openDir(test_dir_path, .{}));
1009 var dir = try ctx.dir.openDir(test_dir_renamed_path, .{});
1008 try testing.expectError(error.FileNotFound, ctx.dir.openDir(io, test_dir_path, .{}));
1009 var dir = try ctx.dir.openDir(io, test_dir_renamed_path, .{});
10101010
10111011 // Put a file in the directory
10121012 var file = try dir.createFile(io, "test_file", .{ .read = true });
......@@ -1017,8 +1017,8 @@ test "Dir.rename directories" {
10171017 try ctx.dir.rename(test_dir_renamed_path, test_dir_renamed_again_path);
10181018
10191019 // Ensure the directory was renamed and the file still exists in it
1020 try testing.expectError(error.FileNotFound, ctx.dir.openDir(test_dir_renamed_path, .{}));
1021 dir = try ctx.dir.openDir(test_dir_renamed_again_path, .{});
1020 try testing.expectError(error.FileNotFound, ctx.dir.openDir(io, test_dir_renamed_path, .{}));
1021 dir = try ctx.dir.openDir(io, test_dir_renamed_again_path, .{});
10221022 file = try dir.openFile(io, "test_file", .{});
10231023 file.close(io);
10241024 dir.close(io);
......@@ -1042,8 +1042,8 @@ test "Dir.rename directory onto empty dir" {
10421042 try ctx.dir.rename(test_dir_path, target_dir_path);
10431043
10441044 // Ensure the directory was renamed
1045 try testing.expectError(error.FileNotFound, ctx.dir.openDir(test_dir_path, .{}));
1046 var dir = try ctx.dir.openDir(target_dir_path, .{});
1045 try testing.expectError(error.FileNotFound, ctx.dir.openDir(io, test_dir_path, .{}));
1046 var dir = try ctx.dir.openDir(io, target_dir_path, .{});
10471047 dir.close(io);
10481048 }
10491049 }.impl);
......@@ -1070,7 +1070,7 @@ test "Dir.rename directory onto non-empty dir" {
10701070 try testing.expectError(error.PathAlreadyExists, ctx.dir.rename(test_dir_path, target_dir_path));
10711071
10721072 // Ensure the directory was not renamed
1073 var dir = try ctx.dir.openDir(test_dir_path, .{});
1073 var dir = try ctx.dir.openDir(io, test_dir_path, .{});
10741074 dir.close(io);
10751075 }
10761076 }.impl);
......@@ -1165,8 +1165,8 @@ test "renameAbsolute" {
11651165 );
11661166
11671167 // ensure the directory was renamed
1168 try testing.expectError(error.FileNotFound, tmp_dir.dir.openDir(test_dir_name, .{}));
1169 var dir = try tmp_dir.dir.openDir(renamed_test_dir_name, .{});
1168 try testing.expectError(error.FileNotFound, tmp_dir.dir.openDir(io, test_dir_name, .{}));
1169 var dir = try tmp_dir.dir.openDir(io, renamed_test_dir_name, .{});
11701170 dir.close(io);
11711171}
11721172
......@@ -1234,6 +1234,7 @@ test "deleteTree on a symlink" {
12341234test "makePath, put some files in it, deleteTree" {
12351235 try testWithAllSupportedPathTypes(struct {
12361236 fn impl(ctx: *TestContext) !void {
1237 const io = ctx.io;
12371238 const allocator = ctx.arena.allocator();
12381239 const dir_path = try ctx.transformPath("os_test_tmp");
12391240
......@@ -1248,7 +1249,7 @@ test "makePath, put some files in it, deleteTree" {
12481249 });
12491250
12501251 try ctx.dir.deleteTree(dir_path);
1251 try testing.expectError(error.FileNotFound, ctx.dir.openDir(dir_path, .{}));
1252 try testing.expectError(error.FileNotFound, ctx.dir.openDir(io, dir_path, .{}));
12521253 }
12531254 }.impl);
12541255}
......@@ -1256,6 +1257,7 @@ test "makePath, put some files in it, deleteTree" {
12561257test "makePath, put some files in it, deleteTreeMinStackSize" {
12571258 try testWithAllSupportedPathTypes(struct {
12581259 fn impl(ctx: *TestContext) !void {
1260 const io = ctx.io;
12591261 const allocator = ctx.arena.allocator();
12601262 const dir_path = try ctx.transformPath("os_test_tmp");
12611263
......@@ -1270,7 +1272,7 @@ test "makePath, put some files in it, deleteTreeMinStackSize" {
12701272 });
12711273
12721274 try ctx.dir.deleteTreeMinStackSize(dir_path);
1273 try testing.expectError(error.FileNotFound, ctx.dir.openDir(dir_path, .{}));
1275 try testing.expectError(error.FileNotFound, ctx.dir.openDir(io, dir_path, .{}));
12741276 }
12751277 }.impl);
12761278}
......@@ -1296,7 +1298,7 @@ test "makePath but sub_path contains pre-existing file" {
12961298}
12971299
12981300fn expectDir(io: Io, dir: Dir, path: []const u8) !void {
1299 var d = try dir.openDir(path, .{});
1301 var d = try dir.openDir(io, path, .{});
13001302 d.close(io);
13011303}
13021304
......@@ -1307,7 +1309,7 @@ test "makepath existing directories" {
13071309 defer tmp.cleanup();
13081310
13091311 try tmp.dir.makeDir("A");
1310 var tmpA = try tmp.dir.openDir("A", .{});
1312 var tmpA = try tmp.dir.openDir(io, "A", .{});
13111313 defer tmpA.close(io);
13121314 try tmpA.makeDir("B");
13131315
......@@ -1569,7 +1571,7 @@ test "sendfile" {
15691571
15701572 try tmp.dir.makePath("os_test_tmp");
15711573
1572 var dir = try tmp.dir.openDir("os_test_tmp", .{});
1574 var dir = try tmp.dir.openDir(io, "os_test_tmp", .{});
15731575 defer dir.close(io);
15741576
15751577 const line1 = "line1\n";
......@@ -1616,7 +1618,7 @@ test "sendfile with buffered data" {
16161618
16171619 try tmp.dir.makePath("os_test_tmp");
16181620
1619 var dir = try tmp.dir.openDir("os_test_tmp", .{});
1621 var dir = try tmp.dir.openDir(io, "os_test_tmp", .{});
16201622 defer dir.close(io);
16211623
16221624 var src_file = try dir.createFile(io, "sendfile1.txt", .{ .read = true });
......@@ -1913,7 +1915,7 @@ test "walker" {
19131915 return err;
19141916 };
19151917 // make sure that the entry.dir is the containing dir
1916 var entry_dir = try entry.dir.openDir(entry.basename, .{});
1918 var entry_dir = try entry.dir.openDir(io, entry.basename, .{});
19171919 defer entry_dir.close(io);
19181920 num_walked += 1;
19191921 }
......@@ -1981,7 +1983,7 @@ test "selective walker, skip entries that start with ." {
19811983 };
19821984
19831985 // make sure that the entry.dir is the containing dir
1984 var entry_dir = try entry.dir.openDir(entry.basename, .{});
1986 var entry_dir = try entry.dir.openDir(io, entry.basename, .{});
19851987 defer entry_dir.close(io);
19861988 num_walked += 1;
19871989 }
......@@ -2026,7 +2028,7 @@ test "'.' and '..' in Io.Dir functions" {
20262028
20272029 try ctx.dir.makeDir(subdir_path);
20282030 try ctx.dir.access(subdir_path, .{});
2029 var created_subdir = try ctx.dir.openDir(subdir_path, .{});
2031 var created_subdir = try ctx.dir.openDir(io, subdir_path, .{});
20302032 created_subdir.close(io);
20312033
20322034 const created_file = try ctx.dir.createFile(io, file_path, .{});
......@@ -2103,7 +2105,7 @@ test "chmod" {
21032105 try testing.expectEqual(@as(File.Mode, 0o644), (try file.stat()).mode & 0o7777);
21042106
21052107 try tmp.dir.makeDir("test_dir");
2106 var dir = try tmp.dir.openDir("test_dir", .{ .iterate = true });
2108 var dir = try tmp.dir.openDir(io, "test_dir", .{ .iterate = true });
21072109 defer dir.close(io);
21082110
21092111 try dir.chmod(0o700);
......@@ -2125,7 +2127,7 @@ test "chown" {
21252127
21262128 try tmp.dir.makeDir("test_dir");
21272129
2128 var dir = try tmp.dir.openDir("test_dir", .{ .iterate = true });
2130 var dir = try tmp.dir.openDir(io, "test_dir", .{ .iterate = true });
21292131 defer dir.close(io);
21302132 try dir.chown(null, null);
21312133}
lib/std/os/linux/IoUring.zig+3-1
......@@ -3066,6 +3066,8 @@ test "unlinkat" {
30663066test "mkdirat" {
30673067 if (!is_linux) return error.SkipZigTest;
30683068
3069 const io = testing.io;
3070
30693071 var ring = IoUring.init(1, 0) catch |err| switch (err) {
30703072 error.SystemOutdated => return error.SkipZigTest,
30713073 error.PermissionDenied => return error.SkipZigTest,
......@@ -3104,7 +3106,7 @@ test "mkdirat" {
31043106 }, cqe);
31053107
31063108 // Validate that the directory exist
3107 _ = try tmp.dir.openDir(path, .{});
3109 _ = try tmp.dir.openDir(io, path, .{});
31083110}
31093111
31103112test "symlinkat" {
lib/std/zig/LibCInstallation.zig+5-5
......@@ -337,7 +337,7 @@ fn findNativeIncludeDirPosix(self: *LibCInstallation, args: FindNativeOptions) F
337337 // search in reverse order
338338 const search_path_untrimmed = search_paths.items[search_paths.items.len - path_i - 1];
339339 const search_path = std.mem.trimStart(u8, search_path_untrimmed, " ");
340 var search_dir = Io.Dir.cwd().openDir(search_path, .{}) catch |err| switch (err) {
340 var search_dir = Io.Dir.cwd().openDir(io, search_path, .{}) catch |err| switch (err) {
341341 error.FileNotFound,
342342 error.NotDir,
343343 error.NoDevice,
......@@ -392,7 +392,7 @@ fn findNativeIncludeDirWindows(
392392 result_buf.shrinkAndFree(0);
393393 try result_buf.print("{s}\\Include\\{s}\\ucrt", .{ install.path, install.version });
394394
395 var dir = Io.Dir.cwd().openDir(result_buf.items, .{}) catch |err| switch (err) {
395 var dir = Io.Dir.cwd().openDir(io, result_buf.items, .{}) catch |err| switch (err) {
396396 error.FileNotFound,
397397 error.NotDir,
398398 error.NoDevice,
......@@ -440,7 +440,7 @@ fn findNativeCrtDirWindows(
440440 result_buf.shrinkAndFree(0);
441441 try result_buf.print("{s}\\Lib\\{s}\\ucrt\\{s}", .{ install.path, install.version, arch_sub_dir });
442442
443 var dir = Io.Dir.cwd().openDir(result_buf.items, .{}) catch |err| switch (err) {
443 var dir = Io.Dir.cwd().openDir(io, result_buf.items, .{}) catch |err| switch (err) {
444444 error.FileNotFound,
445445 error.NotDir,
446446 error.NoDevice,
......@@ -508,7 +508,7 @@ fn findNativeKernel32LibDir(
508508 result_buf.shrinkAndFree(0);
509509 try result_buf.print("{s}\\Lib\\{s}\\um\\{s}", .{ install.path, install.version, arch_sub_dir });
510510
511 var dir = Io.Dir.cwd().openDir(result_buf.items, .{}) catch |err| switch (err) {
511 var dir = Io.Dir.cwd().openDir(io, result_buf.items, .{}) catch |err| switch (err) {
512512 error.FileNotFound,
513513 error.NotDir,
514514 error.NoDevice,
......@@ -544,7 +544,7 @@ fn findNativeMsvcIncludeDir(
544544 const dir_path = try fs.path.join(allocator, &[_][]const u8{ up2, "include" });
545545 errdefer allocator.free(dir_path);
546546
547 var dir = Io.Dir.cwd().openDir(dir_path, .{}) catch |err| switch (err) {
547 var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch |err| switch (err) {
548548 error.FileNotFound,
549549 error.NotDir,
550550 error.NoDevice,
src/Compilation.zig+15-8
......@@ -745,6 +745,7 @@ pub const Directories = struct {
745745 /// Uses `std.process.fatal` on error conditions.
746746 pub fn init(
747747 arena: Allocator,
748 io: Io,
748749 override_zig_lib: ?[]const u8,
749750 override_global_cache: ?[]const u8,
750751 local_cache_strat: union(enum) {
......@@ -768,7 +769,7 @@ pub const Directories = struct {
768769 };
769770
770771 const zig_lib: Cache.Directory = d: {
771 if (override_zig_lib) |path| break :d openUnresolved(arena, cwd, path, .@"zig lib");
772 if (override_zig_lib) |path| break :d openUnresolved(arena, io, cwd, path, .@"zig lib");
772773 if (wasi) break :d openWasiPreopen(wasi_preopens, "/lib");
773774 break :d introspect.findZigLibDirFromSelfExe(arena, cwd, self_exe_path) catch |err| {
774775 fatal("unable to find zig installation directory '{s}': {s}", .{ self_exe_path, @errorName(err) });
......@@ -776,22 +777,22 @@ pub const Directories = struct {
776777 };
777778
778779 const global_cache: Cache.Directory = d: {
779 if (override_global_cache) |path| break :d openUnresolved(arena, cwd, path, .@"global cache");
780 if (override_global_cache) |path| break :d openUnresolved(arena, io, cwd, path, .@"global cache");
780781 if (wasi) break :d openWasiPreopen(wasi_preopens, "/cache");
781782 const path = introspect.resolveGlobalCacheDir(arena) catch |err| {
782783 fatal("unable to resolve zig cache directory: {s}", .{@errorName(err)});
783784 };
784 break :d openUnresolved(arena, cwd, path, .@"global cache");
785 break :d openUnresolved(arena, io, cwd, path, .@"global cache");
785786 };
786787
787788 const local_cache: Cache.Directory = switch (local_cache_strat) {
788 .override => |path| openUnresolved(arena, cwd, path, .@"local cache"),
789 .override => |path| openUnresolved(arena, io, cwd, path, .@"local cache"),
789790 .search => d: {
790791 const maybe_path = introspect.resolveSuitableLocalCacheDir(arena, cwd) catch |err| {
791792 fatal("unable to resolve zig cache directory: {s}", .{@errorName(err)});
792793 };
793794 const path = maybe_path orelse break :d global_cache;
794 break :d openUnresolved(arena, cwd, path, .@"local cache");
795 break :d openUnresolved(arena, io, cwd, path, .@"local cache");
795796 },
796797 .global => global_cache,
797798 };
......@@ -818,13 +819,19 @@ pub const Directories = struct {
818819 },
819820 };
820821 }
821 fn openUnresolved(arena: Allocator, cwd: []const u8, unresolved_path: []const u8, thing: enum { @"zig lib", @"global cache", @"local cache" }) Cache.Directory {
822 fn openUnresolved(
823 arena: Allocator,
824 io: Io,
825 cwd: []const u8,
826 unresolved_path: []const u8,
827 thing: enum { @"zig lib", @"global cache", @"local cache" },
828 ) Cache.Directory {
822829 const path = introspect.resolvePath(arena, cwd, &.{unresolved_path}) catch |err| {
823830 fatal("unable to resolve {s} directory: {s}", .{ @tagName(thing), @errorName(err) });
824831 };
825832 const nonempty_path = if (path.len == 0) "." else path;
826833 const handle_or_err = switch (thing) {
827 .@"zig lib" => Io.Dir.cwd().openDir(nonempty_path, .{}),
834 .@"zig lib" => Io.Dir.cwd().openDir(io, nonempty_path, .{}),
828835 .@"global cache", .@"local cache" => Io.Dir.cwd().makeOpenPath(nonempty_path, .{}),
829836 };
830837 return .{
......@@ -5331,7 +5338,7 @@ fn docsCopyModule(
53315338 const root = module.root;
53325339 var mod_dir = d: {
53335340 const root_dir, const sub_path = root.openInfo(comp.dirs);
5334 break :d root_dir.openDir(sub_path, .{ .iterate = true });
5341 break :d root_dir.openDir(io, sub_path, .{ .iterate = true });
53355342 } catch |err| {
53365343 return comp.lockAndSetMiscFailure(.docs_copy, "unable to open directory '{f}': {t}", .{ root.fmt(comp), err });
53375344 };
src/Package/Fetch.zig+3-2
......@@ -383,7 +383,7 @@ pub fn run(f: *Fetch) RunError!void {
383383 },
384384 .remote => |remote| remote,
385385 .path_or_url => |path_or_url| {
386 if (Io.Dir.cwd().openDir(path_or_url, .{ .iterate = true })) |dir| {
386 if (Io.Dir.cwd().openDir(io, path_or_url, .{ .iterate = true })) |dir| {
387387 var resource: Resource = .{ .dir = dir };
388388 return f.runResource(path_or_url, &resource, null);
389389 } else |dir_err| {
......@@ -2311,8 +2311,9 @@ const TestFetchBuilder = struct {
23112311 }
23122312
23132313 fn packageDir(self: *TestFetchBuilder) !Io.Dir {
2314 const io = self.job_queue.io;
23142315 const root = self.fetch.package_root;
2315 return try root.root_dir.handle.openDir(root.sub_path, .{ .iterate = true });
2316 return try root.root_dir.handle.openDir(io, root.sub_path, .{ .iterate = true });
23162317 }
23172318
23182319 // Test helper, asserts thet package dir constains expected_files.
src/Package/Fetch/git.zig+1-1
......@@ -254,7 +254,7 @@ pub const Repository = struct {
254254 switch (entry.type) {
255255 .directory => {
256256 try dir.makeDir(entry.name);
257 var subdir = try dir.openDir(entry.name, .{});
257 var subdir = try dir.openDir(io, entry.name, .{});
258258 defer subdir.close(io);
259259 const sub_path = try std.fs.path.join(repository.odb.allocator, &.{ current_path, entry.name });
260260 defer repository.odb.allocator.free(sub_path);
src/fmt.zig+2-2
......@@ -186,7 +186,7 @@ pub fn run(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !
186186 error.FileNotFound => continue,
187187 // On Windows, statFile does not work for directories
188188 error.IsDir => dir: {
189 var dir = try Io.Dir.cwd().openDir(file_path, .{});
189 var dir = try Io.Dir.cwd().openDir(io, file_path, .{});
190190 defer dir.close(io);
191191 break :dir try dir.stat();
192192 },
......@@ -224,7 +224,7 @@ fn fmtPathDir(
224224) !void {
225225 const io = fmt.io;
226226
227 var dir = try parent_dir.openDir(parent_sub_path, .{ .iterate = true });
227 var dir = try parent_dir.openDir(io, parent_sub_path, .{ .iterate = true });
228228 defer dir.close(io);
229229
230230 const stat = try dir.stat();
src/introspect.zig+3-3
......@@ -21,7 +21,7 @@ fn testZigInstallPrefix(io: Io, base_dir: Io.Dir) ?Cache.Directory {
2121 zig_dir: {
2222 // Try lib/zig/std/std.zig
2323 const lib_zig = "lib" ++ fs.path.sep_str ++ "zig";
24 var test_zig_dir = base_dir.openDir(lib_zig, .{}) catch break :zig_dir;
24 var test_zig_dir = base_dir.openDir(io, lib_zig, .{}) catch break :zig_dir;
2525 const file = test_zig_dir.openFile(io, test_index_file, .{}) catch {
2626 test_zig_dir.close(io);
2727 break :zig_dir;
......@@ -31,7 +31,7 @@ fn testZigInstallPrefix(io: Io, base_dir: Io.Dir) ?Cache.Directory {
3131 }
3232
3333 // Try lib/std/std.zig
34 var test_zig_dir = base_dir.openDir("lib", .{}) catch return null;
34 var test_zig_dir = base_dir.openDir(io, "lib", .{}) catch return null;
3535 const file = test_zig_dir.openFile(io, test_index_file, .{}) catch {
3636 test_zig_dir.close(io);
3737 return null;
......@@ -85,7 +85,7 @@ pub fn findZigLibDirFromSelfExe(
8585 const cwd = Io.Dir.cwd();
8686 var cur_path: []const u8 = self_exe_path;
8787 while (fs.path.dirname(cur_path)) |dirname| : (cur_path = dirname) {
88 var base_dir = cwd.openDir(dirname, .{}) catch continue;
88 var base_dir = cwd.openDir(io, dirname, .{}) catch continue;
8989 defer base_dir.close(io);
9090
9191 const sub_directory = testZigInstallPrefix(io, base_dir) orelse continue;
src/main.zig+19-18
......@@ -713,7 +713,7 @@ const Emit = union(enum) {
713713 } else e: {
714714 // If there's a dirname, check that dir exists. This will give a more descriptive error than `Compilation` otherwise would.
715715 if (fs.path.dirname(path)) |dir_path| {
716 var dir = Io.Dir.cwd().openDir(dir_path, .{}) catch |err| {
716 var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch |err| {
717717 fatal("unable to open output directory '{s}': {s}", .{ dir_path, @errorName(err) });
718718 };
719719 dir.close(io);
......@@ -3304,7 +3304,7 @@ fn buildOutputType(
33043304 } else emit: {
33053305 // If there's a dirname, check that dir exists. This will give a more descriptive error than `Compilation` otherwise would.
33063306 if (fs.path.dirname(path)) |dir_path| {
3307 var dir = Io.Dir.cwd().openDir(dir_path, .{}) catch |err| {
3307 var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch |err| {
33083308 fatal("unable to open output directory '{s}': {s}", .{ dir_path, @errorName(err) });
33093309 };
33103310 dir.close(io);
......@@ -3959,14 +3959,14 @@ fn createModule(
39593959 if (fs.path.isAbsolute(lib_dir_arg)) {
39603960 const stripped_dir = lib_dir_arg[fs.path.parsePath(lib_dir_arg).root.len..];
39613961 const full_path = try fs.path.join(arena, &[_][]const u8{ root, stripped_dir });
3962 addLibDirectoryWarn(&create_module.lib_directories, full_path);
3962 addLibDirectoryWarn(io, &create_module.lib_directories, full_path);
39633963 } else {
3964 addLibDirectoryWarn(&create_module.lib_directories, lib_dir_arg);
3964 addLibDirectoryWarn(io, &create_module.lib_directories, lib_dir_arg);
39653965 }
39663966 }
39673967 } else {
39683968 for (create_module.lib_dir_args.items) |lib_dir_arg| {
3969 addLibDirectoryWarn(&create_module.lib_directories, lib_dir_arg);
3969 addLibDirectoryWarn(io, &create_module.lib_directories, lib_dir_arg);
39703970 }
39713971 }
39723972 create_module.lib_dir_args = undefined; // From here we use lib_directories instead.
......@@ -4002,7 +4002,7 @@ fn createModule(
40024002 try create_module.rpath_list.appendSlice(arena, paths.rpaths.items);
40034003
40044004 try create_module.lib_directories.ensureUnusedCapacity(arena, paths.lib_dirs.items.len);
4005 for (paths.lib_dirs.items) |path| addLibDirectoryWarn2(&create_module.lib_directories, path, true);
4005 for (paths.lib_dirs.items) |path| addLibDirectoryWarn2(io, &create_module.lib_directories, path, true);
40064006 }
40074007
40084008 if (create_module.libc_paths_file) |paths_file| {
......@@ -4026,8 +4026,8 @@ fn createModule(
40264026 };
40274027 }
40284028 try create_module.lib_directories.ensureUnusedCapacity(arena, 2);
4029 addLibDirectoryWarn(&create_module.lib_directories, create_module.libc_installation.?.msvc_lib_dir.?);
4030 addLibDirectoryWarn(&create_module.lib_directories, create_module.libc_installation.?.kernel32_lib_dir.?);
4029 addLibDirectoryWarn(io, &create_module.lib_directories, create_module.libc_installation.?.msvc_lib_dir.?);
4030 addLibDirectoryWarn(io, &create_module.lib_directories, create_module.libc_installation.?.kernel32_lib_dir.?);
40314031 }
40324032
40334033 // Destructively mutates but does not transfer ownership of `unresolved_link_inputs`.
......@@ -5118,7 +5118,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
51185118 process.raiseFileDescriptorLimit();
51195119
51205120 const cwd_path = try introspect.getResolvedCwd(arena);
5121 const build_root = try findBuildRoot(arena, .{
5121 const build_root = try findBuildRoot(arena, io, .{
51225122 .cwd_path = cwd_path,
51235123 .build_file = build_file,
51245124 });
......@@ -5227,7 +5227,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
52275227 if (system_pkg_dir_path) |p| {
52285228 job_queue.global_cache = .{
52295229 .path = p,
5230 .handle = Io.Dir.cwd().openDir(p, .{}) catch |err| {
5230 .handle = Io.Dir.cwd().openDir(io, p, .{}) catch |err| {
52315231 fatal("unable to open system package directory '{s}': {s}", .{
52325232 p, @errorName(err),
52335233 });
......@@ -7039,7 +7039,7 @@ fn cmdFetch(
70397039
70407040 const cwd_path = try introspect.getResolvedCwd(arena);
70417041
7042 var build_root = try findBuildRoot(arena, .{
7042 var build_root = try findBuildRoot(arena, io, .{
70437043 .cwd_path = cwd_path,
70447044 });
70457045 defer build_root.deinit();
......@@ -7251,7 +7251,7 @@ const FindBuildRootOptions = struct {
72517251 cwd_path: ?[]const u8 = null,
72527252};
72537253
7254fn findBuildRoot(arena: Allocator, options: FindBuildRootOptions) !BuildRoot {
7254fn findBuildRoot(arena: Allocator, io: Io, options: FindBuildRootOptions) !BuildRoot {
72557255 const cwd_path = options.cwd_path orelse try introspect.getResolvedCwd(arena);
72567256 const build_zig_basename = if (options.build_file) |bf|
72577257 fs.path.basename(bf)
......@@ -7260,7 +7260,7 @@ fn findBuildRoot(arena: Allocator, options: FindBuildRootOptions) !BuildRoot {
72607260
72617261 if (options.build_file) |bf| {
72627262 if (fs.path.dirname(bf)) |dirname| {
7263 const dir = Io.Dir.cwd().openDir(dirname, .{}) catch |err| {
7263 const dir = Io.Dir.cwd().openDir(io, dirname, .{}) catch |err| {
72647264 fatal("unable to open directory to build file from argument 'build-file', '{s}': {s}", .{ dirname, @errorName(err) });
72657265 };
72667266 return .{
......@@ -7281,7 +7281,7 @@ fn findBuildRoot(arena: Allocator, options: FindBuildRootOptions) !BuildRoot {
72817281 while (true) {
72827282 const joined_path = try fs.path.join(arena, &[_][]const u8{ dirname, build_zig_basename });
72837283 if (Io.Dir.cwd().access(joined_path, .{})) |_| {
7284 const dir = Io.Dir.cwd().openDir(dirname, .{}) catch |err| {
7284 const dir = Io.Dir.cwd().openDir(io, dirname, .{}) catch |err| {
72857285 fatal("unable to open directory while searching for build.zig file, '{s}': {s}", .{ dirname, @errorName(err) });
72867286 };
72877287 return .{
......@@ -7464,7 +7464,7 @@ fn findTemplates(gpa: Allocator, arena: Allocator, io: Io) Templates {
74647464
74657465 const s = fs.path.sep_str;
74667466 const template_sub_path = "init";
7467 const template_dir = zig_lib_directory.handle.openDir(template_sub_path, .{}) catch |err| {
7467 const template_dir = zig_lib_directory.handle.openDir(io, template_sub_path, .{}) catch |err| {
74687468 const path = zig_lib_directory.path orelse ".";
74697469 fatal("unable to open zig project template directory '{s}{s}{s}': {s}", .{
74707470 path, s, template_sub_path, @errorName(err),
......@@ -7581,17 +7581,18 @@ fn anyObjectLinkInputs(link_inputs: []const link.UnresolvedInput) bool {
75817581 return false;
75827582}
75837583
7584fn addLibDirectoryWarn(lib_directories: *std.ArrayList(Directory), path: []const u8) void {
7585 return addLibDirectoryWarn2(lib_directories, path, false);
7584fn addLibDirectoryWarn(io: Io, lib_directories: *std.ArrayList(Directory), path: []const u8) void {
7585 return addLibDirectoryWarn2(io, lib_directories, path, false);
75867586}
75877587
75887588fn addLibDirectoryWarn2(
7589 io: Io,
75897590 lib_directories: *std.ArrayList(Directory),
75907591 path: []const u8,
75917592 ignore_not_found: bool,
75927593) void {
75937594 lib_directories.appendAssumeCapacity(.{
7594 .handle = Io.Dir.cwd().openDir(path, .{}) catch |err| {
7595 .handle = Io.Dir.cwd().openDir(io, path, .{}) catch |err| {
75957596 if (err == error.FileNotFound and ignore_not_found) return;
75967597 warn("unable to open library directory '{s}': {s}", .{ path, @errorName(err) });
75977598 return;