Skip to content

Commit 6f6de1a

Browse files
committed
Go: Only use EmitInvalidToolchainVersion if installed toolchain is >=1.21 && <1.23
1 parent e1c2805 commit 6f6de1a

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

go/extractor/project/project.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,23 @@ func findGoModFiles(root string) []string {
195195
// A regular expression for the Go toolchain version syntax.
196196
var 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.
200201
func 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

go/extractor/toolchain/toolchain.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var V1_14 = util.NewSemVer("v1.14.0")
1717
var V1_16 = util.NewSemVer("v1.16.0")
1818
var V1_18 = util.NewSemVer("v1.18.0")
1919
var V1_21 = util.NewSemVer("v1.21.0")
20+
var V1_23 = util.NewSemVer("v1.23.0")
2021

2122
// Check if Go is installed in the environment.
2223
func IsInstalled() bool {

0 commit comments

Comments
 (0)