@@ -100,6 +100,7 @@ func tryBuild(buildFile, cmd string, args ...string) bool {
100
100
return false
101
101
}
102
102
103
+ // Returns the import path of the package being built, or "" if it cannot be determined.
103
104
func getImportPath () (importpath string ) {
104
105
importpath = os .Getenv ("LGTM_INDEX_IMPORT_PATH" )
105
106
if importpath == "" {
@@ -124,6 +125,8 @@ func getImportPath() (importpath string) {
124
125
return
125
126
}
126
127
128
+ // Returns the import path of the package being built from `repourl`, or "" if it cannot be
129
+ // determined.
127
130
func getImportPathFromRepoURL (repourl string ) string {
128
131
// check for scp-like URL as in "[email protected] :github/codeql-go.git"
129
132
shorturl := regexp .MustCompile ("^([^@]+@)?([^:]+):([^/].*?)(\\ .git)?$" )
@@ -190,6 +193,8 @@ const (
190
193
ModVendor
191
194
)
192
195
196
+ // argsForGoVersion returns the arguments to pass to the Go compiler for the given `ModMode` and
197
+ // Go version
193
198
func (m ModMode ) argsForGoVersion (version string ) []string {
194
199
switch m {
195
200
case ModUnset :
@@ -229,6 +234,7 @@ func checkVendor() bool {
229
234
return true
230
235
}
231
236
237
+ // Returns the directory containing the source code to be analyzed.
232
238
func getSourceDir () string {
233
239
srcdir := os .Getenv ("LGTM_SRC" )
234
240
if srcdir != "" {
@@ -244,6 +250,7 @@ func getSourceDir() string {
244
250
return srcdir
245
251
}
246
252
253
+ // Returns the appropriate DependencyInstallerMode for the current project
247
254
func getDepMode () DependencyInstallerMode {
248
255
if util .FileExists ("go.mod" ) {
249
256
log .Println ("Found go.mod, enabling go modules" )
@@ -260,6 +267,7 @@ func getDepMode() DependencyInstallerMode {
260
267
return GoGetNoModules
261
268
}
262
269
270
+ // Tries to open `go.mod` and read a go directive, returning the version and whether it was found.
263
271
func tryReadGoDirective (depMode DependencyInstallerMode ) (string , bool ) {
264
272
version := ""
265
273
found := false
@@ -285,6 +293,7 @@ func tryReadGoDirective(depMode DependencyInstallerMode) (string, bool) {
285
293
return version , found
286
294
}
287
295
296
+ // Returns the appropriate ModMode for the current project
288
297
func getModMode (depMode DependencyInstallerMode ) ModMode {
289
298
if depMode == GoGetWithModules {
290
299
// if a vendor/modules.txt file exists, we assume that there are vendored Go dependencies, and
@@ -298,6 +307,7 @@ func getModMode(depMode DependencyInstallerMode) ModMode {
298
307
return ModUnset
299
308
}
300
309
310
+ // fixGoVendorIssues fixes issues with go vendor for go version >= 1.14
301
311
func fixGoVendorIssues (modMode ModMode , depMode DependencyInstallerMode , goDirectiveFound bool ) ModMode {
302
312
if modMode == ModVendor {
303
313
// fix go vendor issues with go versions >= 1.14 when no go version is specified in the go.mod
@@ -327,6 +337,7 @@ func fixGoVendorIssues(modMode ModMode, depMode DependencyInstallerMode, goDirec
327
337
return modMode
328
338
}
329
339
340
+ // Determines whether the project needs a GOPATH set up
330
341
func getNeedGopath (depMode DependencyInstallerMode , importpath string ) bool {
331
342
needGopath := true
332
343
if depMode == GoGetWithModules {
@@ -349,6 +360,7 @@ func getNeedGopath(depMode DependencyInstallerMode, importpath string) bool {
349
360
return needGopath
350
361
}
351
362
363
+ // Try to update `go.mod` and `go.sum` if the go version is >= 1.16.
352
364
func tryUpdateGoModAndGoSum (modMode ModMode , depMode DependencyInstallerMode ) {
353
365
// Go 1.16 and later won't automatically attempt to update go.mod / go.sum during package loading, so try to update them here:
354
366
if modMode != ModVendor && depMode == GoGetWithModules && semver .Compare (getEnvGoSemVer (), "v1.16" ) >= 0 {
@@ -394,6 +406,7 @@ type moveGopathInfo struct {
394
406
files []string
395
407
}
396
408
409
+ // Moves all files in `srcdir` to a temporary directory with the correct layout to be added to the GOPATH
397
410
func moveToTemporaryGopath (srcdir string , importpath string ) moveGopathInfo {
398
411
// a temporary directory where everything is moved while the correct
399
412
// directory structure is created.
@@ -455,6 +468,8 @@ func moveToTemporaryGopath(srcdir string, importpath string) moveGopathInfo {
455
468
}
456
469
}
457
470
471
+ // Creates a path transformer file in the new directory to ensure paths in the source archive and the snapshot
472
+ // match the original source location, not the location we moved it to.
458
473
func createPathTransformerFile (newdir string ) * os.File {
459
474
err := os .Chdir (newdir )
460
475
if err != nil {
@@ -470,6 +485,7 @@ func createPathTransformerFile(newdir string) *os.File {
470
485
return pt
471
486
}
472
487
488
+ // Writes the path transformer file
473
489
func writePathTransformerFile (pt * os.File , realSrc , root , newdir string ) {
474
490
_ , err := pt .WriteString ("#" + realSrc + "\n " + newdir + "//\n " )
475
491
if err != nil {
@@ -485,6 +501,7 @@ func writePathTransformerFile(pt *os.File, realSrc, root, newdir string) {
485
501
}
486
502
}
487
503
504
+ // Adds `root` to GOPATH.
488
505
func setGopath (root string ) {
489
506
// set/extend GOPATH
490
507
oldGopath := os .Getenv ("GOPATH" )
@@ -504,6 +521,8 @@ func setGopath(root string) {
504
521
log .Printf ("GOPATH set to %s.\n " , newGopath )
505
522
}
506
523
524
+ // Try to build the project without custom commands. If that fails, return a boolean indicating
525
+ // that we should install dependencies ourselves.
507
526
func buildWithoutCustomCommands (modMode ModMode ) bool {
508
527
shouldInstallDependencies := false
509
528
// try to build the project
@@ -523,6 +542,7 @@ func buildWithoutCustomCommands(modMode ModMode) bool {
523
542
return shouldInstallDependencies
524
543
}
525
544
545
+ // Build the project with custom commands.
526
546
func buildWithCustomCommands (inst string ) {
527
547
// write custom build commands into a script, then run it
528
548
var (
@@ -556,6 +576,7 @@ func buildWithCustomCommands(inst string) {
556
576
util .RunCmd (exec .Command (script .Name ()))
557
577
}
558
578
579
+ // Install dependencies using the given dependency installer mode.
559
580
func installDependencies (depMode DependencyInstallerMode ) {
560
581
// automatically determine command to install dependencies
561
582
var install * exec.Cmd
@@ -607,6 +628,7 @@ func installDependencies(depMode DependencyInstallerMode) {
607
628
util .RunCmd (install )
608
629
}
609
630
631
+ // Run the extractor.
610
632
func extract (depMode DependencyInstallerMode , modMode ModMode ) {
611
633
extractor , err := util .GetExtractorPath ()
612
634
if err != nil {
@@ -634,6 +656,7 @@ func extract(depMode DependencyInstallerMode, modMode ModMode) {
634
656
}
635
657
}
636
658
659
+ // Build the project and run the extractor.
637
660
func installDependenciesAndBuild () {
638
661
log .Printf ("Autobuilder was built with %s, environment has %s\n " , runtime .Version (), getEnvGoVersion ())
639
662
@@ -707,11 +730,17 @@ func installDependenciesAndBuild() {
707
730
const minGoVersion = "1.11"
708
731
const maxGoVersion = "1.20"
709
732
733
+ // Check if `version` is lower than `minGoVersion` or higher than `maxGoVersion`. Note that for
734
+ // this comparison we ignore the patch part of the version, so 1.20.1 and 1.20 are considered
735
+ // equal.
710
736
func outsideSupportedRange (version string ) bool {
711
737
short := semver .MajorMinor ("v" + version )
712
738
return semver .Compare (short , "v" + minGoVersion ) < 0 || semver .Compare (short , "v" + maxGoVersion ) > 0
713
739
}
714
740
741
+ // Check if `v.goModVersion` or `v.goEnvVersion` are outside of the supported range. If so, emit
742
+ // a diagnostic and return an empty version to indicate that we should not attempt to install a
743
+ // different version of Go.
715
744
func checkForUnsupportedVersions (v versionInfo ) (msg , version string ) {
716
745
if v .goDirectiveFound && outsideSupportedRange (v .goModVersion ) {
717
746
msg = "The version of Go found in the `go.mod` file (" + v .goModVersion +
@@ -730,6 +759,10 @@ func checkForUnsupportedVersions(v versionInfo) (msg, version string) {
730
759
return msg , version
731
760
}
732
761
762
+ // Check if either `v.goInstallationFound` or `v.goDirectiveFound` are false. If so, emit
763
+ // a diagnostic and return the version to install, or the empty string if we should not attempt to
764
+ // install a version of Go. We assume that `checkForUnsupportedVersions` has already been
765
+ // called, so any versions that are found are within the supported range.
733
766
func checkForVersionsNotFound (v versionInfo ) (msg , version string ) {
734
767
if ! v .goInstallationFound && ! v .goDirectiveFound {
735
768
msg = "No version of Go installed and no `go.mod` file found. Writing an environment " +
@@ -754,6 +787,10 @@ func checkForVersionsNotFound(v versionInfo) (msg, version string) {
754
787
return msg , version
755
788
}
756
789
790
+ // Compare `v.goModVersion` and `v.goEnvVersion`. emit a diagnostic and return the version to
791
+ // install, or the empty string if we should not attempt to install a version of Go. We assume that
792
+ // `checkForUnsupportedVersions` and `checkForVersionsNotFound` have already been called, so both
793
+ // versions are found and are within the supported range.
757
794
func compareVersions (v versionInfo ) (msg , version string ) {
758
795
if semver .Compare ("v" + v .goModVersion , "v" + v .goEnvVersion ) > 0 {
759
796
msg = "The version of Go installed in the environment (" + v .goEnvVersion +
@@ -772,6 +809,8 @@ func compareVersions(v versionInfo) (msg, version string) {
772
809
return msg , version
773
810
}
774
811
812
+ // Check the versions of Go found in the environment and in the `go.mod` file, and return a
813
+ // version to install. If the version is the empty string then no installation is required.
775
814
func getVersionToInstall (v versionInfo ) (msg , version string ) {
776
815
msg , version = checkForUnsupportedVersions (v )
777
816
if msg != "" {
@@ -787,6 +826,10 @@ func getVersionToInstall(v versionInfo) (msg, version string) {
787
826
return msg , version
788
827
}
789
828
829
+ // Write an environment file to the current directory. If `version` is the empty string then
830
+ // write an empty environment file, otherwise write an environment file specifying the version
831
+ // of Go to install. The path to the environment file is specified by the
832
+ // CODEQL_EXTRACTOR_ENVIRONMENT_JSON environment variable, or defaults to `environment.json`.
790
833
func writeEnvironmentFile (version string ) {
791
834
var content string
792
835
if version == "" {
@@ -831,11 +874,13 @@ func (v versionInfo) String() string {
831
874
return fmt .Sprintf ("go.mod version: %s, go.mod directive found: %t, go env version: %s, go installation found: %t" , v .goModVersion , v .goDirectiveFound , v .goEnvVersion , v .goInstallationFound )
832
875
}
833
876
877
+ // Check if Go is installed in the environment.
834
878
func isGoInstalled () bool {
835
879
_ , err := exec .LookPath ("go" )
836
880
return err == nil
837
881
}
838
882
883
+ // Get the version of Go to install in the environment and write to an environment file.
839
884
func identifyEnvironment () {
840
885
var v versionInfo
841
886
depMode := getDepMode ()
0 commit comments