Skip to content

Go: Improve log messages in buildWithoutCustomCommands #16480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions go/extractor/autobuilder/autobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
25 changes: 15 additions & 10 deletions go/extractor/cli/go-autobuilder/go-autobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions go/extractor/cli/go-build-runner/go-build-runner.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down