diff --git a/lib/compiler/build_runner.zig b/lib/compiler/build_runner.zig index 7efee4bac4..6ef8e71f7e 100644 --- a/lib/compiler/build_runner.zig +++ b/lib/compiler/build_runner.zig @@ -83,7 +83,7 @@ pub fn main(init: process.Init.Minimal) !void { .io = io, .gpa = gpa, .manifest_dir = try local_cache_directory.handle.createDirPathOpen(io, "h", .{}), - .cwd = try process.getCwdAlloc(single_threaded_arena.allocator()), + .cwd = try process.currentDirAlloc(io, single_threaded_arena.allocator()), }, .zig_exe = zig_exe, .environ_map = try init.environ.createMap(arena), diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig index e35beca617..f595435749 100644 --- a/lib/std/Build/Cache.zig +++ b/lib/std/Build/Cache.zig @@ -1315,7 +1315,7 @@ test "cache file and then recall it" { var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); - const cwd = try std.process.getCwdAlloc(testing.allocator); + const cwd = try std.process.currentDirAlloc(io, testing.allocator); defer testing.allocator.free(cwd); const temp_file = "test.txt"; @@ -1383,7 +1383,7 @@ test "check that changing a file makes cache fail" { var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); - const cwd = try std.process.getCwdAlloc(testing.allocator); + const cwd = try std.process.currentDirAlloc(io, testing.allocator); defer testing.allocator.free(cwd); const temp_file = "cache_hash_change_file_test.txt"; @@ -1459,7 +1459,7 @@ test "no file inputs" { var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); - const cwd = try std.process.getCwdAlloc(testing.allocator); + const cwd = try std.process.currentDirAlloc(io, testing.allocator); defer testing.allocator.free(cwd); const temp_manifest_dir = "no_file_inputs_manifest_dir"; @@ -1509,7 +1509,7 @@ test "Manifest with files added after initial hash work" { var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); - const cwd = try std.process.getCwdAlloc(testing.allocator); + const cwd = try std.process.currentDirAlloc(io, testing.allocator); defer testing.allocator.free(cwd); const temp_file1 = "cache_hash_post_file_test1.txt"; diff --git a/lib/std/Build/Step/Options.zig b/lib/std/Build/Step/Options.zig index 6c316b35fc..adef048423 100644 --- a/lib/std/Build/Step/Options.zig +++ b/lib/std/Build/Step/Options.zig @@ -519,7 +519,7 @@ test Options { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); - const cwd = try std.process.getCwdAlloc(std.testing.allocator); + const cwd = try std.process.currentDirAlloc(io, std.testing.allocator); defer std.testing.allocator.free(cwd); var graph: std.Build.Graph = .{ diff --git a/lib/std/Io.zig b/lib/std/Io.zig index 80a0e24807..20c23e804e 100644 --- a/lib/std/Io.zig +++ b/lib/std/Io.zig @@ -665,6 +665,7 @@ pub const VTable = struct { lockStderr: *const fn (?*anyopaque, ?Terminal.Mode) Cancelable!LockedStderr, tryLockStderr: *const fn (?*anyopaque, ?Terminal.Mode) Cancelable!?LockedStderr, unlockStderr: *const fn (?*anyopaque) void, + processCurrentDir: *const fn (?*anyopaque, buffer: []u8) std.process.CurrentDirError!usize, processSetCurrentDir: *const fn (?*anyopaque, Dir) std.process.SetCurrentDirError!void, processReplace: *const fn (?*anyopaque, std.process.ReplaceOptions) std.process.ReplaceError, processReplacePath: *const fn (?*anyopaque, Dir, std.process.ReplaceOptions) std.process.ReplaceError, diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig index c74850ca0a..2690734e87 100644 --- a/lib/std/Io/Threaded.zig +++ b/lib/std/Io/Threaded.zig @@ -1643,6 +1643,7 @@ pub fn io(t: *Threaded) Io { .lockStderr = lockStderr, .tryLockStderr = tryLockStderr, .unlockStderr = unlockStderr, + .processCurrentDir = processCurrentDir, .processSetCurrentDir = processSetCurrentDir, .processReplace = processReplace, .processReplacePath = processReplacePath, @@ -1801,6 +1802,7 @@ pub fn ioBasic(t: *Threaded) Io { .lockStderr = lockStderr, .tryLockStderr = tryLockStderr, .unlockStderr = unlockStderr, + .processCurrentDir = processCurrentDir, .processSetCurrentDir = processSetCurrentDir, .processReplace = processReplace, .processReplacePath = processReplacePath, @@ -12600,6 +12602,46 @@ fn unlockStderr(userdata: ?*anyopaque) void { process.stderr_thread_mutex.unlock(); } +fn processCurrentDir(userdata: ?*anyopaque, buffer: []u8) process.CurrentDirError!usize { + const t: *Threaded = @ptrCast(@alignCast(userdata)); + _ = t; + if (is_windows) { + var wtf16le_buf: [windows.PATH_MAX_WIDE:0]u16 = undefined; + const n = windows.ntdll.RtlGetCurrentDirectory_U(wtf16le_buf.len + 1, &wtf16le_buf); + if (n == 0) return error.Unexpected; + assert(n <= wtf16le_buf.len); + const wtf16le_slice = wtf16le_buf[0..n]; + var end_index: usize = 0; + var it = std.unicode.Wtf16LeIterator.init(wtf16le_slice); + while (it.nextCodepoint()) |codepoint| { + const seq_len = std.unicode.utf8CodepointSequenceLength(codepoint) catch unreachable; + if (end_index + seq_len >= buffer.len) + return error.NameTooLong; + end_index += std.unicode.wtf8Encode(codepoint, buffer[end_index..]) catch unreachable; + } + return end_index; + } else if (native_os == .wasi and !builtin.link_libc) { + if (buffer.len == 0) return error.NameTooLong; + buffer[0] = '.'; + return 1; + } + + const err: posix.E = if (builtin.link_libc) err: { + const c_err = if (std.c.getcwd(buffer.ptr, buffer.len)) |_| 0 else std.c._errno().*; + break :err @enumFromInt(c_err); + } else err: { + break :err posix.errno(posix.system.getcwd(buffer.ptr, buffer.len)); + }; + switch (err) { + .SUCCESS => return std.mem.findScalar(u8, buffer, 0).?, + .NOENT => return error.CurrentWorkingDirectoryUnlinked, + .RANGE => return error.NameTooLong, + .FAULT => |e| return errnoBug(e), + .INVAL => |e| return errnoBug(e), + else => return posix.unexpectedErrno(err), + } +} + fn processSetCurrentDir(userdata: ?*anyopaque, dir: Dir) process.SetCurrentDirError!void { if (native_os == .wasi) return error.OperationUnsupported; const t: *Threaded = @ptrCast(@alignCast(userdata)); diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig index 4d7077bfc3..be59393e3d 100644 --- a/lib/std/fs/test.zig +++ b/lib/std/fs/test.zig @@ -1787,7 +1787,7 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" { const gpa = testing.allocator; - const cwd = try std.process.getCwdAlloc(gpa); + const cwd = try std.process.currentDirAlloc(io, gpa); defer gpa.free(cwd); const filename = try Dir.path.resolve(gpa, &.{ cwd, sub_path }); diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index 47c5a4d856..b9653b3ac9 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -2878,34 +2878,6 @@ pub fn CloseHandle(hObject: HANDLE) void { assert(ntdll.NtClose(hObject) == .SUCCESS); } -pub const GetCurrentDirectoryError = error{ - NameTooLong, - Unexpected, -}; - -/// The result is a slice of `buffer`, indexed from 0. -/// The result is encoded as [WTF-8](https://wtf-8.codeberg.page/). -pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 { - var wtf16le_buf: [PATH_MAX_WIDE:0]u16 = undefined; - const result = kernel32.GetCurrentDirectoryW(wtf16le_buf.len + 1, &wtf16le_buf); - if (result == 0) { - switch (GetLastError()) { - else => |err| return unexpectedError(err), - } - } - assert(result <= wtf16le_buf.len); - const wtf16le_slice = wtf16le_buf[0..result]; - var end_index: usize = 0; - var it = std.unicode.Wtf16LeIterator.init(wtf16le_slice); - while (it.nextCodepoint()) |codepoint| { - const seq_len = std.unicode.utf8CodepointSequenceLength(codepoint) catch unreachable; - if (end_index + seq_len >= buffer.len) - return error.NameTooLong; - end_index += std.unicode.wtf8Encode(codepoint, buffer[end_index..]) catch unreachable; - } - return buffer[0..end_index]; -} - pub const GetStdHandleError = error{ NoStandardHandleAttached, Unexpected, diff --git a/lib/std/os/windows/kernel32.zig b/lib/std/os/windows/kernel32.zig index ad232046ef..fe28e40cbb 100644 --- a/lib/std/os/windows/kernel32.zig +++ b/lib/std/os/windows/kernel32.zig @@ -128,12 +128,6 @@ pub extern "kernel32" fn SetFileCompletionNotificationModes( Flags: UCHAR, ) callconv(.winapi) BOOL; -// TODO: `RtlGetCurrentDirectory_U(nBufferLength * 2, lpBuffer)` -pub extern "kernel32" fn GetCurrentDirectoryW( - nBufferLength: DWORD, - lpBuffer: ?[*]WCHAR, -) callconv(.winapi) DWORD; - pub extern "kernel32" fn ReadFile( hFile: HANDLE, lpBuffer: LPVOID, diff --git a/lib/std/os/windows/ntdll.zig b/lib/std/os/windows/ntdll.zig index ee3cdeb069..52361ed982 100644 --- a/lib/std/os/windows/ntdll.zig +++ b/lib/std/os/windows/ntdll.zig @@ -496,6 +496,11 @@ pub extern "ntdll" fn RtlGetFullPathName_U( ShortName: ?*[*:0]const u16, ) callconv(.winapi) ULONG; +pub extern "ntdll" fn RtlGetCurrentDirectory_U( + BufferLength: ULONG, + Buffer: [*]u16, +) callconv(.winapi) ULONG; + pub extern "ntdll" fn RtlGetSystemTimePrecise() callconv(.winapi) LARGE_INTEGER; pub extern "ntdll" fn RtlInitializeCriticalSection( diff --git a/lib/std/posix.zig b/lib/std/posix.zig index a12182b406..6bcb181552 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -519,40 +519,6 @@ pub fn getppid() pid_t { return system.getppid(); } -pub const GetCwdError = error{ - NameTooLong, - /// Not possible on Windows. - CurrentWorkingDirectoryUnlinked, -} || UnexpectedError; - -/// The result is a slice of out_buffer, indexed from 0. -pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { - if (native_os == .windows) { - return windows.GetCurrentDirectory(out_buffer); - } else if (native_os == .wasi and !builtin.link_libc) { - const path = "."; - if (out_buffer.len < path.len) return error.NameTooLong; - const result = out_buffer[0..path.len]; - @memcpy(result, path); - return result; - } - - const err: E = if (builtin.link_libc) err: { - const c_err = if (std.c.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else std.c._errno().*; - break :err @enumFromInt(c_err); - } else err: { - break :err errno(system.getcwd(out_buffer.ptr, out_buffer.len)); - }; - switch (err) { - .SUCCESS => return mem.sliceTo(out_buffer, 0), - .FAULT => unreachable, - .INVAL => unreachable, - .NOENT => return error.CurrentWorkingDirectoryUnlinked, - .RANGE => return error.NameTooLong, - else => return unexpectedErrno(err), - } -} - pub const SocketError = error{ /// Permission to create a socket of the specified type and/or /// pro‐tocol is denied. diff --git a/lib/std/process.zig b/lib/std/process.zig index a3547e2b0f..dd3e07c1ea 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -63,53 +63,40 @@ pub const Init = struct { }; }; -pub const GetCwdError = posix.GetCwdError; - -/// The result is a slice of `out_buffer`, from index `0`. -/// On Windows, the result is encoded as [WTF-8](https://wtf-8.codeberg.page/). -/// On other platforms, the result is an opaque sequence of bytes with no particular encoding. -pub fn getCwd(out_buffer: []u8) GetCwdError![]u8 { - return posix.getcwd(out_buffer); -} - -// Same as GetCwdError, minus error.NameTooLong + Allocator.Error -pub const GetCwdAllocError = Allocator.Error || error{ - /// Not possible on Windows. +pub const CurrentDirError = error{ + NameTooLong, + /// Not possible on Windows. Always returned on WASI. CurrentWorkingDirectoryUnlinked, -} || posix.UnexpectedError; +} || Io.UnexpectedError; -/// Caller must free the returned memory. /// On Windows, the result is encoded as [WTF-8](https://wtf-8.codeberg.page/). -/// On other platforms, the result is an opaque sequence of bytes with no particular encoding. -pub fn getCwdAlloc(allocator: Allocator) GetCwdAllocError![]u8 { - // The use of max_path_bytes here is just a heuristic: most paths will fit - // in stack_buf, avoiding an extra allocation in the common case. - var stack_buf: [max_path_bytes]u8 = undefined; - var heap_buf: ?[]u8 = null; - defer if (heap_buf) |buf| allocator.free(buf); - - var current_buf: []u8 = &stack_buf; - while (true) { - if (posix.getcwd(current_buf)) |slice| { - return allocator.dupe(u8, slice); - } else |err| switch (err) { - error.NameTooLong => { - // The path is too long to fit in stack_buf. Allocate geometrically - // increasing buffers until we find one that works - const new_capacity = current_buf.len * 2; - if (heap_buf) |buf| allocator.free(buf); - current_buf = try allocator.alloc(u8, new_capacity); - heap_buf = current_buf; - }, - else => |e| return e, - } - } +/// On other platforms, the result is an opaque sequence of bytes with no +/// particular encoding. +pub fn currentDir(io: Io, buffer: []u8) CurrentDirError!usize { + return io.vtable.processCurrentDir(io.userdata, buffer); } -test getCwdAlloc { - if (native_os == .wasi) return error.SkipZigTest; +pub const CurrentDirAllocError = Allocator.Error || error{ + /// Not possible on Windows. Always returned on WASI. + CurrentWorkingDirectoryUnlinked, +} || Io.UnexpectedError; - const cwd = try getCwdAlloc(testing.allocator); +/// On Windows, the result is encoded as [WTF-8](https://wtf-8.codeberg.page/). +/// On other platforms, the result is an opaque sequence of bytes with no +/// particular encoding. +/// +/// Caller owns returned memory. +pub fn currentDirAlloc(io: Io, allocator: Allocator) CurrentDirAllocError![:0]u8 { + var buffer: [max_path_bytes]u8 = undefined; + const n = currentDir(io, &buffer) catch |err| switch (err) { + error.NameTooLong => unreachable, + else => |e| return e, + }; + return allocator.dupeZ(u8, buffer[0..n]); +} + +test currentDirAlloc { + const cwd = try currentDirAlloc(testing.io, testing.allocator); testing.allocator.free(cwd); } @@ -466,7 +453,7 @@ pub fn spawnPath(io: Io, dir: Io.Dir, options: SpawnOptions) SpawnError!Child { return io.vtable.processSpawnPath(io.userdata, dir, options); } -pub const RunError = posix.GetCwdError || posix.ReadError || SpawnError || posix.PollError || error{ +pub const RunError = CurrentDirError || posix.ReadError || SpawnError || posix.PollError || error{ StdoutStreamTooLong, StderrStreamTooLong, }; diff --git a/src/Compilation.zig b/src/Compilation.zig index 15c1837ec2..180968ac0c 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -767,7 +767,7 @@ pub const Directories = struct { ) Directories { const wasi = builtin.target.os.tag == .wasi; - const cwd = introspect.getResolvedCwd(arena) catch |err| { + const cwd = introspect.getResolvedCwd(io, arena) catch |err| { fatal("unable to get cwd: {t}", .{err}); }; diff --git a/src/introspect.zig b/src/introspect.zig index fa04e7de58..36e94e979f 100644 --- a/src/introspect.zig +++ b/src/introspect.zig @@ -43,7 +43,7 @@ fn testZigInstallPrefix(io: Io, base_dir: Io.Dir) ?Cache.Directory { /// Both the directory handle and the path are newly allocated resources which the caller now owns. pub fn findZigLibDir(gpa: Allocator, io: Io) !Cache.Directory { - const cwd_path = try getResolvedCwd(gpa); + const cwd_path = try getResolvedCwd(io, gpa); defer gpa.free(cwd_path); const self_exe_path = try std.process.executablePathAlloc(io, gpa); defer gpa.free(self_exe_path); @@ -51,23 +51,23 @@ pub fn findZigLibDir(gpa: Allocator, io: Io) !Cache.Directory { return findZigLibDirFromSelfExe(gpa, io, cwd_path, self_exe_path); } -/// Like `std.process.getCwdAlloc`, but also resolves the path with `Dir.path.resolve`. This +/// Like `std.process.currentDirAlloc`, but also resolves the path with `Dir.path.resolve`. This /// means the path has no repeated separators, no "." or ".." components, and no trailing separator. /// On WASI, "" is returned instead of ".". -pub fn getResolvedCwd(gpa: Allocator) error{ +pub fn getResolvedCwd(io: Io, gpa: Allocator) error{ OutOfMemory, CurrentWorkingDirectoryUnlinked, Unexpected, }![]u8 { if (builtin.target.os.tag == .wasi) { if (std.debug.runtime_safety) { - const cwd = try std.process.getCwdAlloc(gpa); + const cwd = try std.process.currentDirAlloc(io, gpa); defer gpa.free(cwd); assert(mem.eql(u8, cwd, ".")); } return ""; } - const cwd = try std.process.getCwdAlloc(gpa); + const cwd = try std.process.currentDirAlloc(io, gpa); defer gpa.free(cwd); const resolved = try Dir.path.resolve(gpa, &.{cwd}); assert(Dir.path.isAbsolute(resolved)); diff --git a/src/main.zig b/src/main.zig index f914593bce..0efab88f6a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4775,7 +4775,7 @@ fn cmdInit(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) ! } } - const cwd_path = try introspect.getResolvedCwd(arena); + const cwd_path = try introspect.getResolvedCwd(io, arena); const cwd_basename = fs.path.basename(cwd_path); const sanitized_root_name = try sanitizeExampleName(arena, cwd_basename); @@ -5141,7 +5141,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8, process.raiseFileDescriptorLimit(); - const cwd_path = try introspect.getResolvedCwd(arena); + const cwd_path = try introspect.getResolvedCwd(io, arena); const build_root = try findBuildRoot(arena, io, .{ .cwd_path = cwd_path, .build_file = build_file, @@ -7077,7 +7077,7 @@ fn cmdFetch( }, }; - const cwd_path = try introspect.getResolvedCwd(arena); + const cwd_path = try introspect.getResolvedCwd(io, arena); var build_root = try findBuildRoot(arena, io, .{ .cwd_path = cwd_path, @@ -7288,7 +7288,7 @@ const FindBuildRootOptions = struct { }; fn findBuildRoot(arena: Allocator, io: Io, options: FindBuildRootOptions) !BuildRoot { - const cwd_path = options.cwd_path orelse try introspect.getResolvedCwd(arena); + const cwd_path = options.cwd_path orelse try introspect.getResolvedCwd(io, arena); const build_zig_basename = if (options.build_file) |bf| fs.path.basename(bf) else @@ -7490,7 +7490,7 @@ fn writeSimpleTemplateFile(io: Io, file_name: []const u8, comptime fmt: []const } fn findTemplates(gpa: Allocator, arena: Allocator, io: Io) Templates { - const cwd_path = introspect.getResolvedCwd(arena) catch |err| { + const cwd_path = introspect.getResolvedCwd(io, arena) catch |err| { fatal("unable to get cwd: {t}", .{err}); }; const self_exe_path = process.executablePathAlloc(io, arena) catch |err| { diff --git a/test/standalone/child_process/main.zig b/test/standalone/child_process/main.zig index 2776c3e95c..159be62a9d 100644 --- a/test/standalone/child_process/main.zig +++ b/test/standalone/child_process/main.zig @@ -9,7 +9,14 @@ pub fn main(init: std.process.Init.Minimal) !void { }; const gpa = gpa_state.allocator(); - const process_cwd_path = try std.process.getCwdAlloc(gpa); + var threaded: Io.Threaded = .init(gpa, .{ + .argv0 = .init(init.args), + .environ = init.environ, + }); + defer threaded.deinit(); + const io = threaded.io(); + + const process_cwd_path = try std.process.currentDirAlloc(io, gpa); defer gpa.free(process_cwd_path); var environ_map = try init.environ.createMap(gpa); @@ -27,13 +34,6 @@ pub fn main(init: std.process.Init.Minimal) !void { }; defer if (needs_free) gpa.free(child_path); - var threaded: Io.Threaded = .init(gpa, .{ - .argv0 = .init(init.args), - .environ = init.environ, - }); - defer threaded.deinit(); - const io = threaded.io(); - var child = try std.process.spawn(io, .{ .argv = &.{ child_path, "hello arg" }, .stdin = .pipe, diff --git a/test/standalone/posix/cwd.zig b/test/standalone/posix/cwd.zig index 7a9517b412..e5e376784a 100644 --- a/test/standalone/posix/cwd.zig +++ b/test/standalone/posix/cwd.zig @@ -13,43 +13,44 @@ pub fn main(init: std.process.Init) !void { .windows => return, // POSIX is not implemented by Windows else => {}, } + const io = init.io; const args = try init.minimal.args.toSlice(init.arena.allocator()); const tmp_dir_path = args[1]; var tmp_dir = try Io.Dir.cwd().openDir(init.io, tmp_dir_path, .{}); defer tmp_dir.close(init.io); - try test_chdir_self(); - try test_chdir_absolute(); - try test_chdir_relative(init.gpa, init.io, tmp_dir); + try test_chdir_self(io); + try test_chdir_absolute(io); + try test_chdir_relative(init.gpa, io, tmp_dir); } // get current working directory and expect it to match given path -fn expect_cwd(expected_cwd: []const u8) !void { +fn expect_cwd(io: Io, expected_cwd: []const u8) !void { var cwd_buf: [path_max]u8 = undefined; - const actual_cwd = try std.posix.getcwd(cwd_buf[0..]); + const actual_cwd = cwd_buf[0..try std.process.currentDir(io, &cwd_buf)]; try std.testing.expectEqualStrings(actual_cwd, expected_cwd); } -fn test_chdir_self() !void { +fn test_chdir_self(io: Io) !void { var old_cwd_buf: [path_max]u8 = undefined; - const old_cwd = try std.posix.getcwd(old_cwd_buf[0..]); + const old_cwd = old_cwd_buf[0..try std.process.currentDir(io, &old_cwd_buf)]; // Try changing to the current directory try std.Io.Threaded.chdir(old_cwd); - try expect_cwd(old_cwd); + try expect_cwd(io, old_cwd); } -fn test_chdir_absolute() !void { +fn test_chdir_absolute(io: Io) !void { var old_cwd_buf: [path_max]u8 = undefined; - const old_cwd = try std.posix.getcwd(old_cwd_buf[0..]); + const old_cwd = old_cwd_buf[0..try std.process.currentDir(io, &old_cwd_buf)]; const parent = std.fs.path.dirname(old_cwd) orelse unreachable; // old_cwd should be absolute // Try changing to the parent via a full path try std.Io.Threaded.chdir(parent); - try expect_cwd(parent); + try expect_cwd(io, parent); } fn test_chdir_relative(gpa: Allocator, io: Io, tmp_dir: Io.Dir) !void { @@ -61,7 +62,7 @@ fn test_chdir_relative(gpa: Allocator, io: Io, tmp_dir: Io.Dir) !void { // Capture base working directory path, to build expected full path var base_cwd_buf: [path_max]u8 = undefined; - const base_cwd = try std.posix.getcwd(base_cwd_buf[0..]); + const base_cwd = base_cwd_buf[0..try std.process.currentDir(io, &base_cwd_buf)]; const expected_path = try std.fs.path.resolve(gpa, &.{ base_cwd, subdir_path }); defer gpa.free(expected_path); @@ -70,7 +71,7 @@ fn test_chdir_relative(gpa: Allocator, io: Io, tmp_dir: Io.Dir) !void { try std.Io.Threaded.chdir(subdir_path); var new_cwd_buf: [path_max]u8 = undefined; - const new_cwd = try std.posix.getcwd(new_cwd_buf[0..]); + const new_cwd = new_cwd_buf[0..try std.process.currentDir(io, &new_cwd_buf)]; // On Windows, fs.path.resolve returns an uppercase drive letter, but the drive letter returned by getcwd may be lowercase const resolved_cwd = try std.fs.path.resolve(gpa, &.{new_cwd}); diff --git a/test/standalone/self_exe_symlink/create-symlink.zig b/test/standalone/self_exe_symlink/create-symlink.zig index 32610ebcde..aaa015c4b0 100644 --- a/test/standalone/self_exe_symlink/create-symlink.zig +++ b/test/standalone/self_exe_symlink/create-symlink.zig @@ -9,7 +9,7 @@ pub fn main(init: std.process.Init) !void { const exe_path = it.next() orelse unreachable; const symlink_path = it.next() orelse unreachable; - const cwd = try std.process.getCwdAlloc(init.arena.allocator()); + const cwd = try std.process.currentDirAlloc(io, init.arena.allocator()); // If `exe_path` is relative to our cwd, we need to convert it to be relative to the dirname of `symlink_path`. const exe_rel_path = try std.fs.path.relative(gpa, cwd, init.environ_map, std.fs.path.dirname(symlink_path) orelse ".", exe_path); diff --git a/test/standalone/windows_paths/relative.zig b/test/standalone/windows_paths/relative.zig index 7b3725e4ee..7dcde62d50 100644 --- a/test/standalone/windows_paths/relative.zig +++ b/test/standalone/windows_paths/relative.zig @@ -4,7 +4,7 @@ pub fn main(init: std.process.Init) !void { const arena = init.arena.allocator(); const args = try init.minimal.args.toSlice(arena); const io = init.io; - const cwd_path = try std.process.getCwdAlloc(arena); + const cwd_path = try std.process.currentDirAlloc(io, arena); if (args.len < 3) return error.MissingArgs; diff --git a/test/standalone/windows_paths/test.zig b/test/standalone/windows_paths/test.zig index 94afe16337..47b13415a8 100644 --- a/test/standalone/windows_paths/test.zig +++ b/test/standalone/windows_paths/test.zig @@ -10,7 +10,7 @@ pub fn main(init: std.process.Init) !void { const exe_path = args[1]; - const cwd_path = try std.process.getCwdAlloc(arena); + const cwd_path = try std.process.currentDirAlloc(io, arena); const parsed_cwd_path = std.fs.path.parsePathWindows(u8, cwd_path); if (parsed_cwd_path.kind == .drive_absolute and !std.ascii.isAlphabetic(cwd_path[0])) { diff --git a/test/standalone/windows_spawn/main.zig b/test/standalone/windows_spawn/main.zig index add20921b5..0bb8e7fe16 100644 --- a/test/standalone/windows_spawn/main.zig +++ b/test/standalone/windows_spawn/main.zig @@ -8,7 +8,7 @@ const utf16Literal = std.unicode.utf8ToUtf16LeStringLiteral; pub fn main(init: std.process.Init) !void { const gpa = init.gpa; const io = init.io; - const process_cwd_path = try std.process.getCwdAlloc(init.arena.allocator()); + const process_cwd_path = try std.process.currentDirAlloc(io, init.arena.allocator()); var initial_process_cwd = try Io.Dir.cwd().openDir(io, ".", .{}); defer initial_process_cwd.close(io); diff --git a/tools/doctest.zig b/tools/doctest.zig index 5037bac2c2..5afec8e62c 100644 --- a/tools/doctest.zig +++ b/tools/doctest.zig @@ -33,7 +33,7 @@ pub fn main(init: std.process.Init) !void { const arena = init.arena.allocator(); const io = init.io; const environ_map = init.environ_map; - const cwd_path = try std.process.getCwdAlloc(arena); + const cwd_path = try std.process.currentDirAlloc(io, arena); try environ_map.put("CLICOLOR_FORCE", "1"); diff --git a/tools/incr-check.zig b/tools/incr-check.zig index daf93be73f..e15a91b548 100644 --- a/tools/incr-check.zig +++ b/tools/incr-check.zig @@ -32,7 +32,7 @@ pub fn main(init: std.process.Init) !void { const arena = init.arena.allocator(); const io = init.io; const environ_map = init.environ_map; - const cwd_path = try std.process.getCwdAlloc(arena); + const cwd_path = try std.process.currentDirAlloc(io, arena); var opt_zig_exe: ?[]const u8 = null; var opt_input_file_name: ?[]const u8 = null; diff --git a/tools/process_headers.zig b/tools/process_headers.zig index 0c72299055..6e5ab38797 100644 --- a/tools/process_headers.zig +++ b/tools/process_headers.zig @@ -145,7 +145,7 @@ pub fn main(init: std.process.Init) !void { const arena = init.arena.allocator(); const io = init.io; const args = try init.minimal.args.toSlice(arena); - const cwd_path = try std.process.getCwdAlloc(arena); + const cwd_path = try std.process.currentDirAlloc(io, arena); const environ_map = init.environ_map; var search_paths = std.array_list.Managed([]const u8).init(arena); diff --git a/tools/update-linux-headers.zig b/tools/update-linux-headers.zig index e05ba08203..649459a634 100644 --- a/tools/update-linux-headers.zig +++ b/tools/update-linux-headers.zig @@ -146,7 +146,7 @@ pub fn main(init: std.process.Init) !void { const io = init.io; const args = try init.minimal.args.toSlice(arena); const environ_map = init.environ_map; - const cwd = try std.process.getCwdAlloc(arena); + const cwd = try std.process.currentDirAlloc(io, arena); var search_paths = std.array_list.Managed([]const u8).init(arena); var opt_out_dir: ?[]const u8 = null;