@@ -100,6 +100,7 @@ func tryBuild(buildFile, cmd string, args ...string) bool {
100100 return false
101101}
102102
103+ // Returns the import path of the package being built, or "" if it cannot be determined.
103104func getImportPath () (importpath string ) {
104105 importpath = os .Getenv ("LGTM_INDEX_IMPORT_PATH" )
105106 if importpath == "" {
@@ -124,6 +125,8 @@ func getImportPath() (importpath string) {
124125 return
125126}
126127
128+ // Returns the import path of the package being built from `repourl`, or "" if it cannot be
129+ // determined.
127130func getImportPathFromRepoURL (repourl string ) string {
128131 // check for scp-like URL as in "git@github.com:github/codeql-go.git"
129132 shorturl := regexp .MustCompile ("^([^@]+@)?([^:]+):([^/].*?)(\\ .git)?$" )
@@ -190,6 +193,8 @@ const (
190193 ModVendor
191194)
192195
196+ // argsForGoVersion returns the arguments to pass to the Go compiler for the given `ModMode` and
197+ // Go version
193198func (m ModMode ) argsForGoVersion (version string ) []string {
194199 switch m {
195200 case ModUnset :
@@ -229,6 +234,7 @@ func checkVendor() bool {
229234 return true
230235}
231236
237+ // Returns the directory containing the source code to be analyzed.
232238func getSourceDir () string {
233239 srcdir := os .Getenv ("LGTM_SRC" )
234240 if srcdir != "" {
@@ -244,6 +250,7 @@ func getSourceDir() string {
244250 return srcdir
245251}
246252
253+ // Returns the appropriate DependencyInstallerMode for the current project
247254func getDepMode () DependencyInstallerMode {
248255 if util .FileExists ("go.mod" ) {
249256 log .Println ("Found go.mod, enabling go modules" )
@@ -260,6 +267,7 @@ func getDepMode() DependencyInstallerMode {
260267 return GoGetNoModules
261268}
262269
270+ // Tries to open `go.mod` and read a go directive, returning the version and whether it was found.
263271func tryReadGoDirective (depMode DependencyInstallerMode ) (string , bool ) {
264272 version := ""
265273 found := false
@@ -285,6 +293,7 @@ func tryReadGoDirective(depMode DependencyInstallerMode) (string, bool) {
285293 return version , found
286294}
287295
296+ // Returns the appropriate ModMode for the current project
288297func getModMode (depMode DependencyInstallerMode ) ModMode {
289298 if depMode == GoGetWithModules {
290299 // 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 {
298307 return ModUnset
299308}
300309
310+ // fixGoVendorIssues fixes issues with go vendor for go version >= 1.14
301311func fixGoVendorIssues (modMode ModMode , depMode DependencyInstallerMode , goDirectiveFound bool ) ModMode {
302312 if modMode == ModVendor {
303313 // 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
327337 return modMode
328338}
329339
340+ // Determines whether the project needs a GOPATH set up
330341func getNeedGopath (depMode DependencyInstallerMode , importpath string ) bool {
331342 needGopath := true
332343 if depMode == GoGetWithModules {
@@ -349,6 +360,7 @@ func getNeedGopath(depMode DependencyInstallerMode, importpath string) bool {
349360 return needGopath
350361}
351362
363+ // Try to update `go.mod` and `go.sum` if the go version is >= 1.16.
352364func tryUpdateGoModAndGoSum (modMode ModMode , depMode DependencyInstallerMode ) {
353365 // Go 1.16 and later won't automatically attempt to update go.mod / go.sum during package loading, so try to update them here:
354366 if modMode != ModVendor && depMode == GoGetWithModules && semver .Compare (getEnvGoSemVer (), "v1.16" ) >= 0 {
@@ -394,6 +406,7 @@ type moveGopathInfo struct {
394406 files []string
395407}
396408
409+ // Moves all files in `srcdir` to a temporary directory with the correct layout to be added to the GOPATH
397410func moveToTemporaryGopath (srcdir string , importpath string ) moveGopathInfo {
398411 // a temporary directory where everything is moved while the correct
399412 // directory structure is created.
@@ -455,6 +468,8 @@ func moveToTemporaryGopath(srcdir string, importpath string) moveGopathInfo {
455468 }
456469}
457470
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.
458473func createPathTransformerFile (newdir string ) * os.File {
459474 err := os .Chdir (newdir )
460475 if err != nil {
@@ -470,6 +485,7 @@ func createPathTransformerFile(newdir string) *os.File {
470485 return pt
471486}
472487
488+ // Writes the path transformer file
473489func writePathTransformerFile (pt * os.File , realSrc , root , newdir string ) {
474490 _ , err := pt .WriteString ("#" + realSrc + "\n " + newdir + "//\n " )
475491 if err != nil {
@@ -485,6 +501,7 @@ func writePathTransformerFile(pt *os.File, realSrc, root, newdir string) {
485501 }
486502}
487503
504+ // Adds `root` to GOPATH.
488505func setGopath (root string ) {
489506 // set/extend GOPATH
490507 oldGopath := os .Getenv ("GOPATH" )
@@ -504,6 +521,8 @@ func setGopath(root string) {
504521 log .Printf ("GOPATH set to %s.\n " , newGopath )
505522}
506523
524+ // Try to build the project without custom commands. If that fails, return a boolean indicating
525+ // that we should install dependencies ourselves.
507526func buildWithoutCustomCommands (modMode ModMode ) bool {
508527 shouldInstallDependencies := false
509528 // try to build the project
@@ -523,6 +542,7 @@ func buildWithoutCustomCommands(modMode ModMode) bool {
523542 return shouldInstallDependencies
524543}
525544
545+ // Build the project with custom commands.
526546func buildWithCustomCommands (inst string ) {
527547 // write custom build commands into a script, then run it
528548 var (
@@ -556,6 +576,7 @@ func buildWithCustomCommands(inst string) {
556576 util .RunCmd (exec .Command (script .Name ()))
557577}
558578
579+ // Install dependencies using the given dependency installer mode.
559580func installDependencies (depMode DependencyInstallerMode ) {
560581 // automatically determine command to install dependencies
561582 var install * exec.Cmd
@@ -607,6 +628,7 @@ func installDependencies(depMode DependencyInstallerMode) {
607628 util .RunCmd (install )
608629}
609630
631+ // Run the extractor.
610632func extract (depMode DependencyInstallerMode , modMode ModMode ) {
611633 extractor , err := util .GetExtractorPath ()
612634 if err != nil {
@@ -634,6 +656,7 @@ func extract(depMode DependencyInstallerMode, modMode ModMode) {
634656 }
635657}
636658
659+ // Build the project and run the extractor.
637660func installDependenciesAndBuild () {
638661 log .Printf ("Autobuilder was built with %s, environment has %s\n " , runtime .Version (), getEnvGoVersion ())
639662
@@ -707,11 +730,17 @@ func installDependenciesAndBuild() {
707730const minGoVersion = "1.11"
708731const maxGoVersion = "1.20"
709732
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.
710736func outsideSupportedRange (version string ) bool {
711737 short := semver .MajorMinor ("v" + version )
712738 return semver .Compare (short , "v" + minGoVersion ) < 0 || semver .Compare (short , "v" + maxGoVersion ) > 0
713739}
714740
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.
715744func checkForUnsupportedVersions (v versionInfo ) (msg , version string ) {
716745 if v .goDirectiveFound && outsideSupportedRange (v .goModVersion ) {
717746 msg = "The version of Go found in the `go.mod` file (" + v .goModVersion +
@@ -730,6 +759,10 @@ func checkForUnsupportedVersions(v versionInfo) (msg, version string) {
730759 return msg , version
731760}
732761
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.
733766func checkForVersionsNotFound (v versionInfo ) (msg , version string ) {
734767 if ! v .goInstallationFound && ! v .goDirectiveFound {
735768 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) {
754787 return msg , version
755788}
756789
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.
757794func compareVersions (v versionInfo ) (msg , version string ) {
758795 if semver .Compare ("v" + v .goModVersion , "v" + v .goEnvVersion ) > 0 {
759796 msg = "The version of Go installed in the environment (" + v .goEnvVersion +
@@ -772,6 +809,8 @@ func compareVersions(v versionInfo) (msg, version string) {
772809 return msg , version
773810}
774811
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.
775814func getVersionToInstall (v versionInfo ) (msg , version string ) {
776815 msg , version = checkForUnsupportedVersions (v )
777816 if msg != "" {
@@ -787,6 +826,10 @@ func getVersionToInstall(v versionInfo) (msg, version string) {
787826 return msg , version
788827}
789828
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`.
790833func writeEnvironmentFile (version string ) {
791834 var content string
792835 if version == "" {
@@ -831,11 +874,13 @@ func (v versionInfo) String() string {
831874 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 )
832875}
833876
877+ // Check if Go is installed in the environment.
834878func isGoInstalled () bool {
835879 _ , err := exec .LookPath ("go" )
836880 return err == nil
837881}
838882
883+ // Get the version of Go to install in the environment and write to an environment file.
839884func identifyEnvironment () {
840885 var v versionInfo
841886 depMode := getDepMode ()
0 commit comments