Commit ae39e7867d

xEgoist <egoist@egoistic.dev>
2022-10-07 21:00:48
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.
1 parent b4e3424
Changed files (1)
lib
std
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);
     }