From ac4d2c5c57fb41b42287cc0bad8b908eede8d01a Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Thu, 7 Nov 2024 15:40:46 +0100 Subject: [PATCH 1/2] fix: improve Go detection --- pkg/config/config.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 93b331bec457..c97a62293520 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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") @@ -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 exist 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 "" +} From ede2237f391b1b3d377f5360bc8ed15a6d50a87a Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Thu, 7 Nov 2024 16:34:30 +0100 Subject: [PATCH 2/2] fix: typos --- pkg/config/config.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index c97a62293520..b863b329f931 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -89,8 +89,8 @@ func detectGoVersion() string { } // detectGoVersionFromGoMod tries to get Go version from go.mod. -// It returns toolchain version if present, -// else it returns go version if present, +// It returns `toolchain` version if present, +// else it returns `go` version if present, // else it returns empty. func detectGoVersionFromGoMod() string { file, _ := gomoddirectives.GetModuleFile() @@ -98,7 +98,7 @@ func detectGoVersionFromGoMod() string { return "" } - // The toolchain exist only if 'toolchain' version > 'go' version. + // 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")