@@ -207,6 +207,12 @@ func (m ModMode) argsForGoVersion(version string) []string {
207
207
return nil
208
208
}
209
209
210
+ type BuildInfo struct {
211
+ DepMode DependencyInstallerMode
212
+ ModMode ModMode
213
+ BaseDir string
214
+ }
215
+
210
216
// addVersionToMod add a go version directive, e.g. `go 1.14` to a `go.mod` file.
211
217
func addVersionToMod (version string ) bool {
212
218
cmd := exec .Command ("go" , "mod" , "edit" , "-go=" + version )
@@ -362,10 +368,10 @@ func getDepMode(emitDiagnostics bool) (DependencyInstallerMode, string) {
362
368
}
363
369
364
370
// 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 {
367
373
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" ))
369
375
if err != nil {
370
376
log .Println ("Failed to read go.mod to check for missing Go version" )
371
377
} else {
@@ -395,16 +401,16 @@ func getModMode(depMode DependencyInstallerMode, baseDir string) ModMode {
395
401
}
396
402
397
403
// 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 {
400
406
// fix go vendor issues with go versions >= 1.14 when no go version is specified in the go.mod
401
407
// if this is the case, and dependencies were vendored with an old go version (and therefore
402
408
// do not contain a '## explicit' annotation, the go command will fail and refuse to do any
403
409
// work
404
410
//
405
411
// we work around this by adding an explicit go version of 1.13, which is the last version
406
412
// where this is not an issue
407
- if depMode == GoGetWithModules {
413
+ if buildInfo . DepMode == GoGetWithModules {
408
414
if ! goModVersionFound {
409
415
// if the go.mod does not contain a version line
410
416
modulesTxt , err := os .ReadFile ("vendor/modules.txt" )
@@ -421,13 +427,13 @@ func fixGoVendorIssues(modMode ModMode, depMode DependencyInstallerMode, goModVe
421
427
}
422
428
}
423
429
}
424
- return modMode
430
+ return buildInfo . ModMode
425
431
}
426
432
427
433
// 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 {
429
435
needGopath := true
430
- if depMode == GoGetWithModules {
436
+ if buildInfo . DepMode == GoGetWithModules {
431
437
needGopath = false
432
438
}
433
439
// 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 {
448
454
}
449
455
450
456
// 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 ) {
452
458
// 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 {
454
460
// stat go.mod and go.sum
455
- goModPath := filepath .Join (baseDir , "go.mod" )
461
+ goModPath := filepath .Join (buildInfo . BaseDir , "go.mod" )
456
462
beforeGoModFileInfo , beforeGoModErr := os .Stat (goModPath )
457
463
if beforeGoModErr != nil {
458
464
log .Println ("Failed to stat go.mod before running `go mod tidy -e`" )
459
465
}
460
466
461
- goSumPath := filepath .Join (baseDir , "go.sum" )
467
+ goSumPath := filepath .Join (buildInfo . BaseDir , "go.sum" )
462
468
beforeGoSumFileInfo , beforeGoSumErr := os .Stat (goSumPath )
463
469
464
470
// run `go mod tidy -e`
465
471
cmd := exec .Command ("go" , "mod" , "tidy" , "-e" )
466
- cmd .Dir = baseDir
472
+ cmd .Dir = buildInfo . BaseDir
467
473
res := util .RunCmd (cmd )
468
474
469
475
if ! res {
@@ -668,10 +674,10 @@ func buildWithCustomCommands(inst string) {
668
674
}
669
675
670
676
// Install dependencies using the given dependency installer mode.
671
- func installDependencies (depMode DependencyInstallerMode , baseDir string ) {
677
+ func installDependencies (buildInfo BuildInfo ) {
672
678
// automatically determine command to install dependencies
673
679
var install * exec.Cmd
674
- if depMode == Dep {
680
+ if buildInfo . DepMode == Dep {
675
681
// set up the dep cache if SEMMLE_CACHE is set
676
682
cacheDir := os .Getenv ("SEMMLE_CACHE" )
677
683
if cacheDir != "" {
@@ -701,41 +707,41 @@ func installDependencies(depMode DependencyInstallerMode, baseDir string) {
701
707
install = exec .Command ("dep" , "ensure" , "-v" )
702
708
}
703
709
log .Println ("Installing dependencies using `dep ensure`." )
704
- } else if depMode == Glide {
710
+ } else if buildInfo . DepMode == Glide {
705
711
install = exec .Command ("glide" , "install" )
706
712
log .Println ("Installing dependencies using `glide install`" )
707
713
} else {
708
714
// explicitly set go module support
709
- if depMode == GoGetWithModules {
715
+ if buildInfo . DepMode == GoGetWithModules {
710
716
os .Setenv ("GO111MODULE" , "on" )
711
- } else if depMode == GoGetNoModules {
717
+ } else if buildInfo . DepMode == GoGetNoModules {
712
718
os .Setenv ("GO111MODULE" , "off" )
713
719
}
714
720
715
721
// get dependencies
716
722
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 )
719
725
}
720
726
util .RunCmd (install )
721
727
}
722
728
723
729
// Run the extractor.
724
- func extract (depMode DependencyInstallerMode , modMode ModMode , baseDir string ) {
730
+ func extract (buildInfo BuildInfo ) {
725
731
extractor , err := util .GetExtractorPath ()
726
732
if err != nil {
727
733
log .Fatalf ("Could not determine path of extractor: %v.\n " , err )
728
734
}
729
735
730
736
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 ())... )
733
739
}
734
740
extractorArgs = append (extractorArgs , "./..." )
735
741
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 )
737
743
cmd := exec .Command (extractor , extractorArgs ... )
738
- cmd .Dir = baseDir
744
+ cmd .Dir = buildInfo . BaseDir
739
745
cmd .Stdout = os .Stdout
740
746
cmd .Stderr = os .Stderr
741
747
err = cmd .Run ()
@@ -744,6 +750,12 @@ func extract(depMode DependencyInstallerMode, modMode ModMode, baseDir string) {
744
750
}
745
751
}
746
752
753
+ func getBuildInfo (emitDiagnostics bool ) BuildInfo {
754
+ depMode , baseDir := getDepMode (true )
755
+ modMode := getModMode (depMode , baseDir )
756
+ return BuildInfo {depMode , modMode , baseDir }
757
+ }
758
+
747
759
// Build the project and run the extractor.
748
760
func installDependenciesAndBuild () {
749
761
log .Printf ("Autobuilder was built with %s, environment has %s\n " , runtime .Version (), getEnvGoVersion ())
@@ -755,24 +767,23 @@ func installDependenciesAndBuild() {
755
767
756
768
// determine how to install dependencies and whether a GOPATH needs to be set up before
757
769
// extraction
758
- depMode , baseDir := getDepMode (true )
770
+ buildInfo := getBuildInfo (true )
759
771
if _ , present := os .LookupEnv ("GO111MODULE" ); ! present {
760
772
os .Setenv ("GO111MODULE" , "auto" )
761
773
}
762
774
763
- goModVersion , goModVersionFound := tryReadGoDirective (depMode , baseDir )
775
+ goModVersion , goModVersionFound := tryReadGoDirective (buildInfo )
764
776
765
777
if goModVersionFound && semver .Compare ("v" + goModVersion , getEnvGoSemVer ()) >= 0 {
766
778
diagnostics .EmitNewerGoVersionNeeded ()
767
779
}
768
780
769
- modMode := getModMode (depMode , baseDir )
770
- modMode = fixGoVendorIssues (modMode , depMode , goModVersionFound )
781
+ buildInfo .ModMode = fixGoVendorIssues (buildInfo , goModVersionFound )
771
782
772
- tryUpdateGoModAndGoSum (modMode , depMode , baseDir )
783
+ tryUpdateGoModAndGoSum (buildInfo )
773
784
774
785
importpath := getImportPath ()
775
- needGopath := getNeedGopath (depMode , importpath )
786
+ needGopath := getNeedGopath (buildInfo , importpath )
776
787
777
788
inLGTM := os .Getenv ("LGTM_SRC" ) != "" || os .Getenv ("LGTM_INDEX_NEED_GOPATH" ) != ""
778
789
@@ -793,30 +804,30 @@ func installDependenciesAndBuild() {
793
804
inst := util .Getenv ("CODEQL_EXTRACTOR_GO_BUILD_COMMAND" , "LGTM_INDEX_BUILD_COMMAND" )
794
805
shouldInstallDependencies := false
795
806
if inst == "" {
796
- shouldInstallDependencies = buildWithoutCustomCommands (modMode )
807
+ shouldInstallDependencies = buildWithoutCustomCommands (buildInfo . ModMode )
797
808
} else {
798
809
buildWithCustomCommands (inst )
799
810
}
800
811
801
- if modMode == ModVendor {
812
+ if buildInfo . ModMode == ModVendor {
802
813
// test if running `go` with -mod=vendor works, and if it doesn't, try to fallback to -mod=mod
803
814
// or not set if the go version < 1.14. Note we check this post-build in case the build brings
804
815
// the vendor directory up to date.
805
816
if ! checkVendor () {
806
- modMode = ModMod
817
+ buildInfo . ModMode = ModMod
807
818
log .Println ("The vendor directory is not consistent with the go.mod; not using vendored dependencies." )
808
819
}
809
820
}
810
821
811
822
if shouldInstallDependencies {
812
- if modMode == ModVendor {
823
+ if buildInfo . ModMode == ModVendor {
813
824
log .Printf ("Skipping dependency installation because a Go vendor directory was found." )
814
825
} else {
815
- installDependencies (depMode , baseDir )
826
+ installDependencies (buildInfo )
816
827
}
817
828
}
818
829
819
- extract (depMode , modMode , baseDir )
830
+ extract (buildInfo )
820
831
}
821
832
822
833
const minGoVersion = "1.11"
@@ -1081,8 +1092,8 @@ func isGoInstalled() bool {
1081
1092
// Get the version of Go to install and output it to stdout as json.
1082
1093
func identifyEnvironment () {
1083
1094
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 )
1086
1097
1087
1098
v .goEnvVersionFound = isGoInstalled ()
1088
1099
if v .goEnvVersionFound {
0 commit comments