@@ -16,6 +16,7 @@ import (
1616 "golang.org/x/mod/semver"
1717
1818 "github.com/github/codeql-go/extractor/autobuilder"
19+ "github.com/github/codeql-go/extractor/diagnostics"
1920 "github.com/github/codeql-go/extractor/util"
2021)
2122
@@ -249,12 +250,30 @@ func main() {
249250 depMode := GoGetNoModules
250251 modMode := ModUnset
251252 needGopath := true
253+ goDirectiveFound := false
254+ goDirectiveVersion := "1.16"
252255 if _ , present := os .LookupEnv ("GO111MODULE" ); ! present {
253256 os .Setenv ("GO111MODULE" , "auto" )
254257 }
255258 if util .FileExists ("go.mod" ) {
256259 depMode = GoGetWithModules
257260 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+ }
258277 log .Println ("Found go.mod, enabling go modules" )
259278 } else if util .FileExists ("Gopkg.toml" ) {
260279 depMode = Dep
@@ -283,10 +302,7 @@ func main() {
283302 // we work around this by adding an explicit go version of 1.13, which is the last version
284303 // where this is not an issue
285304 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 {
290306 // if the go.mod does not contain a version line
291307 modulesTxt , err := ioutil .ReadFile ("vendor/modules.txt" )
292308 if err != nil {
0 commit comments