Skip to content

Commit 15a6308

Browse files
committed
Go: Refactor condition for EmitInvalidToolchainVersion into separate function
1 parent 9082972 commit 15a6308

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

go/extractor/project/project.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ func findGoModFiles(root string) []string {
179179
// A regular expression for the Go toolchain version syntax.
180180
var toolchainVersionRe *regexp.Regexp = regexp.MustCompile(`(?m)^([0-9]+\.[0-9]+\.[0-9]+)$`)
181181

182+
// Returns true if the `go.mod` file specifies a Go language version, that version is `1.21` or greater, and
183+
// there is no `toolchain` directive, and the Go language version is not a valid toolchain version.
184+
func hasInvalidToolchainVersion(modFile *modfile.File) bool {
185+
return modFile.Toolchain == nil && modFile.Go != nil &&
186+
!toolchainVersionRe.Match([]byte(modFile.Go.Version)) && semver.Compare("v"+modFile.Go.Version, "v1.21.0") >= 0
187+
}
188+
182189
// Given a list of `go.mod` file paths, try to parse them all. The resulting array of `GoModule` objects
183190
// will be the same length as the input array and the objects will contain at least the `go.mod` path.
184191
// If parsing the corresponding file is successful, then the parsed contents will also be available.
@@ -209,8 +216,7 @@ func LoadGoModules(emitDiagnostics bool, goModFilePaths []string) []*GoModule {
209216
// there is no `toolchain` directive, check that it is a valid Go toolchain version. Otherwise,
210217
// `go` commands which try to download the right version of the Go toolchain will fail. We detect
211218
// this situation and emit a diagnostic.
212-
if modFile.Toolchain == nil && modFile.Go != nil &&
213-
!toolchainVersionRe.Match([]byte(modFile.Go.Version)) && semver.Compare("v"+modFile.Go.Version, "v1.21.0") >= 0 {
219+
if hasInvalidToolchainVersion(modFile) {
214220
diagnostics.EmitInvalidToolchainVersion(goModFilePath, modFile.Go.Version)
215221
}
216222
}

0 commit comments

Comments
 (0)