Skip to content

Commit e7234da

Browse files
fix: use semantic version comparison and handle config parse errors
Replace string comparison with proper semver parsing (fixes 0.9.0 > 0.19.0 bug). Handle json.Unmarshal error for user config instead of silently ignoring malformed config.json. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
1 parent 003e23b commit e7234da

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

internal/updater/updater.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
"path/filepath"
1010
"runtime"
11+
"strconv"
12+
"strings"
1113
"sync"
1214
"time"
1315

@@ -53,7 +55,9 @@ func loadUserConfig() UserConfig {
5355
if err != nil {
5456
return cfg
5557
}
56-
json.Unmarshal(data, &cfg)
58+
if err := json.Unmarshal(data, &cfg); err != nil {
59+
return cfg
60+
}
5761
if cfg.AutoUpdate == "" {
5862
cfg.AutoUpdate = AutoUpdateEnabled
5963
}
@@ -196,13 +200,41 @@ func isNewerVersion(latest, current string) bool {
196200
if latest == "" {
197201
return false
198202
}
199-
// Dev builds (built from source without version injection) never auto-update
200203
if current == "dev" {
201204
return false
202205
}
203206
latestClean := trimVersionPrefix(latest)
204207
currentClean := trimVersionPrefix(current)
205-
return latestClean != currentClean && latestClean > currentClean
208+
return compareSemver(latestClean, currentClean) > 0
209+
}
210+
211+
func compareSemver(a, b string) int {
212+
aParts := parseSemver(a)
213+
bParts := parseSemver(b)
214+
for i := 0; i < 3; i++ {
215+
if aParts[i] != bParts[i] {
216+
if aParts[i] > bParts[i] {
217+
return 1
218+
}
219+
return -1
220+
}
221+
}
222+
return 0
223+
}
224+
225+
func parseSemver(v string) [3]int {
226+
var result [3]int
227+
parts := strings.SplitN(v, ".", 3)
228+
for i, p := range parts {
229+
if i >= 3 {
230+
break
231+
}
232+
n, err := strconv.Atoi(p)
233+
if err == nil {
234+
result[i] = n
235+
}
236+
}
237+
return result
206238
}
207239

208240
func trimVersionPrefix(v string) string {

0 commit comments

Comments
 (0)