Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc75e44

Browse files
committed
Go: Allow GetBuildInfo to return multiple BuildInfo objects
1 parent 4f5c43a commit fc75e44

3 files changed

Lines changed: 75 additions & 77 deletions

File tree

go/extractor/autobuilder/build-environment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ func outputEnvironmentJson(version string) {
267267
// Get the version of Go to install and output it to stdout as json.
268268
func IdentifyEnvironment() {
269269
var v versionInfo
270-
buildInfo := project.GetBuildInfo(false)
271-
goVersionInfo := project.TryReadGoDirective(buildInfo)
270+
buildInfos := project.GetBuildInfo(false)
271+
goVersionInfo := project.TryReadGoDirective(buildInfos[0]) // TODO: find greatest version
272272
v.goModVersion, v.goModVersionFound = goVersionInfo.Version, goVersionInfo.Found
273273

274274
v.goEnvVersionFound = toolchain.IsInstalled()

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -546,75 +546,77 @@ func installDependenciesAndBuild() {
546546

547547
// determine how to install dependencies and whether a GOPATH needs to be set up before
548548
// extraction
549-
buildInfo := project.GetBuildInfo(true)
549+
buildInfos := project.GetBuildInfo(true)
550550
if _, present := os.LookupEnv("GO111MODULE"); !present {
551551
os.Setenv("GO111MODULE", "auto")
552552
}
553553

554-
goVersionInfo := project.TryReadGoDirective(buildInfo)
554+
for _, buildInfo := range buildInfos {
555+
goVersionInfo := project.TryReadGoDirective(buildInfo)
555556

556-
// This diagnostic is not required if the system Go version is 1.21 or greater, since the
557-
// Go tooling should install required Go versions as needed.
558-
if semver.Compare(toolchain.GetEnvGoSemVer(), "v1.21.0") < 0 && goVersionInfo.Found && semver.Compare("v"+goVersionInfo.Version, toolchain.GetEnvGoSemVer()) > 0 {
559-
diagnostics.EmitNewerGoVersionNeeded(toolchain.GetEnvGoSemVer(), "v"+goVersionInfo.Version)
560-
if val, _ := os.LookupEnv("GITHUB_ACTIONS"); val == "true" {
561-
log.Printf(
562-
"The go.mod file requires version %s of Go, but version %s is installed. Consider adding an actions/setup-go step to your workflow.\n",
563-
"v"+goVersionInfo.Version,
564-
toolchain.GetEnvGoSemVer())
557+
// This diagnostic is not required if the system Go version is 1.21 or greater, since the
558+
// Go tooling should install required Go versions as needed.
559+
if semver.Compare(toolchain.GetEnvGoSemVer(), "v1.21.0") < 0 && goVersionInfo.Found && semver.Compare("v"+goVersionInfo.Version, toolchain.GetEnvGoSemVer()) > 0 {
560+
diagnostics.EmitNewerGoVersionNeeded(toolchain.GetEnvGoSemVer(), "v"+goVersionInfo.Version)
561+
if val, _ := os.LookupEnv("GITHUB_ACTIONS"); val == "true" {
562+
log.Printf(
563+
"The go.mod file requires version %s of Go, but version %s is installed. Consider adding an actions/setup-go step to your workflow.\n",
564+
"v"+goVersionInfo.Version,
565+
toolchain.GetEnvGoSemVer())
566+
}
565567
}
566-
}
567568

568-
fixGoVendorIssues(&buildInfo, goVersionInfo.Found)
569+
fixGoVendorIssues(&buildInfo, goVersionInfo.Found)
569570

570-
tryUpdateGoModAndGoSum(buildInfo)
571+
tryUpdateGoModAndGoSum(buildInfo)
571572

572-
importpath := getImportPath()
573-
needGopath := getNeedGopath(buildInfo, importpath)
573+
importpath := getImportPath()
574+
needGopath := getNeedGopath(buildInfo, importpath)
574575

575-
inLGTM := os.Getenv("LGTM_SRC") != "" || os.Getenv("LGTM_INDEX_NEED_GOPATH") != ""
576+
inLGTM := os.Getenv("LGTM_SRC") != "" || os.Getenv("LGTM_INDEX_NEED_GOPATH") != ""
576577

577-
if inLGTM && needGopath {
578-
paths := moveToTemporaryGopath(srcdir, importpath)
578+
if inLGTM && needGopath {
579+
paths := moveToTemporaryGopath(srcdir, importpath)
579580

580-
// schedule restoring the contents of newdir to their original location after this function completes:
581-
defer restoreRepoLayout(paths.newdir, paths.files, filepath.Base(paths.scratch), srcdir)
581+
// schedule restoring the contents of newdir to their original location after this function completes:
582+
defer restoreRepoLayout(paths.newdir, paths.files, filepath.Base(paths.scratch), srcdir)
582583

583-
pt := createPathTransformerFile(paths.newdir)
584-
defer os.Remove(pt.Name())
584+
pt := createPathTransformerFile(paths.newdir)
585+
defer os.Remove(pt.Name())
585586

586-
writePathTransformerFile(pt, paths.realSrc, paths.root, paths.newdir)
587-
setGopath(paths.root)
588-
}
589-
590-
// check whether an explicit dependency installation command was provided
591-
inst := util.Getenv("CODEQL_EXTRACTOR_GO_BUILD_COMMAND", "LGTM_INDEX_BUILD_COMMAND")
592-
shouldInstallDependencies := false
593-
if inst == "" {
594-
shouldInstallDependencies = buildWithoutCustomCommands(buildInfo.ModMode)
595-
} else {
596-
buildWithCustomCommands(inst)
597-
}
587+
writePathTransformerFile(pt, paths.realSrc, paths.root, paths.newdir)
588+
setGopath(paths.root)
589+
}
598590

599-
if buildInfo.ModMode == project.ModVendor {
600-
// test if running `go` with -mod=vendor works, and if it doesn't, try to fallback to -mod=mod
601-
// or not set if the go version < 1.14. Note we check this post-build in case the build brings
602-
// the vendor directory up to date.
603-
if !checkVendor() {
604-
buildInfo.ModMode = project.ModMod
605-
log.Println("The vendor directory is not consistent with the go.mod; not using vendored dependencies.")
591+
// check whether an explicit dependency installation command was provided
592+
inst := util.Getenv("CODEQL_EXTRACTOR_GO_BUILD_COMMAND", "LGTM_INDEX_BUILD_COMMAND")
593+
shouldInstallDependencies := false
594+
if inst == "" {
595+
shouldInstallDependencies = buildWithoutCustomCommands(buildInfo.ModMode)
596+
} else {
597+
buildWithCustomCommands(inst)
606598
}
607-
}
608599

609-
if shouldInstallDependencies {
610600
if buildInfo.ModMode == project.ModVendor {
611-
log.Printf("Skipping dependency installation because a Go vendor directory was found.")
612-
} else {
613-
installDependencies(buildInfo)
601+
// test if running `go` with -mod=vendor works, and if it doesn't, try to fallback to -mod=mod
602+
// or not set if the go version < 1.14. Note we check this post-build in case the build brings
603+
// the vendor directory up to date.
604+
if !checkVendor() {
605+
buildInfo.ModMode = project.ModMod
606+
log.Println("The vendor directory is not consistent with the go.mod; not using vendored dependencies.")
607+
}
614608
}
615-
}
616609

617-
extract(buildInfo)
610+
if shouldInstallDependencies {
611+
if buildInfo.ModMode == project.ModVendor {
612+
log.Printf("Skipping dependency installation because a Go vendor directory was found.")
613+
} else {
614+
installDependencies(buildInfo)
615+
}
616+
}
617+
618+
extract(buildInfo)
619+
}
618620
}
619621

620622
func main() {

go/extractor/project/project.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ func discoverWorkspaces(emitDiagnostics bool) []GoWorkspace {
185185

186186
// Returns the directory to run the go build in and whether to use a go.mod
187187
// file.
188-
func getBuildRoot(emitDiagnostics bool) (baseDir string, useGoMod bool) {
188+
func getBuildRoot(emitDiagnostics bool) (baseDirs []string, useGoMod bool) {
189189
goModPaths := util.FindAllFilesWithName(".", "go.mod", "vendor")
190190
if len(goModPaths) == 0 {
191-
baseDir = "."
191+
baseDirs = []string{"."}
192192
useGoMod = false
193193
return
194194
}
@@ -197,13 +197,13 @@ func getBuildRoot(emitDiagnostics bool) (baseDir string, useGoMod bool) {
197197
if emitDiagnostics {
198198
diagnostics.EmitGoFilesOutsideGoModules(goModPaths)
199199
}
200-
baseDir = "."
200+
baseDirs = []string{"."}
201201
useGoMod = false
202202
return
203203
}
204204
if len(goModPaths) > 1 {
205205
// currently not supported
206-
baseDir = "."
206+
baseDirs = []string{"."}
207207
commonRoot, nested := checkDirsNested(goModDirs)
208208
if nested && commonRoot == "" {
209209
useGoMod = true
@@ -216,17 +216,7 @@ func getBuildRoot(emitDiagnostics bool) (baseDir string, useGoMod bool) {
216216
} else {
217217
diagnostics.EmitMultipleGoModFoundNotNested(goModPaths)
218218
}
219-
}
220-
return
221-
}
222-
if emitDiagnostics {
223-
if goModDirs[0] == "." {
224-
diagnostics.EmitSingleRootGoModFound(goModPaths[0])
225-
} else {
226-
diagnostics.EmitSingleNonRootGoModFound(goModPaths[0])
227-
}
228-
}
229-
baseDir = goModDirs[0]
219+
baseDirs = goModDirs
230220
useGoMod = true
231221
return
232222
}
@@ -246,7 +236,7 @@ const (
246236
)
247237

248238
// Returns the appropriate DependencyInstallerMode for the current project
249-
func getDepMode(emitDiagnostics bool) (DependencyInstallerMode, string) {
239+
func getDepMode(emitDiagnostics bool) (DependencyInstallerMode, []string) {
250240
bazelPaths := util.FindAllFilesWithName(".", "BUILD", "vendor")
251241
bazelPaths = append(bazelPaths, util.FindAllFilesWithName(".", "BUILD.bazel", "vendor")...)
252242
if len(bazelPaths) > 0 {
@@ -264,28 +254,28 @@ func getDepMode(emitDiagnostics bool) (DependencyInstallerMode, string) {
264254
}
265255
}
266256

267-
baseDir, useGoMod := getBuildRoot(emitDiagnostics)
257+
baseDirs, useGoMod := getBuildRoot(emitDiagnostics)
268258
if useGoMod {
269-
log.Println("Found go.mod, enabling go modules")
270-
return GoGetWithModules, baseDir
259+
log.Printf("Found go.mod files in %s: enabling go modules.\n", strings.Join(baseDirs, ", "))
260+
return GoGetWithModules, baseDirs
271261
}
272262

273263
if util.FileExists("Gopkg.toml") {
274264
if emitDiagnostics {
275265
diagnostics.EmitGopkgTomlFound()
276266
}
277267
log.Println("Found Gopkg.toml, using dep instead of go get")
278-
return Dep, "."
268+
return Dep, []string{"."}
279269
}
280270

281271
if util.FileExists("glide.yaml") {
282272
if emitDiagnostics {
283273
diagnostics.EmitGlideYamlFound()
284274
}
285275
log.Println("Found glide.yaml, using Glide instead of go get")
286-
return Glide, "."
276+
return Glide, []string{"."}
287277
}
288-
return GoGetNoModules, "."
278+
return GoGetNoModules, []string{"."}
289279
}
290280

291281
// ModMode corresponds to the possible values of the -mod flag for the Go compiler
@@ -341,10 +331,16 @@ type BuildInfo struct {
341331
BaseDir string
342332
}
343333

344-
func GetBuildInfo(emitDiagnostics bool) BuildInfo {
345-
depMode, baseDir := getDepMode(true)
346-
modMode := getModMode(depMode, baseDir)
347-
return BuildInfo{depMode, modMode, baseDir}
334+
func GetBuildInfo(emitDiagnostics bool) []BuildInfo {
335+
depMode, baseDirs := getDepMode(true)
336+
results := make([]BuildInfo, len(baseDirs))
337+
338+
for i, baseDir := range baseDirs {
339+
modMode := getModMode(depMode, baseDir)
340+
results[i] = BuildInfo{depMode, modMode, baseDir}
341+
}
342+
343+
return results
348344
}
349345

350346
type GoVersionInfo struct {

0 commit comments

Comments
 (0)