@@ -179,6 +179,13 @@ func findGoModFiles(root string) []string {
179
179
// A regular expression for the Go toolchain version syntax.
180
180
var toolchainVersionRe * regexp.Regexp = regexp .MustCompile (`(?m)^([0-9]+\.[0-9]+\.[0-9]+)$` )
181
181
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
+
182
189
// Given a list of `go.mod` file paths, try to parse them all. The resulting array of `GoModule` objects
183
190
// will be the same length as the input array and the objects will contain at least the `go.mod` path.
184
191
// 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 {
196
203
continue
197
204
}
198
205
199
- modFile , err := modfile .ParseLax (goModFilePath , modFileSrc , nil )
206
+ modFile , err := modfile .Parse (goModFilePath , modFileSrc , nil )
200
207
201
208
if err != nil {
202
209
log .Printf ("Unable to parse %s: %s.\n " , goModFilePath , err .Error ())
@@ -209,8 +216,7 @@ func LoadGoModules(emitDiagnostics bool, goModFilePaths []string) []*GoModule {
209
216
// there is no `toolchain` directive, check that it is a valid Go toolchain version. Otherwise,
210
217
// `go` commands which try to download the right version of the Go toolchain will fail. We detect
211
218
// 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 ) {
214
220
diagnostics .EmitInvalidToolchainVersion (goModFilePath , modFile .Go .Version )
215
221
}
216
222
}
0 commit comments