@@ -179,6 +179,13 @@ func findGoModFiles(root string) []string {
179179// A regular expression for the Go toolchain version syntax.
180180var 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.
@@ -196,7 +203,7 @@ func LoadGoModules(emitDiagnostics bool, goModFilePaths []string) []*GoModule {
196203 continue
197204 }
198205
199- modFile , err := modfile .ParseLax (goModFilePath , modFileSrc , nil )
206+ modFile , err := modfile .Parse (goModFilePath , modFileSrc , nil )
200207
201208 if err != nil {
202209 log .Printf ("Unable to parse %s: %s.\n " , goModFilePath , err .Error ())
@@ -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