Skip to content

Commit 964b3f2

Browse files
authored
Merge pull request github#16480 from github/mbg/go/improve-script-fail-message
Go: Improve log messages in `buildWithoutCustomCommands`
2 parents 586e900 + 38e1065 commit 964b3f2

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

go/extractor/autobuilder/autobuilder.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ func checkExtractorRun() bool {
5454
}
5555

5656
// tryBuildIfExists tries to run the command `cmd args...` if the file `buildFile` exists and is not
57-
// a directory. Returns true if the command was successful and false if not.
58-
func tryBuildIfExists(buildFile, cmd string, args ...string) bool {
59-
if util.FileExists(buildFile) {
57+
// a directory. Returns values indicating whether the script succeeded as well as whether the script was found.
58+
func tryBuildIfExists(buildFile, cmd string, args ...string) (scriptSuccess bool, scriptFound bool) {
59+
scriptSuccess = false
60+
scriptFound = util.FileExists(buildFile)
61+
if scriptFound {
6062
log.Printf("%s found.\n", buildFile)
61-
return tryBuild(cmd, args...)
63+
scriptSuccess = tryBuild(cmd, args...)
6264
}
63-
return false
65+
return
6466
}
6567

6668
// tryBuild tries to run `cmd args...`, returning true if successful and false if not.
@@ -92,11 +94,25 @@ var BuildScripts = []BuildScript{
9294
// Autobuild attempts to detect build systems based on the presence of build scripts from the
9395
// list in `BuildScripts` and run the corresponding command. This may invoke zero or more
9496
// build scripts in the order given by `BuildScripts`.
95-
func Autobuild() bool {
97+
// Returns `scriptSuccess` which indicates whether a build script was successfully executed.
98+
// Returns `scriptsExecuted` which contains the names of all build scripts that were executed.
99+
func Autobuild() (scriptSuccess bool, scriptsExecuted []string) {
100+
scriptSuccess = false
101+
scriptsExecuted = []string{}
102+
96103
for _, script := range BuildScripts {
97-
if tryBuildIfExists(script.Filename, script.Tool) {
98-
return true
104+
// Try to run the build script
105+
success, scriptFound := tryBuildIfExists(script.Filename, script.Tool)
106+
107+
// If it was found, we attempted to run it: add it to the array.
108+
if scriptFound {
109+
scriptsExecuted = append(scriptsExecuted, script.Filename)
110+
}
111+
// If it was successfully executed, we stop here.
112+
if success {
113+
scriptSuccess = true
114+
return
99115
}
100116
}
101-
return false
117+
return
102118
}

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -320,21 +320,26 @@ func setGopath(root string) {
320320
log.Printf("GOPATH set to %s.\n", newGopath)
321321
}
322322

323-
// Try to build the project without custom commands. If that fails, return a boolean indicating
324-
// that we should install dependencies ourselves.
323+
// Try to build the project with a build script. If that fails, return a boolean indicating
324+
// that we should install dependencies in the normal way.
325325
func buildWithoutCustomCommands(modMode project.ModMode) bool {
326326
shouldInstallDependencies := false
327-
// try to build the project
328-
buildSucceeded := autobuilder.Autobuild()
329-
330-
// Build failed or there are still dependency errors; we'll try to install dependencies
331-
// ourselves
332-
if !buildSucceeded {
333-
log.Println("Build failed, continuing to install dependencies.")
327+
// try to run a build script
328+
scriptSucceeded, scriptsExecuted := autobuilder.Autobuild()
329+
scriptCount := len(scriptsExecuted)
330+
331+
// If there is no build script we could invoke successfully or there are still dependency errors;
332+
// we'll try to install dependencies ourselves in the normal Go way.
333+
if !scriptSucceeded {
334+
if scriptCount > 0 {
335+
log.Printf("Unsuccessfully ran %d build scripts(s), continuing to install dependencies in the normal way.\n", scriptCount)
336+
} else {
337+
log.Println("Unable to find any build scripts, continuing to install dependencies in the normal way.")
338+
}
334339

335340
shouldInstallDependencies = true
336341
} else if toolchain.DepErrors("./...", modMode.ArgsForGoVersion(toolchain.GetEnvGoSemVer())...) {
337-
log.Println("Dependencies are still not resolving after the build, continuing to install dependencies.")
342+
log.Printf("Dependencies are still not resolving after executing %d build script(s), continuing to install dependencies in the normal way.\n", scriptCount)
338343

339344
shouldInstallDependencies = true
340345
}

go/extractor/cli/go-build-runner/go-build-runner.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package main
22

33
import (
4-
"github.com/github/codeql-go/extractor/util"
54
"log"
65
"os"
76
"os/exec"
87
"path/filepath"
98
"runtime"
109

10+
"github.com/github/codeql-go/extractor/util"
11+
1112
"github.com/github/codeql-go/extractor/autobuilder"
1213
)
1314

1415
func main() {
1516
// check if a build command has successfully extracted something
1617
autobuilder.CheckExtracted = true
17-
if autobuilder.Autobuild() {
18+
scriptSuccess, _ := autobuilder.Autobuild()
19+
if scriptSuccess {
1820
return
1921
}
2022

0 commit comments

Comments
 (0)