diff --git a/go/extractor/autobuilder/autobuilder.go b/go/extractor/autobuilder/autobuilder.go index eec9ba84c04b..b9405a764d40 100644 --- a/go/extractor/autobuilder/autobuilder.go +++ b/go/extractor/autobuilder/autobuilder.go @@ -54,13 +54,15 @@ func checkExtractorRun() bool { } // tryBuildIfExists tries to run the command `cmd args...` if the file `buildFile` exists and is not -// a directory. Returns true if the command was successful and false if not. -func tryBuildIfExists(buildFile, cmd string, args ...string) bool { - if util.FileExists(buildFile) { +// a directory. Returns values indicating whether the script succeeded as well as whether the script was found. +func tryBuildIfExists(buildFile, cmd string, args ...string) (scriptSuccess bool, scriptFound bool) { + scriptSuccess = false + scriptFound = util.FileExists(buildFile) + if scriptFound { log.Printf("%s found.\n", buildFile) - return tryBuild(cmd, args...) + scriptSuccess = tryBuild(cmd, args...) } - return false + return } // tryBuild tries to run `cmd args...`, returning true if successful and false if not. @@ -92,11 +94,25 @@ var BuildScripts = []BuildScript{ // Autobuild attempts to detect build systems based on the presence of build scripts from the // list in `BuildScripts` and run the corresponding command. This may invoke zero or more // build scripts in the order given by `BuildScripts`. -func Autobuild() bool { +// Returns `scriptSuccess` which indicates whether a build script was successfully executed. +// Returns `scriptsExecuted` which contains the names of all build scripts that were executed. +func Autobuild() (scriptSuccess bool, scriptsExecuted []string) { + scriptSuccess = false + scriptsExecuted = []string{} + for _, script := range BuildScripts { - if tryBuildIfExists(script.Filename, script.Tool) { - return true + // Try to run the build script + success, scriptFound := tryBuildIfExists(script.Filename, script.Tool) + + // If it was found, we attempted to run it: add it to the array. + if scriptFound { + scriptsExecuted = append(scriptsExecuted, script.Filename) + } + // If it was successfully executed, we stop here. + if success { + scriptSuccess = true + return } } - return false + return } diff --git a/go/extractor/cli/go-autobuilder/go-autobuilder.go b/go/extractor/cli/go-autobuilder/go-autobuilder.go index a4d01b29c5d7..07b41c98b0a7 100644 --- a/go/extractor/cli/go-autobuilder/go-autobuilder.go +++ b/go/extractor/cli/go-autobuilder/go-autobuilder.go @@ -320,21 +320,26 @@ func setGopath(root string) { log.Printf("GOPATH set to %s.\n", newGopath) } -// Try to build the project without custom commands. If that fails, return a boolean indicating -// that we should install dependencies ourselves. +// Try to build the project with a build script. If that fails, return a boolean indicating +// that we should install dependencies in the normal way. func buildWithoutCustomCommands(modMode project.ModMode) bool { shouldInstallDependencies := false - // try to build the project - buildSucceeded := autobuilder.Autobuild() - - // Build failed or there are still dependency errors; we'll try to install dependencies - // ourselves - if !buildSucceeded { - log.Println("Build failed, continuing to install dependencies.") + // try to run a build script + scriptSucceeded, scriptsExecuted := autobuilder.Autobuild() + scriptCount := len(scriptsExecuted) + + // If there is no build script we could invoke successfully or there are still dependency errors; + // we'll try to install dependencies ourselves in the normal Go way. + if !scriptSucceeded { + if scriptCount > 0 { + log.Printf("Unsuccessfully ran %d build scripts(s), continuing to install dependencies in the normal way.\n", scriptCount) + } else { + log.Println("Unable to find any build scripts, continuing to install dependencies in the normal way.") + } shouldInstallDependencies = true } else if toolchain.DepErrors("./...", modMode.ArgsForGoVersion(toolchain.GetEnvGoSemVer())...) { - log.Println("Dependencies are still not resolving after the build, continuing to install dependencies.") + log.Printf("Dependencies are still not resolving after executing %d build script(s), continuing to install dependencies in the normal way.\n", scriptCount) shouldInstallDependencies = true } diff --git a/go/extractor/cli/go-build-runner/go-build-runner.go b/go/extractor/cli/go-build-runner/go-build-runner.go index 118de5caf2ef..8c6c4f47651f 100644 --- a/go/extractor/cli/go-build-runner/go-build-runner.go +++ b/go/extractor/cli/go-build-runner/go-build-runner.go @@ -1,20 +1,22 @@ package main import ( - "github.com/github/codeql-go/extractor/util" "log" "os" "os/exec" "path/filepath" "runtime" + "github.com/github/codeql-go/extractor/util" + "github.com/github/codeql-go/extractor/autobuilder" ) func main() { // check if a build command has successfully extracted something autobuilder.CheckExtracted = true - if autobuilder.Autobuild() { + scriptSuccess, _ := autobuilder.Autobuild() + if scriptSuccess { return }