File tree Expand file tree Collapse file tree 1 file changed +26
-4
lines changed Expand file tree Collapse file tree 1 file changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -75,10 +75,9 @@ func IsGoGreaterThanOrEqual(current, limit string) bool {
7575}
7676
7777func 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+ }
You can’t perform that action at this time.
0 commit comments