Skip to content

Commit e7a60b7

Browse files
committed
Go: Check dependencies per workspace
1 parent b9586a8 commit e7a60b7

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,7 @@ func setGopath(root string) {
320320

321321
// Try to build the project with a build script. If that fails, return a boolean indicating
322322
// that we should install dependencies in the normal way.
323-
func buildWithoutCustomCommands(modMode project.ModMode) bool {
324-
shouldInstallDependencies := false
323+
func buildWithoutCustomCommands(workspaces []project.GoWorkspace) {
325324
// try to run a build script
326325
scriptSucceeded, scriptsExecuted := autobuilder.Autobuild()
327326
scriptCount := len(scriptsExecuted)
@@ -335,13 +334,19 @@ func buildWithoutCustomCommands(modMode project.ModMode) bool {
335334
log.Println("Unable to find any build scripts, continuing to install dependencies in the normal way.")
336335
}
337336

338-
shouldInstallDependencies = true
339-
} else if toolchain.DepErrors("./...", modMode.ArgsForGoVersion(toolchain.GetEnvGoSemVer())...) {
340-
log.Printf("Dependencies are still not resolving after executing %d build script(s), continuing to install dependencies in the normal way.\n", scriptCount)
337+
// Install dependencies for all workspaces.
338+
for i, _ := range workspaces {
339+
workspaces[i].ShouldInstallDependencies = true
340+
}
341+
} else {
342+
for i, workspace := range workspaces {
343+
if toolchain.DepErrors("./...", workspace.ModMode.ArgsForGoVersion(toolchain.GetEnvGoSemVer())...) {
344+
log.Printf("Dependencies are still not resolving for `%s` after executing %d build script(s), continuing to install dependencies in the normal way.\n", workspace.BaseDir, scriptCount)
341345

342-
shouldInstallDependencies = true
346+
workspaces[i].ShouldInstallDependencies = true
347+
}
348+
}
343349
}
344-
return shouldInstallDependencies
345350
}
346351

347352
// Build the project with custom commands.
@@ -562,17 +567,16 @@ func installDependenciesAndBuild() {
562567
tryUpdateGoModAndGoSum(workspace)
563568
}
564569

570+
// check whether an explicit dependency installation command was provided
571+
inst := util.Getenv("CODEQL_EXTRACTOR_GO_BUILD_COMMAND", "LGTM_INDEX_BUILD_COMMAND")
572+
if inst == "" {
573+
buildWithoutCustomCommands(workspaces)
574+
} else {
575+
buildWithCustomCommands(inst)
576+
}
577+
565578
// Attempt to extract all workspaces; we will tolerate individual extraction failures here
566579
for i, workspace := range workspaces {
567-
// check whether an explicit dependency installation command was provided
568-
inst := util.Getenv("CODEQL_EXTRACTOR_GO_BUILD_COMMAND", "LGTM_INDEX_BUILD_COMMAND")
569-
shouldInstallDependencies := false
570-
if inst == "" {
571-
shouldInstallDependencies = buildWithoutCustomCommands(workspace.ModMode)
572-
} else {
573-
buildWithCustomCommands(inst)
574-
}
575-
576580
if workspace.ModMode == project.ModVendor {
577581
// test if running `go` with -mod=vendor works, and if it doesn't, try to fallback to -mod=mod
578582
// or not set if the go version < 1.14. Note we check this post-build in case the build brings
@@ -583,7 +587,7 @@ func installDependenciesAndBuild() {
583587
}
584588
}
585589

586-
if shouldInstallDependencies {
590+
if workspace.ShouldInstallDependencies {
587591
if workspace.ModMode == project.ModVendor {
588592
log.Printf("Skipping dependency installation because a Go vendor directory was found.")
589593
} else {

go/extractor/project/project.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ type GoWorkspace struct {
6868
DepMode DependencyInstallerMode // A value indicating how to install dependencies for this workspace
6969
ModMode ModMode // A value indicating which module mode to use for this workspace
7070
Extracted bool // A value indicating whether this workspace was extracted successfully
71+
72+
ShouldInstallDependencies bool // A value indicating whether dependencies should be installed for this module
7173
}
7274

7375
// Represents a nullable version string.

0 commit comments

Comments
 (0)