Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(windows): use bun.spawnSync for bun upgrade + different check for bun #10006

Merged
merged 3 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bun.js/WebKit
10 changes: 8 additions & 2 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1224,11 +1224,17 @@ pub const Command = struct {
};

pub fn isBunX(argv0: []const u8) bool {
return strings.endsWithComptime(argv0, "bunx" ++ bun.exe_suffix);
if (Environment.isWindows) {
return strings.endsWithComptime(argv0, "bunx.exe");
}
return strings.endsWithComptime(argv0, "bunx");
}

pub fn isNode(argv0: []const u8) bool {
return strings.endsWithComptime(argv0, "node" ++ bun.exe_suffix);
if (Environment.isWindows) {
return strings.endsWithComptime(argv0, "node.exe");
}
return strings.endsWithComptime(argv0, "node");
}

pub fn which() Tag {
Expand Down
45 changes: 30 additions & 15 deletions src/cli/upgrade_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -657,33 +657,48 @@ pub const UpgradeCommand = struct {
},
);

var buf: bun.PathBuffer = undefined;
const powershell_path =
bun.which(&buf, bun.getenvZ("PATH") orelse "", "", "powershell") orelse
hardcoded_system_powershell: {
const system_root = bun.getenvZ("SystemRoot") orelse "C:\\Windows";
const hardcoded_system_powershell = bun.path.joinAbsStringBuf(system_root, &buf, &.{ system_root, "System32\\WindowsPowerShell\\v1.0\\powershell.exe" }, .windows);
if (bun.sys.exists(hardcoded_system_powershell)) {
break :hardcoded_system_powershell hardcoded_system_powershell;
}
Output.prettyErrorln("<r><red>error:<r> Failed to unzip {s} due to PowerShell not being installed.", .{tmpname});
Global.exit(1);
};

var unzip_argv = [_]string{
"powershell.exe",
powershell_path,
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
unzip_script,
};

var unzip_process = std.ChildProcess.init(&unzip_argv, ctx.allocator);
_ = (bun.spawnSync(&.{
.argv = &unzip_argv,

unzip_process.cwd = tmpdir_path;
unzip_process.stdin_behavior = .Inherit;
unzip_process.stdout_behavior = .Inherit;
unzip_process.stderr_behavior = .Inherit;
.envp = null,
.cwd = tmpdir_path,

const unzip_result = unzip_process.spawnAndWait() catch |err| {
save_dir.deleteFileZ(tmpname) catch {};
Output.prettyErrorln("<r><red>error:<r> Failed to spawn unzip due to {s}.", .{@errorName(err)});
Global.exit(1);
};
.stderr = .inherit,
.stdout = .inherit,
.stdin = .inherit,

if (unzip_result.Exited != 0) {
Output.prettyErrorln("<r><red>Unzip failed<r> (exit code: {d})", .{unzip_result.Exited});
save_dir.deleteFileZ(tmpname) catch {};
.windows = if (Environment.isWindows) .{
.loop = bun.JSC.EventLoopHandle.init(bun.JSC.MiniEventLoop.initGlobal(null)),
} else {},
}) catch |err| {
Output.prettyErrorln("<r><red>error:<r> Failed to spawn Expand-Archive on {s} due to error {s}", .{ tmpname, @errorName(err) });
Global.exit(1);
}
}).unwrap() catch |err| {
Output.prettyErrorln("<r><red>error:<r> Failed to run Expand-Archive on {s} due to error {s}", .{ tmpname, @errorName(err) });
Global.exit(1);
};
}
}
{
Expand Down
2 changes: 0 additions & 2 deletions src/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1259,8 +1259,6 @@ pub fn fmtSlice(data: anytype, comptime delim: []const u8) FormatSlice(@TypeOf(d
}

fn FormatSlice(comptime T: type, comptime delim: []const u8) type {
std.debug.assert(@typeInfo(T).Pointer.size == .Slice);

return struct {
slice: T,

Expand Down
2 changes: 2 additions & 0 deletions src/which.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ fn isValid(buf: *bun.PathBuffer, segment: []const u8, bin: []const u8) ?u16 {
// Like /usr/bin/which but without needing to exec a child process
// Remember to resolve the symlink if necessary
pub fn which(buf: *bun.PathBuffer, path: []const u8, cwd: []const u8, bin: []const u8) ?[:0]const u8 {
bun.Output.scoped(.which, true)("path={s} cwd={s} bin={s}", .{ path, cwd, bin });

if (bun.Environment.os == .windows) {
var convert_buf: bun.WPathBuffer = undefined;
const result = whichWin(&convert_buf, path, cwd, bin) orelse return null;
Expand Down
Loading