File tree 1 file changed +26
-4
lines changed
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 {
75
75
}
76
76
77
77
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
82
81
}
83
82
84
83
v := os .Getenv ("GOVERSION" )
@@ -88,3 +87,26 @@ func detectGoVersion() string {
88
87
89
88
return "1.17"
90
89
}
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