Skip to content

Commit

Permalink
fix windows symlink/junction
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-conway committed Apr 15, 2024
1 parent 2596e8c commit 0fe06f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2101,16 +2101,16 @@ pub const PackageInstall = struct {
const dest_z = dest_buf[0..offset :0];

to_buf[to_path.len] = 0;
const to_path_z = to_buf[0..to_path.len :0];
const target_z = to_buf[0..to_path.len :0];

// https://github.com/npm/cli/blob/162c82e845d410ede643466f9f8af78a312296cc/workspaces/arborist/lib/arborist/reify.js#L738
// https://github.com/npm/cli/commit/0e58e6f6b8f0cd62294642a502c17561aaf46553
switch (bun.sys.symlinkOrJunctionOnWindows(dest_z, to_path_z)) {
switch (bun.sys.symlinkOrJunctionOnWindows(dest_z, target_z)) {
.err => |err_| brk: {
var err = err_;
if (err.getErrno() == .EXIST) {
_ = bun.sys.unlink(to_path_z);
switch (bun.sys.symlinkOrJunctionOnWindows(dest_z, to_path_z)) {
_ = bun.sys.unlink(target_z);
switch (bun.sys.symlinkOrJunctionOnWindows(dest_z, target_z)) {
.err => |e| err = e,
.result => break :brk,
}
Expand Down
14 changes: 7 additions & 7 deletions src/sys.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1733,11 +1733,11 @@ pub const WindowsSymlinkOptions = packed struct {
pub var has_failed_to_create_symlink = false;
};

pub fn symlinkOrJunctionOnWindows(sym: [:0]const u8, target: [:0]const u8) Maybe(void) {
pub fn symlinkOrJunctionOnWindows(dest: [:0]const u8, target: [:0]const u8) Maybe(void) {
if (!WindowsSymlinkOptions.has_failed_to_create_symlink) {
var sym16: bun.WPathBuffer = undefined;
var target16: bun.WPathBuffer = undefined;
const sym_path = bun.strings.toNTPath(&sym16, sym);
const sym_path = bun.strings.toNTPath(&sym16, dest);
const target_path = bun.strings.toNTPath(&target16, target);
switch (symlinkW(sym_path, target_path, .{ .directory = true })) {
.result => {
Expand All @@ -1751,17 +1751,17 @@ pub fn symlinkOrJunctionOnWindows(sym: [:0]const u8, target: [:0]const u8) Maybe
}
}

return sys_uv.symlinkUV(sym, target, bun.windows.libuv.UV_FS_SYMLINK_JUNCTION);
return sys_uv.symlinkUV(target, dest, bun.windows.libuv.UV_FS_SYMLINK_JUNCTION);
}

pub fn symlinkW(sym: [:0]const u16, target: [:0]const u16, options: WindowsSymlinkOptions) Maybe(void) {
pub fn symlinkW(dest: [:0]const u16, target: [:0]const u16, options: WindowsSymlinkOptions) Maybe(void) {
while (true) {
const flags = options.flags();

if (windows.kernel32.CreateSymbolicLinkW(sym, target, flags) == 0) {
if (windows.kernel32.CreateSymbolicLinkW(dest, target, flags) == 0) {
const errno = bun.windows.Win32Error.get();
log("CreateSymbolicLinkW({}, {}, {any}) = {s}", .{
bun.fmt.fmtPath(u16, sym, .{}),
bun.fmt.fmtPath(u16, dest, .{}),
bun.fmt.fmtPath(u16, target, .{}),
flags,
@tagName(errno),
Expand All @@ -1788,7 +1788,7 @@ pub fn symlinkW(sym: [:0]const u16, target: [:0]const u16, options: WindowsSymli
}

log("CreateSymbolicLinkW({}, {}, {any}) = 0", .{
bun.fmt.fmtPath(u16, sym, .{}),
bun.fmt.fmtPath(u16, dest, .{}),
bun.fmt.fmtPath(u16, target, .{}),
flags,
});
Expand Down

0 comments on commit 0fe06f2

Please sign in to comment.