@@ -16,6 +16,7 @@ import (
16
16
"golang.org/x/mod/semver"
17
17
18
18
"github.com/github/codeql-go/extractor/autobuilder"
19
+ "github.com/github/codeql-go/extractor/diagnostics"
19
20
"github.com/github/codeql-go/extractor/util"
20
21
)
21
22
@@ -249,12 +250,30 @@ func main() {
249
250
depMode := GoGetNoModules
250
251
modMode := ModUnset
251
252
needGopath := true
253
+ goDirectiveFound := false
254
+ goDirectiveVersion := "1.16"
252
255
if _ , present := os .LookupEnv ("GO111MODULE" ); ! present {
253
256
os .Setenv ("GO111MODULE" , "auto" )
254
257
}
255
258
if util .FileExists ("go.mod" ) {
256
259
depMode = GoGetWithModules
257
260
needGopath = false
261
+ versionRe := regexp .MustCompile (`(?m)^go[ \t\r]+([0-9]+\.[0-9]+)$` )
262
+ goMod , err := ioutil .ReadFile ("go.mod" )
263
+ if err != nil {
264
+ log .Println ("Failed to read go.mod to check for missing Go version" )
265
+ } else {
266
+ matches := versionRe .FindSubmatch (goMod )
267
+ if matches != nil {
268
+ goDirectiveFound = true
269
+ if len (matches ) > 1 {
270
+ goDirectiveVersion = "v" + string (matches [1 ])
271
+ if semver .Compare (goDirectiveVersion , getEnvGoSemVer ()) >= 0 {
272
+ diagnostics .EmitNewerGoVersionNeeded ()
273
+ }
274
+ }
275
+ }
276
+ }
258
277
log .Println ("Found go.mod, enabling go modules" )
259
278
} else if util .FileExists ("Gopkg.toml" ) {
260
279
depMode = Dep
@@ -283,10 +302,7 @@ func main() {
283
302
// we work around this by adding an explicit go version of 1.13, which is the last version
284
303
// where this is not an issue
285
304
if depMode == GoGetWithModules {
286
- goMod , err := ioutil .ReadFile ("go.mod" )
287
- if err != nil {
288
- log .Println ("Failed to read go.mod to check for missing Go version" )
289
- } else if versionRe := regexp .MustCompile (`(?m)^go[ \t\r]+[0-9]+\.[0-9]+$` ); ! versionRe .Match (goMod ) {
305
+ if ! goDirectiveFound {
290
306
// if the go.mod does not contain a version line
291
307
modulesTxt , err := ioutil .ReadFile ("vendor/modules.txt" )
292
308
if err != nil {
0 commit comments