@@ -207,6 +207,12 @@ func (m ModMode) argsForGoVersion(version string) []string {
207207 return nil
208208}
209209
210+ type BuildInfo struct {
211+ DepMode DependencyInstallerMode
212+ ModMode ModMode
213+ BaseDir string
214+ }
215+
210216// addVersionToMod add a go version directive, e.g. `go 1.14` to a `go.mod` file.
211217func addVersionToMod (version string ) bool {
212218 cmd := exec .Command ("go" , "mod" , "edit" , "-go=" + version )
@@ -362,10 +368,10 @@ func getDepMode(emitDiagnostics bool) (DependencyInstallerMode, string) {
362368}
363369
364370// Tries to open `go.mod` and read a go directive, returning the version and whether it was found.
365- func tryReadGoDirective (depMode DependencyInstallerMode , baseDir string ) (string , bool ) {
366- if depMode == GoGetWithModules {
371+ func tryReadGoDirective (buildInfo BuildInfo ) (string , bool ) {
372+ if buildInfo . DepMode == GoGetWithModules {
367373 versionRe := regexp .MustCompile (`(?m)^go[ \t\r]+([0-9]+\.[0-9]+)$` )
368- goMod , err := os .ReadFile (filepath .Join (baseDir , "go.mod" ))
374+ goMod , err := os .ReadFile (filepath .Join (buildInfo . BaseDir , "go.mod" ))
369375 if err != nil {
370376 log .Println ("Failed to read go.mod to check for missing Go version" )
371377 } else {
@@ -395,16 +401,16 @@ func getModMode(depMode DependencyInstallerMode, baseDir string) ModMode {
395401}
396402
397403// fixGoVendorIssues fixes issues with go vendor for go version >= 1.14
398- func fixGoVendorIssues (modMode ModMode , depMode DependencyInstallerMode , goModVersionFound bool ) ModMode {
399- if modMode == ModVendor {
404+ func fixGoVendorIssues (buildInfo BuildInfo , goModVersionFound bool ) ModMode {
405+ if buildInfo . ModMode == ModVendor {
400406 // fix go vendor issues with go versions >= 1.14 when no go version is specified in the go.mod
401407 // if this is the case, and dependencies were vendored with an old go version (and therefore
402408 // do not contain a '## explicit' annotation, the go command will fail and refuse to do any
403409 // work
404410 //
405411 // we work around this by adding an explicit go version of 1.13, which is the last version
406412 // where this is not an issue
407- if depMode == GoGetWithModules {
413+ if buildInfo . DepMode == GoGetWithModules {
408414 if ! goModVersionFound {
409415 // if the go.mod does not contain a version line
410416 modulesTxt , err := os .ReadFile ("vendor/modules.txt" )
@@ -421,13 +427,13 @@ func fixGoVendorIssues(modMode ModMode, depMode DependencyInstallerMode, goModVe
421427 }
422428 }
423429 }
424- return modMode
430+ return buildInfo . ModMode
425431}
426432
427433// Determines whether the project needs a GOPATH set up
428- func getNeedGopath (depMode DependencyInstallerMode , importpath string ) bool {
434+ func getNeedGopath (buildInfo BuildInfo , importpath string ) bool {
429435 needGopath := true
430- if depMode == GoGetWithModules {
436+ if buildInfo . DepMode == GoGetWithModules {
431437 needGopath = false
432438 }
433439 // if `LGTM_INDEX_NEED_GOPATH` is set, it overrides the value for `needGopath` inferred above
@@ -448,22 +454,22 @@ func getNeedGopath(depMode DependencyInstallerMode, importpath string) bool {
448454}
449455
450456// Try to update `go.mod` and `go.sum` if the go version is >= 1.16.
451- func tryUpdateGoModAndGoSum (modMode ModMode , depMode DependencyInstallerMode , baseDir string ) {
457+ func tryUpdateGoModAndGoSum (buildInfo BuildInfo ) {
452458 // Go 1.16 and later won't automatically attempt to update go.mod / go.sum during package loading, so try to update them here:
453- if modMode != ModVendor && depMode == GoGetWithModules && semver .Compare (getEnvGoSemVer (), "v1.16" ) >= 0 {
459+ if buildInfo . ModMode != ModVendor && buildInfo . DepMode == GoGetWithModules && semver .Compare (getEnvGoSemVer (), "v1.16" ) >= 0 {
454460 // stat go.mod and go.sum
455- goModPath := filepath .Join (baseDir , "go.mod" )
461+ goModPath := filepath .Join (buildInfo . BaseDir , "go.mod" )
456462 beforeGoModFileInfo , beforeGoModErr := os .Stat (goModPath )
457463 if beforeGoModErr != nil {
458464 log .Println ("Failed to stat go.mod before running `go mod tidy -e`" )
459465 }
460466
461- goSumPath := filepath .Join (baseDir , "go.sum" )
467+ goSumPath := filepath .Join (buildInfo . BaseDir , "go.sum" )
462468 beforeGoSumFileInfo , beforeGoSumErr := os .Stat (goSumPath )
463469
464470 // run `go mod tidy -e`
465471 cmd := exec .Command ("go" , "mod" , "tidy" , "-e" )
466- cmd .Dir = baseDir
472+ cmd .Dir = buildInfo . BaseDir
467473 res := util .RunCmd (cmd )
468474
469475 if ! res {
@@ -668,10 +674,10 @@ func buildWithCustomCommands(inst string) {
668674}
669675
670676// Install dependencies using the given dependency installer mode.
671- func installDependencies (depMode DependencyInstallerMode , baseDir string ) {
677+ func installDependencies (buildInfo BuildInfo ) {
672678 // automatically determine command to install dependencies
673679 var install * exec.Cmd
674- if depMode == Dep {
680+ if buildInfo . DepMode == Dep {
675681 // set up the dep cache if SEMMLE_CACHE is set
676682 cacheDir := os .Getenv ("SEMMLE_CACHE" )
677683 if cacheDir != "" {
@@ -701,41 +707,41 @@ func installDependencies(depMode DependencyInstallerMode, baseDir string) {
701707 install = exec .Command ("dep" , "ensure" , "-v" )
702708 }
703709 log .Println ("Installing dependencies using `dep ensure`." )
704- } else if depMode == Glide {
710+ } else if buildInfo . DepMode == Glide {
705711 install = exec .Command ("glide" , "install" )
706712 log .Println ("Installing dependencies using `glide install`" )
707713 } else {
708714 // explicitly set go module support
709- if depMode == GoGetWithModules {
715+ if buildInfo . DepMode == GoGetWithModules {
710716 os .Setenv ("GO111MODULE" , "on" )
711- } else if depMode == GoGetNoModules {
717+ } else if buildInfo . DepMode == GoGetNoModules {
712718 os .Setenv ("GO111MODULE" , "off" )
713719 }
714720
715721 // get dependencies
716722 install = exec .Command ("go" , "get" , "-v" , "./..." )
717- install .Dir = baseDir
718- log .Printf ("Installing dependencies using `go get -v ./...` in `%s`.\n " , baseDir )
723+ install .Dir = buildInfo . BaseDir
724+ log .Printf ("Installing dependencies using `go get -v ./...` in `%s`.\n " , buildInfo . BaseDir )
719725 }
720726 util .RunCmd (install )
721727}
722728
723729// Run the extractor.
724- func extract (depMode DependencyInstallerMode , modMode ModMode , baseDir string ) {
730+ func extract (buildInfo BuildInfo ) {
725731 extractor , err := util .GetExtractorPath ()
726732 if err != nil {
727733 log .Fatalf ("Could not determine path of extractor: %v.\n " , err )
728734 }
729735
730736 extractorArgs := []string {}
731- if depMode == GoGetWithModules {
732- extractorArgs = append (extractorArgs , modMode .argsForGoVersion (getEnvGoSemVer ())... )
737+ if buildInfo . DepMode == GoGetWithModules {
738+ extractorArgs = append (extractorArgs , buildInfo . ModMode .argsForGoVersion (getEnvGoSemVer ())... )
733739 }
734740 extractorArgs = append (extractorArgs , "./..." )
735741
736- log .Printf ("Running extractor command '%s %v' from directory '%s'.\n " , extractor , extractorArgs , baseDir )
742+ log .Printf ("Running extractor command '%s %v' from directory '%s'.\n " , extractor , extractorArgs , buildInfo . BaseDir )
737743 cmd := exec .Command (extractor , extractorArgs ... )
738- cmd .Dir = baseDir
744+ cmd .Dir = buildInfo . BaseDir
739745 cmd .Stdout = os .Stdout
740746 cmd .Stderr = os .Stderr
741747 err = cmd .Run ()
@@ -744,6 +750,12 @@ func extract(depMode DependencyInstallerMode, modMode ModMode, baseDir string) {
744750 }
745751}
746752
753+ func getBuildInfo (emitDiagnostics bool ) BuildInfo {
754+ depMode , baseDir := getDepMode (true )
755+ modMode := getModMode (depMode , baseDir )
756+ return BuildInfo {depMode , modMode , baseDir }
757+ }
758+
747759// Build the project and run the extractor.
748760func installDependenciesAndBuild () {
749761 log .Printf ("Autobuilder was built with %s, environment has %s\n " , runtime .Version (), getEnvGoVersion ())
@@ -755,24 +767,23 @@ func installDependenciesAndBuild() {
755767
756768 // determine how to install dependencies and whether a GOPATH needs to be set up before
757769 // extraction
758- depMode , baseDir := getDepMode (true )
770+ buildInfo := getBuildInfo (true )
759771 if _ , present := os .LookupEnv ("GO111MODULE" ); ! present {
760772 os .Setenv ("GO111MODULE" , "auto" )
761773 }
762774
763- goModVersion , goModVersionFound := tryReadGoDirective (depMode , baseDir )
775+ goModVersion , goModVersionFound := tryReadGoDirective (buildInfo )
764776
765777 if goModVersionFound && semver .Compare ("v" + goModVersion , getEnvGoSemVer ()) >= 0 {
766778 diagnostics .EmitNewerGoVersionNeeded ()
767779 }
768780
769- modMode := getModMode (depMode , baseDir )
770- modMode = fixGoVendorIssues (modMode , depMode , goModVersionFound )
781+ buildInfo .ModMode = fixGoVendorIssues (buildInfo , goModVersionFound )
771782
772- tryUpdateGoModAndGoSum (modMode , depMode , baseDir )
783+ tryUpdateGoModAndGoSum (buildInfo )
773784
774785 importpath := getImportPath ()
775- needGopath := getNeedGopath (depMode , importpath )
786+ needGopath := getNeedGopath (buildInfo , importpath )
776787
777788 inLGTM := os .Getenv ("LGTM_SRC" ) != "" || os .Getenv ("LGTM_INDEX_NEED_GOPATH" ) != ""
778789
@@ -793,30 +804,30 @@ func installDependenciesAndBuild() {
793804 inst := util .Getenv ("CODEQL_EXTRACTOR_GO_BUILD_COMMAND" , "LGTM_INDEX_BUILD_COMMAND" )
794805 shouldInstallDependencies := false
795806 if inst == "" {
796- shouldInstallDependencies = buildWithoutCustomCommands (modMode )
807+ shouldInstallDependencies = buildWithoutCustomCommands (buildInfo . ModMode )
797808 } else {
798809 buildWithCustomCommands (inst )
799810 }
800811
801- if modMode == ModVendor {
812+ if buildInfo . ModMode == ModVendor {
802813 // test if running `go` with -mod=vendor works, and if it doesn't, try to fallback to -mod=mod
803814 // or not set if the go version < 1.14. Note we check this post-build in case the build brings
804815 // the vendor directory up to date.
805816 if ! checkVendor () {
806- modMode = ModMod
817+ buildInfo . ModMode = ModMod
807818 log .Println ("The vendor directory is not consistent with the go.mod; not using vendored dependencies." )
808819 }
809820 }
810821
811822 if shouldInstallDependencies {
812- if modMode == ModVendor {
823+ if buildInfo . ModMode == ModVendor {
813824 log .Printf ("Skipping dependency installation because a Go vendor directory was found." )
814825 } else {
815- installDependencies (depMode , baseDir )
826+ installDependencies (buildInfo )
816827 }
817828 }
818829
819- extract (depMode , modMode , baseDir )
830+ extract (buildInfo )
820831}
821832
822833const minGoVersion = "1.11"
@@ -1081,8 +1092,8 @@ func isGoInstalled() bool {
10811092// Get the version of Go to install and output it to stdout as json.
10821093func identifyEnvironment () {
10831094 var v versionInfo
1084- depMode , baseDir := getDepMode (false )
1085- v .goModVersion , v .goModVersionFound = tryReadGoDirective (depMode , baseDir )
1095+ buildInfo := getBuildInfo (false )
1096+ v .goModVersion , v .goModVersionFound = tryReadGoDirective (buildInfo )
10861097
10871098 v .goEnvVersionFound = isGoInstalled ()
10881099 if v .goEnvVersionFound {
0 commit comments