Skip to content

Commit

Permalink
fix: improve Go detection (#5112)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Nov 7, 2024
1 parent beb8721 commit 7560b4f
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ func IsGoGreaterThanOrEqual(current, limit string) bool {
}

func detectGoVersion() string {
file, _ := gomoddirectives.GetModuleFile()

if file != nil && file.Go != nil && file.Go.Version != "" {
return file.Go.Version
goVersion := detectGoVersionFromGoMod()
if goVersion != "" {
return goVersion
}

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

return "1.17"
}

// detectGoVersionFromGoMod tries to get Go version from go.mod.
// It returns `toolchain` version if present,
// else it returns `go` version if present,
// else it returns empty.
func detectGoVersionFromGoMod() string {
file, _ := gomoddirectives.GetModuleFile()
if file == nil {
return ""
}

// The toolchain exists only if 'toolchain' version > 'go' version.
// If 'toolchain' version <= 'go' version, `go mod tidy` will remove 'toolchain' version from go.mod.
if file.Toolchain != nil && file.Toolchain.Name != "" {
return strings.TrimPrefix(file.Toolchain.Name, "go")
}

if file.Go != nil && file.Go.Version != "" {
return file.Go.Version
}

return ""
}

0 comments on commit 7560b4f

Please sign in to comment.