Skip to content

Commit 2806b13

Browse files
simnalamburtaklinker1Aternus
authored
fix: Upgrade zig to 0.15.2 (#32)
Co-authored-by: Aaron <aaronklinker1@gmail.com> Co-authored-by: Aternus <1684891+Aternus@users.noreply.github.com>
1 parent ae85680 commit 2806b13

File tree

5 files changed

+84
-43
lines changed

5 files changed

+84
-43
lines changed

.github/actions/setup/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ runs:
55
steps:
66
- uses: goto-bus-stop/setup-zig@v2
77
with:
8-
version: 0.14.0
8+
version: 0.15.2

build.zig

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const bunv = "bunv";
99
pub fn build(b: *std.Build) !void {
1010
const min_zig_version = try std.SemanticVersion.parse("0.13.0");
1111
if (std.SemanticVersion.order(builtin.zig_version, min_zig_version) == .lt) {
12-
std.debug.print("Bunv requires Zig 0.13.0 or above, got {}", .{builtin.zig_version});
12+
std.debug.print("Bunv requires Zig 0.13.0 or above, got {f}", .{builtin.zig_version});
1313
return error.InvalidZigVersion;
1414
}
1515

@@ -30,9 +30,11 @@ pub fn build(b: *std.Build) !void {
3030
fn addExe(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, version: std.SemanticVersion, comptime name: []const u8) void {
3131
const exe = b.addExecutable(.{
3232
.name = name,
33-
.root_source_file = b.path("src/" ++ name ++ ".zig"),
34-
.target = target,
35-
.optimize = optimize,
33+
.root_module = b.createModule(.{
34+
.root_source_file = b.path("src/" ++ name ++ ".zig"),
35+
.target = target,
36+
.optimize = optimize,
37+
}),
3638
.version = version,
3739
});
3840

src/bunv.zig

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const std = @import("std");
22
const mem = std.mem;
3-
const utils = @import("utils.zig");
4-
const vm = @import("vm.zig");
53
const builtin = @import("builtin");
4+
65
const config = @import("config");
6+
77
const c = @import("colors.zig");
8+
const utils = @import("utils.zig");
9+
const vm = @import("vm.zig");
810

911
pub fn main() !void {
1012
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@@ -13,7 +15,7 @@ pub fn main() !void {
1315

1416
const is_debug = try utils.isDebug(allocator);
1517
if (is_debug) {
16-
std.debug.print("Bunv Version: {}\n", .{config.version});
18+
std.debug.print("Bunv Version: {f}\n", .{config.version});
1719
std.debug.print("Operating System: {s}\n", .{@tagName(builtin.os.tag)});
1820
std.debug.print("Architecture: {s}\n", .{@tagName(builtin.cpu.arch)});
1921
}
@@ -51,12 +53,12 @@ pub fn main() !void {
5153
}
5254

5355
fn listVersions(allocator: mem.Allocator, config_dir: []const u8) !void {
54-
const installed_versions = try vm.getInstalledVersions(allocator, config_dir);
56+
var installed_versions = try vm.getInstalledVersions(allocator, config_dir);
5557
defer {
5658
for (installed_versions.items) |item| {
5759
allocator.free(item);
5860
}
59-
installed_versions.deinit();
61+
installed_versions.deinit(allocator);
6062
}
6163

6264
try printInstalledVersions(allocator, config_dir, installed_versions);
@@ -82,7 +84,7 @@ fn printInstalledVersions(allocator: mem.Allocator, config_dir: []const u8, vers
8284
}
8385

8486
fn printHelp() !void {
85-
std.debug.print("\n{s}{s}Bunv{s} - Manage installed versions of Bun {s}({}){s}\n\n", .{ c.bold, c.blue, c.reset, c.dim, config.version, c.reset });
87+
std.debug.print("\n{s}{s}Bunv{s} - Manage installed versions of Bun {s}({f}){s}\n\n", .{ c.bold, c.blue, c.reset, c.dim, config.version, c.reset });
8688
std.debug.print("{s}Commands:{s}\n", .{ c.bold, c.reset });
8789
std.debug.print(" List installed Bun versions\n", .{});
8890
std.debug.print(" {s}{s}rm{s} {s}<version>{s} Remove an installed Bun version\n", .{ c.bold, c.yellow, c.reset, c.dim, c.reset });
@@ -92,12 +94,12 @@ fn printHelp() !void {
9294

9395
fn removeVersion(allocator: mem.Allocator, config_dir: []const u8, version: []const u8) !void {
9496
// Check if version is installed
95-
const installed_versions = try vm.getInstalledVersions(allocator, config_dir);
97+
var installed_versions = try vm.getInstalledVersions(allocator, config_dir);
9698
defer {
9799
for (installed_versions.items) |item| {
98100
allocator.free(item);
99101
}
100-
installed_versions.deinit();
102+
installed_versions.deinit(allocator);
101103
}
102104

103105
var version_exists = false;

src/utils.zig

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,34 @@ const mem = std.mem;
44
const fs = std.fs;
55
const json = std.json;
66
const builtin = @import("builtin");
7+
78
const vm = @import("vm.zig");
89

910
pub const Cmd = enum {
1011
bun,
1112
bunx,
1213
};
1314

15+
/// Stringify a list of strings into a formatted string representation: ["item1", "item2", "item3"]
16+
/// Caller owns the returned memory and must free it.
17+
pub fn stringifyStringList(allocator: mem.Allocator, list: []const []const u8) ![]const u8 {
18+
var result: std.ArrayList(u8) = .empty;
19+
errdefer result.deinit(allocator);
20+
21+
try result.append(allocator, '[');
22+
for (list, 0..) |item, i| {
23+
if (i > 0) {
24+
try result.appendSlice(allocator, ", ");
25+
}
26+
try result.append(allocator, '"');
27+
try result.appendSlice(allocator, item);
28+
try result.append(allocator, '"');
29+
}
30+
try result.append(allocator, ']');
31+
32+
return result.toOwnedSlice(allocator);
33+
}
34+
1435
pub fn run(allocator: mem.Allocator, cmd: Cmd) !void {
1536
const is_debug = try isDebug(allocator);
1637
if (is_debug) std.debug.print("Executable: {}\n", .{cmd});
@@ -30,20 +51,26 @@ pub fn run(allocator: mem.Allocator, cmd: Cmd) !void {
3051
const bin = try fs.path.join(allocator, &[_][]const u8{ config_dir, "versions", project_version, "bin", "bun" });
3152
defer allocator.free(bin);
3253

33-
var new_args = try std.ArrayList([]const u8).initCapacity(allocator, 5);
34-
defer new_args.deinit();
54+
var new_args: std.ArrayList([]const u8) = .empty;
55+
defer new_args.deinit(allocator);
3556

36-
try new_args.append(bin);
37-
if (cmd == .bunx) try new_args.append("x");
57+
try new_args.append(allocator, bin);
58+
if (cmd == .bunx) try new_args.append(allocator, "x");
3859

3960
var args = try std.process.argsAlloc(allocator);
4061
defer std.process.argsFree(allocator, args);
4162

4263
for (args[1..]) |arg| {
43-
try new_args.append(arg);
64+
try new_args.append(allocator, arg);
4465
}
4566

46-
if (is_debug) std.debug.print("Original args: {s}\nModified args: {s}\n---\n", .{ args, new_args.items });
67+
if (is_debug) {
68+
const original_str = try stringifyStringList(allocator, args);
69+
defer allocator.free(original_str);
70+
const modified_str = try stringifyStringList(allocator, new_args.items);
71+
defer allocator.free(modified_str);
72+
std.debug.print("Original args: {s}\nModified args: {s}\n---\n", .{ original_str, modified_str });
73+
}
4774
return runBunCmd(allocator, new_args.items);
4875
}
4976

src/vm.zig

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ const mem = std.mem;
44
const fs = std.fs;
55
const json = std.json;
66
const http = std.http;
7-
const utils = @import("utils.zig");
7+
88
const c = @import("colors.zig");
9+
const utils = @import("utils.zig");
910

1011
pub fn getInstalledVersions(allocator: mem.Allocator, config_dir: []const u8) !std.ArrayList([]const u8) {
1112
const versions_dir_path = try getVersionsDir(allocator, config_dir);
1213
defer allocator.free(versions_dir_path);
1314

14-
var result = std.ArrayList([]const u8).init(allocator);
15+
var result: std.ArrayList([]const u8) = .empty;
1516

1617
var versions_dir = fs.openDirAbsolute(versions_dir_path, .{ .iterate = true }) catch |err| switch (err) {
1718
error.FileNotFound => return result,
@@ -30,7 +31,7 @@ pub fn getInstalledVersions(allocator: mem.Allocator, config_dir: []const u8) !s
3031
defer allocator.free(bin);
3132

3233
if (try utils.file_exists(bin)) {
33-
try result.append(version);
34+
try result.append(allocator, version);
3435
} else {
3536
allocator.free(version);
3637
}
@@ -90,13 +91,17 @@ pub fn detectProjectVersion(allocator: mem.Allocator, is_debug: bool) !?[]const
9091
pub fn getLatestLocalVersion(allocator: mem.Allocator, is_debug: bool, config_dir: []const u8) !?[]const u8 {
9192
if (is_debug) std.debug.print("Getting latest local version...\n", .{});
9293

93-
const installed_versions = try getInstalledVersions(allocator, config_dir);
94-
if (is_debug) std.debug.print("{d} versions: {s}\n", .{ installed_versions.items.len, installed_versions.items });
94+
var installed_versions = try getInstalledVersions(allocator, config_dir);
95+
if (is_debug) {
96+
const versions_str = try utils.stringifyStringList(allocator, installed_versions.items);
97+
defer allocator.free(versions_str);
98+
std.debug.print("{d} versions: {s}\n", .{ installed_versions.items.len, versions_str });
99+
}
95100
defer {
96101
for (installed_versions.items) |item| {
97102
allocator.free(item);
98103
}
99-
installed_versions.deinit();
104+
installed_versions.deinit(allocator);
100105
}
101106

102107
if (installed_versions.items.len == 0) {
@@ -120,19 +125,20 @@ pub fn getLatestRemoteVersion(allocator: mem.Allocator, is_debug: bool) ![]const
120125
const uri = try std.Uri.parse("https://ungh.cc/repos/oven-sh/bun/releases/latest");
121126
const buf = try allocator.alloc(u8, 1024 * 1024 * 4);
122127
defer allocator.free(buf);
123-
var req = try client.open(
124-
.GET,
125-
uri,
126-
.{ .server_header_buffer = buf, .keep_alive = false, .extra_headers = &[_]http.Header{accept_header} },
127-
);
128+
var req = try client.request(.GET, uri, .{
129+
.keep_alive = false,
130+
.headers = .{ .accept_encoding = .omit },
131+
.extra_headers = &[_]http.Header{accept_header},
132+
});
128133
defer req.deinit();
129134

130-
try req.send();
131-
try req.finish();
132-
try req.wait();
135+
try req.sendBodiless();
133136

134-
var rdr = req.reader();
135-
const body = try rdr.readAllAlloc(allocator, 1024 * 1024 * 4);
137+
var redirect_buffer: [8 * 1024]u8 = undefined;
138+
var response = try req.receiveHead(&redirect_buffer);
139+
var transfer_buffer: [4 * 1024]u8 = undefined;
140+
const reader = response.reader(&transfer_buffer);
141+
const body = try reader.allocRemaining(allocator, .limited(1024 * 1024 * 4));
136142
defer allocator.free(body);
137143
if (is_debug) std.debug.print("Latest release: {s}\n", .{body});
138144

@@ -238,25 +244,29 @@ fn confirmInstallation(version: []const u8) !void {
238244
}
239245
}
240246

241-
const stdout = std.io.getStdOut().writer();
247+
var stdout_buf: [1024]u8 = undefined;
248+
var stdout = std.fs.File.stdout().writer(&stdout_buf);
242249

243250
// Check if stdin is a TTY (interactive)
244-
const is_interactive = std.io.getStdIn().isTty();
251+
const stdin_file = std.fs.File.stdin();
252+
const is_interactive = stdin_file.isTty();
245253

246254
if (is_interactive) {
247-
const stdin = std.io.getStdIn().reader();
255+
var stdin_buf: [1024]u8 = undefined;
256+
var stdin = stdin_file.reader(&stdin_buf);
248257

249-
try stdout.print("{s}Bun v{s} is not installed. Do you want to install it? [y/N]{s} ", .{ c.yellow, version, c.reset });
250-
var buffer: [2]u8 = undefined;
251-
const user_input = stdin.readUntilDelimiterOrEof(&buffer, '\n') catch |err| switch (err) {
258+
try stdout.interface.print("{s}Bun v{s} is not installed. Do you want to install it? [y/N]{s} ", .{ c.yellow, version, c.reset });
259+
try stdout.interface.flush();
260+
const user_input = stdin.interface.takeDelimiterExclusive('\n') catch |err| switch (err) {
252261
error.StreamTooLong => "N",
262+
error.EndOfStream => "N",
253263
else => return err,
254-
} orelse "N";
264+
};
255265

256266
if (mem.eql(u8, user_input, "y")) return;
257267
} else {
258268
// Non-interactive mode, just display message and abort
259-
try stdout.print("{s}Bun v{s} is not installed. Run in an interactive terminal to install or set BUNV_AUTO_INSTALL=1.{s}\n", .{ c.yellow, version, c.reset });
269+
try stdout.interface.print("{s}Bun v{s} is not installed. Run in an interactive terminal to install or set BUNV_AUTO_INSTALL=1.{s}\n", .{ c.yellow, version, c.reset });
260270
}
261271

262272
std.debug.print("Installation aborted by user\n", .{});

0 commit comments

Comments
 (0)