From ae39e7867db8053cd0538da10e144bf86bce4eba Mon Sep 17 00:00:00 2001 From: xEgoist Date: Fri, 7 Oct 2022 14:00:48 -0500 Subject: [PATCH] Added os check for std.fs.setAsCwd() to work with windows Due to the unavailability of fchdir in Windows, a call for setting the CWD needs to either call chdir with the path string or call SetCurrentDirectory. Either way, since we are dealing with a Handle in Windows, a call for GetFinalPathNameByHandle is necessary for getting the file path first. --- lib/std/fs.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/std/fs.zig b/lib/std/fs.zig index c85d9d82f5a905b8d71a887e5161b191da8874c4..f7027a70c933e88c2297bb7b4cee8e05650f15b7 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -1602,6 +1602,14 @@ pub const Dir = struct { if (builtin.os.tag == .wasi) { @compileError("changing cwd is not currently possible in WASI"); } + if (builtin.os.tag == .windows) { + var dir_path_buffer: [os.windows.PATH_MAX_WIDE]u16 = undefined; + var dir_path = try os.windows.GetFinalPathNameByHandle(self.fd, .{}, &dir_path_buffer); + if (builtin.link_libc) { + return os.chdirW(dir_path); + } + return os.windows.SetCurrentDirectory(dir_path); + } try os.fchdir(self.fd); } -- 2.54.0