Skip to content

Commit

Permalink
Fix process.cwd on windows (#14081)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Sep 21, 2024
1 parent 722e3fa commit c298b23
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
22 changes: 5 additions & 17 deletions src/bun.js/node/path.zig
Original file line number Diff line number Diff line change
Expand Up @@ -186,37 +186,25 @@ pub fn getCwdWindowsU8(buf: []u8) MaybeBuf(u8) {
}
}

const withoutTrailingSlash = if (Environment.isWindows) strings.withoutTrailingSlashWindowsPath else strings.withoutTrailingSlash;

pub fn getCwdWindowsU16(buf: []u16) MaybeBuf(u16) {
const len: u32 = windows.GetCurrentDirectoryW(buf.len, &buf);
const len: u32 = strings.convertUTF8toUTF16InBuffer(&buf, withoutTrailingSlash(bun.fs.FileSystem.instance.top_level_dir));
if (len == 0) {
// Indirectly calls std.os.windows.kernel32.GetLastError().
return MaybeBuf(u16).errnoSys(0, Syscall.Tag.getcwd).?;
}
return MaybeBuf(u16){ .result = buf[0..len] };
}

pub fn getCwdWindowsT(comptime T: type, buf: []T) MaybeBuf(T) {
comptime validatePathT(T, "getCwdWindowsT");
return if (T == u16)
getCwdWindowsU16(buf)
else
getCwdWindowsU8(buf);
}

pub fn getCwdU8(buf: []u8) MaybeBuf(u8) {
const cached_cwd = strings.withoutTrailingSlash(bun.fs.FileSystem.instance.top_level_dir);
const cached_cwd = withoutTrailingSlash(bun.fs.FileSystem.instance.top_level_dir);
@memcpy(buf[0..cached_cwd.len], cached_cwd);
return MaybeBuf(u8){ .result = buf[0..cached_cwd.len] };
}

pub fn getCwdU16(buf: []u16) MaybeBuf(u16) {
if (comptime Environment.isWindows) {
return getCwdWindowsU16(&buf);
}
const u8Buf: bun.PathBuffer = undefined;
const result = strings.convertUTF8toUTF16InBuffer(&buf, bun.getcwd(strings.convertUTF16ToUTF8InBuffer(&u8Buf, buf))) catch {
return MaybeBuf(u16).errnoSys(0, Syscall.Tag.getcwd).?;
};
const result = strings.convertUTF8toUTF16InBuffer(&buf, withoutTrailingSlash(bun.fs.FileSystem.instance.top_level_dir));
return MaybeBuf(u16){ .result = result };
}

Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2034,8 +2034,8 @@ pub const Process = struct {
fs.top_level_dir_buf[len] = std.fs.path.sep;
fs.top_level_dir_buf[len + 1] = 0;
fs.top_level_dir = fs.top_level_dir_buf[0 .. len + 1];

var str = bun.String.createUTF8(strings.withoutTrailingSlash(fs.top_level_dir));
const withoutTrailingSlash = if (Environment.isWindows) strings.withoutTrailingSlashWindowsPath else strings.withoutTrailingSlash;
var str = bun.String.createUTF8(withoutTrailingSlash(fs.top_level_dir));
return str.transferToJS(globalObject);
},
.err => |e| {
Expand Down
17 changes: 17 additions & 0 deletions test/js/node/process/process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ it("process", () => {
expect(cwd).toEqual(process.cwd());
});

it("process.chdir() on root dir", () => {
const cwd = process.cwd();
try {
let root = "/";
if (process.platform === "win32") {
const driveLetter = process.cwd().split(":\\")[0];
root = `${driveLetter}:\\`;
}
process.chdir(root);
expect(process.cwd()).toBe(root);
process.chdir(cwd);
expect(process.cwd()).toBe(cwd);
} finally {
process.chdir(cwd);
}
});

it("process.hrtime()", () => {
const start = process.hrtime();
const end = process.hrtime(start);
Expand Down

0 comments on commit c298b23

Please sign in to comment.