forked from lispking/zvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.zig
More file actions
517 lines (438 loc) · 20.7 KB
/
Copy pathinstall.zig
File metadata and controls
517 lines (438 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//! Zig version installation command.
//! Handles the full install flow: fetch version map → resolve version → download
//! (with mirror support) → verify SHA256 → extract → rename → symlink.
//! Optionally installs ZLS (Zig Language Server) alongside Zig.
const std = @import("std");
const zvm_mod = @import("../core/zvm.zig");
const cli = @import("../cli.zig");
const platform = @import("../core/platform.zig");
const Console = @import("../core/Console.zig");
const version_map = @import("../network/version_map.zig");
const http_client = @import("../network/http_client.zig");
const archive = @import("archive.zig");
const crypto = @import("../core/crypto.zig");
/// Main entry point for the `zvm install` command.
/// Resolves the requested version, checks if already installed,
/// downloads, verifies, extracts, and activates.
pub fn run(
zvm: *zvm_mod.ZVM,
allocator: std.mem.Allocator,
version: []const u8,
flags: cli.InstallFlags,
console: Console,
) !void {
// Get system info for platform-specific download
const sys_info = platform.zigStyleSystemInfo();
var platform_buf: [128]u8 = undefined;
const target = platform.platformTarget(&platform_buf, sys_info);
// Fetch version map from configured URL
console.plain("Fetching version map...", .{});
const parsed_map = version_map.fetchVersionMap(allocator, zvm.io, zvm.environ_map, zvm.settings.version_map_url, zvm.settings.proxy) catch |err| {
console.err("Failed to fetch version map", .{});
return err;
};
defer parsed_map.deinit();
const vmap = &parsed_map.value.object;
// Check if already installed (skip for --force or master with newer version available)
if (!flags.force and zvm.isVersionInstalled(version)) {
if (std.mem.eql(u8, version, "master")) {
// For master, check if the installed version matches the latest
if (version_map.getMasterVersion(vmap)) |remote_ver| {
var ver_buf: [std.fs.max_path_bytes]u8 = undefined;
const ver_path = zvm.versionPath(&ver_buf, version);
const zig_path = try std.fmt.allocPrint(allocator, "{s}/zig", .{ver_path});
defer allocator.free(zig_path);
const result = std.process.run(allocator, zvm.io, .{
.argv = &.{ zig_path, "version" },
.stdout_limit = .limited(1024),
}) catch {
try installVersion(zvm, allocator, version, target, vmap, flags, console);
return;
};
defer allocator.free(result.stdout);
defer allocator.free(result.stderr);
const installed_ver = std.mem.trim(u8, result.stdout, " \n\r");
if (std.mem.eql(u8, installed_ver, remote_ver)) {
console.plain("Master is already up to date ({s}). Use --force to reinstall.", .{installed_ver});
return;
}
}
} else {
console.plain("Zig {s} is already installed. Use --force to reinstall.", .{version});
return;
}
}
try installVersion(zvm, allocator, version, target, vmap, flags, console);
}
/// Core installation logic: download, verify, extract, rename, symlink.
fn installVersion(
zvm: *zvm_mod.ZVM,
allocator: std.mem.Allocator,
version: []const u8,
target: []const u8,
vmap: *const version_map.VersionMap,
flags: cli.InstallFlags,
console: Console,
) !void {
// Resolve the tarball download URL from the version map
const tar_url = version_map.getTarPath(version, target, vmap) catch {
console.err("Failed to find download for this version/platform", .{});
return error.UnsupportedVersion;
};
// Get expected SHA256 checksum (optional — warns if missing)
const shasum = version_map.getVersionShasum(version, target, vmap) catch blk: {
console.plain("Warning: No shasum found, skipping verification.", .{});
break :blk null;
};
// Determine local archive filename from the download URL
const archive_name = if (std.mem.lastIndexOfScalar(u8, tar_url, '/')) |idx| tar_url[idx + 1 ..] else "zig-archive";
var archive_path_buf: [std.fs.max_path_bytes * 2]u8 = undefined;
const archive_path = try std.fmt.bufPrint(&archive_path_buf, "{s}/{s}", .{ zvm.cache_dir, archive_name });
// Download archive (with optional mirror support)
console.plain("Downloading Zig {s}...", .{version});
const stdout = console.stdout.writer;
const actual_url = if (flags.nomirror) blk: {
try http_client.downloadToFileWithProxy(allocator, zvm.io, zvm.environ_map, tar_url, archive_path, zvm.settings.proxy, stdout);
break :blk tar_url;
} else blk: {
const mirror_url = http_client.attemptMirrorDownload(allocator, zvm.io, zvm.environ_map, zvm.settings.mirror_list_url, tar_url, archive_path, stdout, stdout, &zvm.settings) catch {
try http_client.downloadToFileWithProxy(allocator, zvm.io, zvm.environ_map, tar_url, archive_path, zvm.settings.proxy, stdout);
break :blk tar_url;
};
break :blk mirror_url;
};
// Show the actual download source
if (actual_url.ptr != tar_url.ptr) {
console.plain(" from: {s}", .{actual_url});
allocator.free(actual_url);
}
// Verify SHA256 checksum
if (shasum) |expected| {
console.plain("Verifying checksum...", .{});
const matches = crypto.verifyFileSha256(zvm.io, archive_path, expected) catch {
console.err("Failed to verify checksum", .{});
return error.ShasumMismatch;
};
if (!matches) {
// Mirror tarball may have a different shasum than the official source.
// Fallback to the original URL and re-verify.
if (actual_url.ptr != tar_url.ptr) {
console.warn("Mirror checksum mismatch, falling back to official source...", .{});
std.Io.Dir.cwd().deleteFile(zvm.io, archive_path) catch {};
try http_client.downloadToFileWithProxy(allocator, zvm.io, zvm.environ_map, tar_url, archive_path, zvm.settings.proxy, stdout);
const fallback_matches = crypto.verifyFileSha256(zvm.io, archive_path, expected) catch {
console.err("Failed to verify checksum", .{});
return error.ShasumMismatch;
};
if (!fallback_matches) {
console.err("SHA256 checksum mismatch!", .{});
std.Io.Dir.cwd().deleteFile(zvm.io, archive_path) catch {};
return error.ShasumMismatch;
}
} else {
console.err("SHA256 checksum mismatch!", .{});
std.Io.Dir.cwd().deleteFile(zvm.io, archive_path) catch {};
return error.ShasumMismatch;
}
}
console.success("Checksum verified.", .{});
}
// Extract the archive
console.plain("Extracting...", .{});
archive.extractArchive(allocator, zvm.io, archive_path, zvm.data_dir) catch {
console.err("Failed to extract archive", .{});
return error.ExtractionFailed;
};
// Rename the extracted directory (e.g., zig-macos-x86_64-0.13.0 → 0.13.0)
try renameExtractedDir(zvm, allocator, version, target, console);
// Only auto-activate if there is no currently active version
const active_opt = zvm.getActiveVersion(allocator);
const has_active = active_opt != null;
if (active_opt) |active| {
allocator.free(active);
} else {
try zvm.setBin(version);
// On Windows, ensure the bin directory is in the user PATH
if (platform.isWindows()) {
var bin_buf: [std.fs.max_path_bytes]u8 = undefined;
const bin_path = zvm.binPath(&bin_buf);
if (platform.addToUserPath(zvm.io, bin_path)) |added| {
if (added) {
console.plain("Added zvm bin directory to PATH. Please restart your terminal for changes to take effect.", .{});
}
} else |err| {
console.warn("Failed to update PATH ({s}). Please add {s} to your PATH manually.", .{ @errorName(err), bin_path });
}
}
}
// Clean up the downloaded archive
std.Io.Dir.cwd().deleteFile(zvm.io, archive_path) catch {};
// Clean up any leftover extracted zig-* directories
cleanupExtractedDirs(zvm);
console.success("Installed Zig", .{});
// Verify the installed binary can actually compile (detect platform compatibility issues)
console.plain("Verifying installation...", .{});
if (!try verifyInstall(zvm, allocator, version)) {
console.warn("This Zig version has linking issues on your platform.", .{});
console.plain(
\\This is a known issue with official Zig releases on macOS 26+.
\\The binary can run basic commands but cannot compile programs.
\\
\\Suggested fixes:
\\ 1. Install the latest nightly: zvm install master
\\ 2. Use Mach engine builds: zvm vmu zig mach && zvm install <version>
\\
, .{});
return;
}
if (has_active) {
console.plain("Use `zvm use {s}` to activate this version.", .{version});
} else {
console.plain("Now using Zig {s}", .{version});
}
// Optionally install ZLS alongside Zig
if (flags.zls) {
try installZls(zvm, allocator, version, flags.full, console);
}
}
/// Rename the extracted archive directory to match the version name.
/// Zig archives extract to directories like "zig-macos-x86_64-0.13.0".
/// We rename them to just "0.13.0" for cleaner path management.
/// If the version directory already exists (--force), it is removed first.
fn renameExtractedDir(
zvm: *zvm_mod.ZVM,
allocator: std.mem.Allocator,
version: []const u8,
target: []const u8,
console: Console,
) !void {
_ = allocator;
// Find the extracted directory (starts with "zig-" and matches both arch and os).
// The target string is "arch-os" (e.g., "aarch64-macos"), but archive directories
// use "os-arch" order (e.g., "zig-macos-aarch64-0.13.0"), so we match components separately.
const dash_idx = std.mem.indexOfScalar(u8, target, '-') orelse return;
const arch_part = target[0..dash_idx];
const os_part = target[dash_idx + 1 ..];
var dir = std.Io.Dir.cwd().openDir(zvm.io, zvm.data_dir, .{ .iterate = true }) catch return;
defer dir.close(zvm.io);
var found: ?[]const u8 = null;
var iter = dir.iterate();
while (try iter.next(zvm.io)) |entry| {
if (entry.kind != .directory) continue;
if (std.mem.startsWith(u8, entry.name, "zig-") and
std.mem.containsAtLeast(u8, entry.name, 1, arch_part) and
std.mem.containsAtLeast(u8, entry.name, 1, os_part))
{
found = entry.name;
break;
}
}
// No extracted directory found — version dir may already exist
if (found == null) return;
// Remove existing version directory if it exists (for --force reinstalls)
var version_buf: [std.fs.max_path_bytes]u8 = undefined;
const version_path = zvm.versionPath(&version_buf, version);
std.Io.Dir.cwd().deleteTree(zvm.io, version_path) catch {};
// Rename the extracted directory to the version name
dir.rename(found.?, dir, version, zvm.io) catch {
console.err("Failed to rename extracted directory", .{});
return;
};
}
/// Remove any leftover zig-* directories from failed or interrupted extractions.
fn cleanupExtractedDirs(zvm: *zvm_mod.ZVM) void {
var dir = std.Io.Dir.cwd().openDir(zvm.io, zvm.data_dir, .{ .iterate = true }) catch return;
defer dir.close(zvm.io);
var iter = dir.iterate();
while (iter.next(zvm.io) catch return) |entry| {
if (entry.kind != .directory) continue;
if (std.mem.startsWith(u8, entry.name, "zig-")) {
var buf: [std.fs.max_path_bytes * 2]u8 = undefined;
const path = std.fmt.bufPrint(&buf, "{s}/{s}", .{ zvm.data_dir, entry.name }) catch continue;
std.Io.Dir.cwd().deleteTree(zvm.io, path) catch {};
}
}
}
/// Verify the installed Zig binary can actually compile programs.
/// Creates a minimal Zig source file, compiles it, and checks for linking errors.
/// Returns true if the binary works correctly, false if it has platform compatibility issues.
fn verifyInstall(
zvm: *zvm_mod.ZVM,
allocator: std.mem.Allocator,
version: []const u8,
) !bool {
var ver_buf: [std.fs.max_path_bytes]u8 = undefined;
const ver_path = zvm.versionPath(&ver_buf, version);
const zig_path = try std.fmt.allocPrint(allocator, "{s}/zig", .{ver_path});
defer allocator.free(zig_path);
// Create a temporary test file in the cache directory
var test_buf: [std.fs.max_path_bytes * 2]u8 = undefined;
const test_path = try std.fmt.bufPrint(&test_buf, "{s}/_zvm_smoke_test.zig", .{zvm.cache_dir});
defer std.Io.Dir.cwd().deleteFile(zvm.io, test_path) catch {};
const test_file = try std.Io.Dir.cwd().createFile(zvm.io, test_path, .{});
defer test_file.close(zvm.io);
var test_writer_buf: [256]u8 = undefined;
var test_writer = test_file.writer(zvm.io, &test_writer_buf);
try test_writer.interface.writeAll("pub fn main() void {}\n");
try test_writer.interface.flush();
// Try to compile the test file
const result = std.process.run(allocator, zvm.io, .{
.argv = &.{ zig_path, "build-exe", test_path, "-fno-emit-bin" },
.stdout_limit = .limited(4096),
}) catch return false;
defer allocator.free(result.stdout);
defer allocator.free(result.stderr);
// Check for linking errors (undefined symbol errors indicate platform incompatibility)
if (result.term == .exited and result.term.exited == 0) {
return true;
}
// Check stderr for "undefined symbol" — indicates macOS 26+ linking issue
if (std.mem.containsAtLeast(u8, result.stderr, 1, "undefined symbol")) {
return false;
}
// Other errors — might be a real issue, but not a platform compat problem
return true;
}
/// Install ZLS (Zig Language Server) for a specific Zig version.
/// Queries the ZLS select-version API, downloads the platform-specific binary,
/// and places it alongside the Zig installation.
fn installZls(
zvm: *zvm_mod.ZVM,
allocator: std.mem.Allocator,
version: []const u8,
full_compat: bool,
console: Console,
) !void {
console.plain("Installing ZLS for Zig {s}...", .{version});
// Get the installed Zig version string
var ver_buf: [std.fs.max_path_bytes]u8 = undefined;
const ver_path = zvm.versionPath(&ver_buf, version);
const zig_path = try std.fmt.allocPrint(allocator, "{s}/zig", .{ver_path});
defer allocator.free(zig_path);
const result = std.process.run(allocator, zvm.io, .{
.argv = &.{ zig_path, "version" },
.stdout_limit = .limited(1024),
}) catch {
console.err("Failed to get Zig version for ZLS lookup", .{});
return;
};
defer allocator.free(result.stdout);
defer allocator.free(result.stderr);
const zig_version = std.mem.trim(u8, result.stdout, " \n\r");
// Query ZLS select-version API with the Zig version and compatibility mode
const compat_mode = if (full_compat) "full" else "only-runtime";
const zls_url = try std.fmt.allocPrint(
allocator,
"{s}v1/zls/select-version?zig_version={s}&compatibility={s}",
.{ zvm.settings.zls_vmu, zig_version, compat_mode },
);
defer allocator.free(zls_url);
const zls_response = http_client.downloadToMemoryWithProxy(allocator, zvm.io, zvm.environ_map, zls_url, zvm.settings.proxy) catch {
console.err("Failed to query ZLS version", .{});
return;
};
defer allocator.free(zls_response);
const parsed = std.json.parseFromSlice(std.json.Value, allocator, zls_response, .{}) catch {
console.err("Failed to parse ZLS response", .{});
return;
};
defer parsed.deinit();
const zls_data = switch (parsed.value) {
.object => |obj| obj,
else => {
console.err("Invalid ZLS response", .{});
return;
},
};
// Find platform-specific download URL in the ZLS response
const sys_info = platform.zigStyleSystemInfo();
var plat_buf: [128]u8 = undefined;
const target = platform.platformTarget(&plat_buf, sys_info);
const plat_entry = zls_data.get(target) orelse {
console.err("No ZLS build found for your platform", .{});
return;
};
const plat_obj = switch (plat_entry) {
.object => |obj| obj,
else => {
console.err("Invalid ZLS platform entry", .{});
return;
},
};
const tarball_val = plat_obj.get("tarball") orelse {
console.err("No tarball URL in ZLS response", .{});
return;
};
const zls_tarball = switch (tarball_val) {
.string => |s| s,
else => {
console.err("Invalid tarball URL format", .{});
return;
},
};
// Download ZLS archive
const zls_archive_name = if (std.mem.lastIndexOfScalar(u8, zls_tarball, '/')) |idx| zls_tarball[idx + 1 ..] else "zls-archive";
var archive_buf: [std.fs.max_path_bytes * 2]u8 = undefined;
const zls_archive_path = try std.fmt.bufPrint(&archive_buf, "{s}/{s}", .{ zvm.cache_dir, zls_archive_name });
try http_client.downloadToFileWithProxy(allocator, zvm.io, zvm.environ_map, zls_tarball, zls_archive_path, zvm.settings.proxy, console.stdout.writer);
// Extract to a temporary directory
var temp_buf: [std.fs.max_path_bytes * 2]u8 = undefined;
const temp_dir = try std.fmt.bufPrint(&temp_buf, "{s}/zls-temp", .{zvm.cache_dir});
std.Io.Dir.cwd().createDirPath(zvm.io, temp_dir) catch {};
archive.extractArchive(allocator, zvm.io, zls_archive_path, temp_dir) catch {
console.err("Failed to extract ZLS archive", .{});
return;
};
// Find the zls binary in the extracted directory and copy it to the version dir.
// ZLS archives may have a flat layout (zls at root) or a nested one (dir/zls).
var temp_handle = std.Io.Dir.cwd().openDir(zvm.io, temp_dir, .{ .iterate = true }) catch return;
defer temp_handle.close(zvm.io);
var found = false;
var iter = temp_handle.iterate();
while (try iter.next(zvm.io)) |entry| {
// Flat layout: zls binary at root of archive
if (entry.kind == .file and (std.mem.eql(u8, entry.name, "zls") or std.mem.eql(u8, entry.name, "zls.exe"))) {
var src_buf: [std.fs.max_path_bytes * 2]u8 = undefined;
const src = try std.fmt.bufPrint(&src_buf, "{s}/{s}", .{ temp_dir, entry.name });
var dst_buf: [std.fs.max_path_bytes * 2]u8 = undefined;
const dst = try std.fmt.bufPrint(&dst_buf, "{s}/{s}/zls", .{ zvm.data_dir, version });
try platform.copyFile(zvm.io, src, dst);
_ = std.process.run(allocator, zvm.io, .{
.argv = &.{ "chmod", "+x", dst },
}) catch {};
console.success("Installed ZLS", .{});
found = true;
break;
}
// Nested layout: zls binary inside a subdirectory
if (entry.kind == .directory) {
var inner_buf: [std.fs.max_path_bytes * 2]u8 = undefined;
const inner_path = try std.fmt.bufPrint(&inner_buf, "{s}/{s}", .{ temp_dir, entry.name });
var inner_dir = std.Io.Dir.cwd().openDir(zvm.io, inner_path, .{ .iterate = true }) catch continue;
defer inner_dir.close(zvm.io);
var inner_iter = inner_dir.iterate();
while (try inner_iter.next(zvm.io)) |inner_entry| {
if (std.mem.eql(u8, inner_entry.name, "zls") or std.mem.eql(u8, inner_entry.name, "zls.exe")) {
var src_buf: [std.fs.max_path_bytes * 2]u8 = undefined;
const src = try std.fmt.bufPrint(&src_buf, "{s}/{s}/{s}", .{ temp_dir, entry.name, inner_entry.name });
var dst_buf: [std.fs.max_path_bytes * 2]u8 = undefined;
const dst = try std.fmt.bufPrint(&dst_buf, "{s}/{s}/zls", .{ zvm.data_dir, version });
try platform.copyFile(zvm.io, src, dst);
_ = std.process.run(allocator, zvm.io, .{
.argv = &.{ "chmod", "+x", dst },
}) catch {};
console.success("Installed ZLS", .{});
found = true;
break;
}
}
if (found) break;
}
}
if (!found) {
console.err("ZLS binary not found in archive", .{});
}
// Cleanup temporary files
std.Io.Dir.cwd().deleteFile(zvm.io, zls_archive_path) catch {};
std.Io.Dir.cwd().deleteTree(zvm.io, temp_dir) catch {};
}