@@ -195,11 +195,23 @@ func findGoModFiles(root string) []string {
195195// A regular expression for the Go toolchain version syntax.
196196var toolchainVersionRe * regexp.Regexp = regexp .MustCompile (`(?m)^([0-9]+\.[0-9]+(\.[0-9]+|rc[0-9]+))$` )
197197
198- // Returns true if the `go.mod` file specifies a Go language version, that version is `1.21` or greater, and
199- // there is no `toolchain` directive, and the Go language version is not a valid toolchain version.
198+ // Returns true if the `go.mod` file specifies a Go language version which is not of the format that
199+ // is expected by the Go 1.21 and Go 1.22 toolchains for toolchain versions, and there is no
200+ // explicit toolchain version declared.
200201func hasInvalidToolchainVersion (modFile * modfile.File ) bool {
201- return modFile .Toolchain == nil && modFile .Go != nil &&
202- ! toolchainVersionRe .Match ([]byte (modFile .Go .Version )) && util .NewSemVer (modFile .Go .Version ).IsAtLeast (toolchain .V1_21 )
202+ if modFile .Toolchain != nil {
203+ // There is an explicit toolchain directive, so it doesn't matter what format the
204+ // Go language version is in, since it will not be used as a fallback toolchain version.
205+ return false
206+ } else if modFile .Go != nil && ! toolchainVersionRe .Match ([]byte (modFile .Go .Version )) {
207+ // There's no explicit toolchain directive, but we have a language version which
208+ // does not match the toolchain version format in Go 1.21 and Go 1.22.
209+ // This is a problem if the installed Go toolchain is within that version range
210+ // as it will try to use the language version as the toolchain version.
211+ installed := util .NewSemVer (toolchain .GetEnvGoVersion ())
212+ return installed .IsAtLeast (toolchain .V1_21 ) && installed .IsOlderThan (toolchain .V1_23 )
213+ }
214+ return false
203215}
204216
205217// Given a list of `go.mod` file paths, try to parse them all. The resulting array of `GoModule` objects
0 commit comments