Skip to content

Commit 3852a2d

Browse files
authored
Make k6 get it version entirely from the build info (#5079)
Try to more accurately asses the version of k6 also in cases where it is build with xk6. This should remove the need to bump the constant with the version.
1 parent 3dfe125 commit 3852a2d

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

internal/cmd/version.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/spf13/cobra"
11+
1112
"go.k6.io/k6/cmd/state"
1213
"go.k6.io/k6/ext"
1314
"go.k6.io/k6/internal/build"
@@ -16,6 +17,7 @@ import (
1617
const (
1718
commitKey = "commit"
1819
commitDirtyKey = "commit_dirty"
20+
mainK6Path = "go.k6.io/k6"
1921
)
2022

2123
// fullVersion returns the maximally full version and build information for
@@ -44,13 +46,13 @@ func fullVersion() string {
4446
}
4547

4648
// versionDetails returns the structured details about version
47-
func versionDetails() map[string]interface{} {
49+
func versionDetails() map[string]any {
4850
v := build.Version
4951
if !strings.HasPrefix(v, "v") {
5052
v = "v" + v
5153
}
5254

53-
details := map[string]interface{}{
55+
details := map[string]any{
5456
"version": v,
5557
"go_version": runtime.Version(),
5658
"go_os": runtime.GOOS,
@@ -62,35 +64,33 @@ func versionDetails() map[string]interface{} {
6264
return details
6365
}
6466

65-
var (
66-
commit string
67-
dirty bool
68-
)
69-
for _, s := range buildInfo.Settings {
70-
switch s.Key {
71-
case "vcs.revision":
72-
commitLen := 10
73-
if len(s.Value) < commitLen {
74-
commitLen = len(s.Value)
67+
if buildInfo.Main.Path == mainK6Path {
68+
details["version"] = buildInfo.Main.Version
69+
if buildInfo.Main.Version == "(devel)" {
70+
details["version"] = v
71+
details[commitKey] = "devel"
72+
}
73+
for _, s := range buildInfo.Settings {
74+
switch s.Key {
75+
case "vcs.revision":
76+
commitLen := min(len(s.Value), 10)
77+
details[commitKey] = s.Value[:commitLen]
78+
case "vcs.modified":
79+
if s.Value == "true" {
80+
details[commitDirtyKey] = true
81+
}
82+
default:
7583
}
76-
commit = s.Value[:commitLen]
77-
case "vcs.modified":
78-
if s.Value == "true" {
79-
dirty = true
84+
}
85+
} else {
86+
for _, dep := range buildInfo.Deps {
87+
if dep.Path == mainK6Path {
88+
details["version"] = dep.Version
89+
break
8090
}
81-
default:
8291
}
8392
}
8493

85-
if commit == "" {
86-
return details
87-
}
88-
89-
details[commitKey] = commit
90-
if dirty {
91-
details[commitDirtyKey] = true
92-
}
93-
9494
return details
9595
}
9696

internal/cmd/version_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/stretchr/testify/assert"
9+
910
"go.k6.io/k6/internal/build"
1011
"go.k6.io/k6/internal/cmd/tests"
1112
)
@@ -79,4 +80,5 @@ func TestVersionJSONSubCommand(t *testing.T) {
7980
assert.Equal(t, runtime.Version(), details["go_version"])
8081
assert.Equal(t, runtime.GOOS, details["go_os"])
8182
assert.Equal(t, runtime.GOARCH, details["go_arch"])
83+
assert.Equal(t, "devel", details[commitKey])
8284
}

0 commit comments

Comments
 (0)