Merge pull request #16480 from github/mbg/go/improve-script-fail-message

Go: Improve log messages in `buildWithoutCustomCommands`
This commit is contained in:
Michael B. Gale
2024-05-15 10:00:30 +01:00
committed by GitHub
3 changed files with 43 additions and 20 deletions

View File

@@ -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
}

View File

@@ -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()
// try to run a build script
scriptSucceeded, scriptsExecuted := autobuilder.Autobuild()
scriptCount := len(scriptsExecuted)
// 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.")
// 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
}

View File

@@ -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
}