Skip to content

Commit c0cc1c3

Browse files
committed
Emit diagnostic to pass second integration test
1 parent 3f805d3 commit c0cc1c3

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

go/extractor/diagnostics/diagnostics.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,13 @@ func EmitPackageDifferentOSArchitecture(pkgPath string) {
101101
"", 0, 0, 0, 0,
102102
)
103103
}
104+
105+
func EmitNewerGoVersionNeeded() {
106+
emitDiagnostic("go/autobuilder/newer-go-version-needed",
107+
"Newer Go version needed",
108+
"The version of Go available in the environment is lower than the version specified in the `go.mod` file. [Install a newer version](https://github.com/actions/setup-go#basic)",
109+
severityError, false,
110+
true, true, true,
111+
"", 0, 0, 0, 0,
112+
)
113+
}

0 commit comments

Comments
 (0)