mirror of
https://github.com/github/codeql.git
synced 2026-03-05 23:26:51 +01:00
Merge pull request #14391 from github/mbg/go/update-newer-go-version-needed
Go: Fix version detection and test for `newer-go-version-needed`
This commit is contained in:
@@ -61,11 +61,19 @@ var goVersion = ""
|
||||
// Returns the current Go version as returned by 'go version', e.g. go1.14.4
|
||||
func getEnvGoVersion() string {
|
||||
if goVersion == "" {
|
||||
gover, err := exec.Command("go", "version").CombinedOutput()
|
||||
// Since Go 1.21, running 'go version' in a directory with a 'go.mod' file will attempt to
|
||||
// download the version of Go specified in there. That may either fail or result in us just
|
||||
// being told what's already in 'go.mod'. Setting 'GOTOOLCHAIN' to 'local' will force it
|
||||
// to use the local Go toolchain instead.
|
||||
cmd := exec.Command("go", "version")
|
||||
cmd.Env = append(os.Environ(), "GOTOOLCHAIN=local")
|
||||
out, err := cmd.CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to run the go command, is it installed?\nError: %s", err.Error())
|
||||
}
|
||||
goVersion = parseGoVersion(string(gover))
|
||||
|
||||
goVersion = parseGoVersion(string(out))
|
||||
}
|
||||
return goVersion
|
||||
}
|
||||
@@ -367,8 +375,15 @@ func getDepMode(emitDiagnostics bool) (DependencyInstallerMode, string) {
|
||||
return GoGetNoModules, "."
|
||||
}
|
||||
|
||||
type GoVersionInfo struct {
|
||||
// The version string, if any
|
||||
Version string
|
||||
// A value indicating whether a version string was found
|
||||
Found bool
|
||||
}
|
||||
|
||||
// Tries to open `go.mod` and read a go directive, returning the version and whether it was found.
|
||||
func tryReadGoDirective(buildInfo BuildInfo) (string, bool) {
|
||||
func tryReadGoDirective(buildInfo BuildInfo) GoVersionInfo {
|
||||
if buildInfo.DepMode == GoGetWithModules {
|
||||
versionRe := regexp.MustCompile(`(?m)^go[ \t\r]+([0-9]+\.[0-9]+(\.[0-9]+)?)$`)
|
||||
goMod, err := os.ReadFile(filepath.Join(buildInfo.BaseDir, "go.mod"))
|
||||
@@ -378,12 +393,12 @@ func tryReadGoDirective(buildInfo BuildInfo) (string, bool) {
|
||||
matches := versionRe.FindSubmatch(goMod)
|
||||
if matches != nil {
|
||||
if len(matches) > 1 {
|
||||
return string(matches[1]), true
|
||||
return GoVersionInfo{string(matches[1]), true}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
return GoVersionInfo{"", false}
|
||||
}
|
||||
|
||||
// Returns the appropriate ModMode for the current project
|
||||
@@ -771,13 +786,15 @@ func installDependenciesAndBuild() {
|
||||
os.Setenv("GO111MODULE", "auto")
|
||||
}
|
||||
|
||||
goModVersion, goModVersionFound := tryReadGoDirective(buildInfo)
|
||||
goVersionInfo := tryReadGoDirective(buildInfo)
|
||||
|
||||
if goModVersionFound && semver.Compare("v"+goModVersion, getEnvGoSemVer()) > 0 {
|
||||
// This diagnostic is not required if the system Go version is 1.21 or greater, since the
|
||||
// Go tooling should install required Go versions as needed.
|
||||
if semver.Compare(getEnvGoSemVer(), "v1.21.0") < 0 && goVersionInfo.Found && semver.Compare("v"+goVersionInfo.Version, getEnvGoSemVer()) > 0 {
|
||||
diagnostics.EmitNewerGoVersionNeeded()
|
||||
}
|
||||
|
||||
fixGoVendorIssues(&buildInfo, goModVersionFound)
|
||||
fixGoVendorIssues(&buildInfo, goVersionInfo.Found)
|
||||
|
||||
tryUpdateGoModAndGoSum(buildInfo)
|
||||
|
||||
@@ -1092,7 +1109,8 @@ func isGoInstalled() bool {
|
||||
func identifyEnvironment() {
|
||||
var v versionInfo
|
||||
buildInfo := getBuildInfo(false)
|
||||
v.goModVersion, v.goModVersionFound = tryReadGoDirective(buildInfo)
|
||||
goVersionInfo := tryReadGoDirective(buildInfo)
|
||||
v.goModVersion, v.goModVersionFound = goVersionInfo.Version, goVersionInfo.Found
|
||||
|
||||
v.goEnvVersionFound = isGoInstalled()
|
||||
if v.goEnvVersionFound {
|
||||
|
||||
@@ -12,17 +12,3 @@
|
||||
"telemetry": true
|
||||
}
|
||||
}
|
||||
{
|
||||
"markdownMessage": "The detected version of Go is lower than the version specified in `go.mod`. [Install a newer version](https://github.com/actions/setup-go#basic).",
|
||||
"severity": "error",
|
||||
"source": {
|
||||
"extractorName": "go",
|
||||
"id": "go/autobuilder/newer-go-version-needed",
|
||||
"name": "Newer Go version needed"
|
||||
},
|
||||
"visibility": {
|
||||
"cliSummaryTable": true,
|
||||
"statusPage": true,
|
||||
"telemetry": true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ from create_database_utils import *
|
||||
from diagnostics_test_utils import *
|
||||
|
||||
os.environ['LGTM_INDEX_IMPORT_PATH'] = "test"
|
||||
run_codeql_database_create([], lang="go", source="work", db=None)
|
||||
run_codeql_database_create([], lang="go", source="work", db=None, runFunction=runUnsuccessfully)
|
||||
|
||||
check_diagnostics()
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
go 999.0
|
||||
go 1.999.0
|
||||
|
||||
module test
|
||||
|
||||
Reference in New Issue
Block a user