Skip to content

Commit d32333b

Browse files
fix: default version (#108)
* fix: default version * fix: default version
1 parent f98d5bf commit d32333b

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

src/core/alias.zig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ pub fn set_version(ctx: *context.CliContext, version: []const u8, is_zls: bool)
6767
// For Zig, point to zvm binary for smart version detection
6868
try update_current_to_zvm(ctx, symlink_path);
6969

70+
// Save the default version for when no build.zig.zon exists
71+
try save_default_version(ctx, version);
72+
7073
// Print success message for smart mode
7174
var stdout_buffer: [io_buffer_size]u8 = undefined;
7275
var stdout_writer = std.fs.File.Writer.init(std.fs.File.stdout(), &stdout_buffer);
@@ -76,6 +79,41 @@ pub fn set_version(ctx: *context.CliContext, version: []const u8, is_zls: bool)
7679
}
7780
}
7881

82+
fn save_default_version(ctx: *context.CliContext, version: []const u8) !void {
83+
var zm_path_buffer = try ctx.acquire_path_buffer();
84+
defer zm_path_buffer.reset();
85+
86+
const home_dir = ctx.get_home_dir();
87+
var stream = std.io.fixedBufferStream(zm_path_buffer.slice());
88+
89+
if (util_tool.getenv_cross_platform("XDG_DATA_HOME")) |xdg_data| {
90+
try stream.writer().print("{s}/.zm", .{xdg_data});
91+
} else {
92+
try stream.writer().print("{s}/.local/share/.zm", .{home_dir});
93+
}
94+
95+
const zm_dir = try zm_path_buffer.set(stream.getWritten());
96+
97+
// Ensure .zm directory exists
98+
std.fs.makeDirAbsolute(zm_dir) catch |err| switch (err) {
99+
error.PathAlreadyExists => {},
100+
else => return err,
101+
};
102+
103+
// Use a separate buffer for the config file path
104+
var config_path_buffer = try ctx.acquire_path_buffer();
105+
defer config_path_buffer.reset();
106+
107+
var config_stream = std.io.fixedBufferStream(config_path_buffer.slice());
108+
try config_stream.writer().print("{s}/default_version", .{zm_dir});
109+
const config_path = try config_path_buffer.set(config_stream.getWritten());
110+
111+
const file = try std.fs.cwd().createFile(config_path, .{});
112+
defer file.close();
113+
114+
try file.writeAll(version);
115+
}
116+
79117
fn update_current_to_zvm(ctx: *context.CliContext, symlink_path: []const u8) !void {
80118
assert(symlink_path.len > 0);
81119

src/core/detect_version.zig

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,24 @@ pub fn detect_version(ctx: *Context.CliContext, args: []const []const u8) !Smart
3939
};
4040
}
4141

42+
// Check if user has set a default version with 'zvm use'
43+
if (try find_default_version(ctx)) |default_version| {
44+
if (!is_ci_environment()) {
45+
log.info("No build.zig.zon found. Using default version: {s}", .{default_version});
46+
}
47+
return SmartVersionResult{
48+
.version = default_version,
49+
.source = .current,
50+
.allocator = ctx.get_allocator(),
51+
};
52+
}
53+
4254
if (!is_ci_environment()) {
4355
log.info("No build.zig.zon found. Defaulting to master version.", .{});
4456
log.info("Consider creating build.zig.zon with 'minimum_zig_version' field for reproducible builds.", .{});
4557
}
4658

47-
// Default to master when no build.zig.zon found - this is user-friendly
59+
// Default to master when no build.zig.zon found and no default set
4860
return SmartVersionResult{
4961
.version = "master",
5062
.source = .current,
@@ -260,6 +272,36 @@ pub fn is_ci_environment() bool {
260272
return false;
261273
}
262274

275+
fn find_default_version(ctx: *Context.CliContext) !?[]const u8 {
276+
// Look for a default version config file
277+
var config_path_buffer = try ctx.acquire_path_buffer();
278+
defer config_path_buffer.reset();
279+
280+
const home_dir = ctx.get_home_dir();
281+
var stream = std.io.fixedBufferStream(config_path_buffer.slice());
282+
283+
if (util_tool.getenv_cross_platform("XDG_DATA_HOME")) |xdg_data| {
284+
try stream.writer().print("{s}/.zm/default_version", .{xdg_data});
285+
} else {
286+
try stream.writer().print("{s}/.local/share/.zm/default_version", .{home_dir});
287+
}
288+
289+
const config_path = try config_path_buffer.set(stream.getWritten());
290+
291+
const file = std.fs.cwd().openFile(config_path, .{}) catch return null;
292+
defer file.close();
293+
294+
var version_buffer: [32]u8 = undefined;
295+
const bytes_read = file.readAll(&version_buffer) catch return null;
296+
if (bytes_read == 0) return null;
297+
298+
// Trim whitespace
299+
const content = std.mem.trim(u8, version_buffer[0..bytes_read], " \t\n\r");
300+
if (content.len == 0) return null;
301+
302+
return try ctx.get_allocator().dupe(u8, content);
303+
}
304+
263305
fn log_version_suggestion() void {
264306
log.info("No build.zig.zon found. Consider creating one with 'minimum_zig_version' field, or specify version on command line (e.g., 'zig 0.13.0 build')", .{});
265307
}

0 commit comments

Comments
 (0)