Skip to content

Commit d5439ce

Browse files
committed
fix: Actually use latest version of bun when no project version is found
Before, we weren't actually looking up the latest local version, just grabbing the first version returned by listing files in the `~/.bunv` dir.
1 parent 2806b13 commit d5439ce

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/vm.zig

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ pub fn getInstalledVersions(allocator: mem.Allocator, config_dir: []const u8) !s
3737
}
3838
}
3939

40+
// Sort by semantic version
41+
mem.sort([]const u8, result.items, {}, struct {
42+
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
43+
const semver_a = std.SemanticVersion.parse(a) catch return true;
44+
const semver_b = std.SemanticVersion.parse(b) catch return false;
45+
return semver_a.order(semver_b) == .lt;
46+
}
47+
}.lessThan);
48+
4049
return result;
4150
}
4251

@@ -107,8 +116,11 @@ pub fn getLatestLocalVersion(allocator: mem.Allocator, is_debug: bool, config_di
107116
if (installed_versions.items.len == 0) {
108117
return null;
109118
}
110-
// TODO: installed versions are not sorted, so naively assuming the first item is the latest is wrong.
111-
return try allocator.dupe(u8, installed_versions.items[0]);
119+
120+
// Versions are sorted, so the last item is the latest
121+
const latest = installed_versions.items[installed_versions.items.len - 1];
122+
if (is_debug) std.debug.print("Latest local version: {s}\n", .{latest});
123+
return try allocator.dupe(u8, latest);
112124
}
113125

114126
pub fn getLatestRemoteVersion(allocator: mem.Allocator, is_debug: bool) ![]const u8 {

0 commit comments

Comments
 (0)