Skip to content

Commit 7560b4f

Browse files
authored
fix: improve Go detection (#5112)
1 parent beb8721 commit 7560b4f

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

pkg/config/config.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ func IsGoGreaterThanOrEqual(current, limit string) bool {
7575
}
7676

7777
func detectGoVersion() string {
78-
file, _ := gomoddirectives.GetModuleFile()
79-
80-
if file != nil && file.Go != nil && file.Go.Version != "" {
81-
return file.Go.Version
78+
goVersion := detectGoVersionFromGoMod()
79+
if goVersion != "" {
80+
return goVersion
8281
}
8382

8483
v := os.Getenv("GOVERSION")
@@ -88,3 +87,26 @@ func detectGoVersion() string {
8887

8988
return "1.17"
9089
}
90+
91+
// detectGoVersionFromGoMod tries to get Go version from go.mod.
92+
// It returns `toolchain` version if present,
93+
// else it returns `go` version if present,
94+
// else it returns empty.
95+
func detectGoVersionFromGoMod() string {
96+
file, _ := gomoddirectives.GetModuleFile()
97+
if file == nil {
98+
return ""
99+
}
100+
101+
// The toolchain exists only if 'toolchain' version > 'go' version.
102+
// If 'toolchain' version <= 'go' version, `go mod tidy` will remove 'toolchain' version from go.mod.
103+
if file.Toolchain != nil && file.Toolchain.Name != "" {
104+
return strings.TrimPrefix(file.Toolchain.Name, "go")
105+
}
106+
107+
if file.Go != nil && file.Go.Version != "" {
108+
return file.Go.Version
109+
}
110+
111+
return ""
112+
}

0 commit comments

Comments
 (0)