Skip to content

Commit

Permalink
fix(windows): use bun.spawnSync for bun upgrade + different check for…
Browse files Browse the repository at this point in the history
… bun (#10006)

* small changes

* [autofix.ci] apply automated fixes

* fxifsdahjfkdsahjk

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
paperdave and autofix-ci[bot] committed Apr 6, 2024
1 parent fd3cd05 commit f014f35
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
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

0 comments on commit f014f35

Please sign in to comment.