Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ""
}
Loading