diff --git a/extractor/cli/go-autobuilder/go-autobuilder.go b/extractor/cli/go-autobuilder/go-autobuilder.go index daaf733f..07b8a6cb 100644 --- a/extractor/cli/go-autobuilder/go-autobuilder.go +++ b/extractor/cli/go-autobuilder/go-autobuilder.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "golang.org/x/mod/semver" "io/ioutil" "log" "net/url" @@ -13,6 +12,8 @@ import ( "runtime" "strings" + "golang.org/x/mod/semver" + "github.com/github/codeql-go/extractor/autobuilder" "github.com/github/codeql-go/extractor/util" ) @@ -289,6 +290,44 @@ func main() { } } + // Go 1.16 and later won't automatically attempt to update go.mod / go.sum during package loading, so try to update them here: + if depMode == GoGetWithModules && semver.Compare(getEnvGoSemVer(), "1.16") >= 0 { + // stat go.mod and go.sum + beforeGoModFileInfo, beforeGoModErr := os.Stat("go.mod") + if beforeGoModErr != nil { + log.Println("Failed to stat go.mod before running `go mod tidy -e`") + } + + beforeGoSumFileInfo, beforeGoSumErr := os.Stat("go.sum") + + // run `go mod tidy -e` + res := util.RunCmd(exec.Command("go", "mod", "tidy", "-e")) + + if !res { + log.Println("Failed to run `go mod tidy -e`") + } else { + if beforeGoModFileInfo != nil { + afterGoModFileInfo, afterGoModErr := os.Stat("go.mod") + if afterGoModErr != nil { + log.Println("Failed to stat go.mod after running `go mod tidy -e`") + } else if afterGoModFileInfo.ModTime().After(beforeGoModFileInfo.ModTime()) { + // if go.mod has been changed then notify the user + log.Println("We have run `go mod tidy -e` and it altered go.mod. You may wish to check these changes into version control. ") + } + } + + afterGoSumFileInfo, afterGoSumErr := os.Stat("go.sum") + if afterGoSumErr != nil { + log.Println("Failed to stat go.sum after running `go mod tidy -e`") + } else { + if beforeGoSumErr != nil || afterGoSumFileInfo.ModTime().After(beforeGoSumFileInfo.ModTime()) { + // if go.sum has been changed then notify the user + log.Println("We have run `go mod tidy -e` and it altered go.sum. You may wish to check these changes into version control. ") + } + } + } + } + // if `LGTM_INDEX_NEED_GOPATH` is set, it overrides the value for `needGopath` inferred above if needGopathOverride := os.Getenv("LGTM_INDEX_NEED_GOPATH"); needGopathOverride != "" { inLGTM = true