From 9b8c0554a3357a79b294c7b00217694b4dd06351 Mon Sep 17 00:00:00 2001 From: asafambar Date: Thu, 23 May 2024 10:13:03 +0300 Subject: [PATCH] Curation go support - Add the ability to recognize error from curation when building go tree --- build/golang.go | 2 +- utils/goutils.go | 11 +++++++++-- utils/goutils_test.go | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/build/golang.go b/build/golang.go index 0755eba9..548bf5e9 100644 --- a/build/golang.go +++ b/build/golang.go @@ -81,7 +81,7 @@ func (gm *GoModule) loadDependencies() ([]entities.Dependency, error) { } func (gm *GoModule) getGoDependencies(cachePath string) (map[string]entities.Dependency, error) { - modulesMap, err := utils.GetDependenciesList(gm.srcPath, gm.containingBuild.logger) + modulesMap, err := utils.GetDependenciesList(gm.srcPath, gm.containingBuild.logger, nil) if err != nil || len(modulesMap) == 0 { return nil, err } diff --git a/utils/goutils.go b/utils/goutils.go index 1b988dbd..45d30693 100644 --- a/utils/goutils.go +++ b/utils/goutils.go @@ -94,16 +94,23 @@ func getListCmdArgs() (cmdArgs []string, err error) { return []string{"list", "-mod=mod"}, nil } +type HandleErrorFunc func(err error) (bool, error) + // Runs go list -f {{with .Module}}{{.Path}}:{{.Version}}{{end}} all command and returns map of the dependencies -func GetDependenciesList(projectDir string, log Log) (map[string]bool, error) { +func GetDependenciesList(projectDir string, log Log, handleError HandleErrorFunc) (map[string]bool, error) { cmdArgs, err := getListCmdArgs() if err != nil { return nil, err } output, err := runDependenciesCmd(projectDir, append(cmdArgs, "-f", "{{with .Module}}{{.Path}}:{{.Version}}{{end}}", "all"), log) if err != nil { - // Errors occurred while running "go list". Run again and this time ignore errors (with '-e') log.Warn("Errors occurred while building the Go dependency tree. The dependency tree may be incomplete: " + err.Error()) + if handleError != nil { + if stop, newErr := handleError(err); stop { + return nil, newErr + } + } + // Errors occurred while running "go list". Run again and this time ignore errors (with '-e') output, err = runDependenciesCmd(projectDir, append(cmdArgs, "-e", "-f", "{{with .Module}}{{.Path}}:{{.Version}}{{end}}", "all"), log) if err != nil { return nil, err diff --git a/utils/goutils_test.go b/utils/goutils_test.go index 857b3b7f..7ad3448a 100644 --- a/utils/goutils_test.go +++ b/utils/goutils_test.go @@ -113,7 +113,7 @@ func testGetDependenciesList(t *testing.T, testDir string) { err = os.Rename(filepath.Join(goModPath, "test.go"), filepath.Join(goModPath, "test.go.txt")) assert.NoError(t, err) }() - actual, err := GetDependenciesList(goModPath, log) + actual, err := GetDependenciesList(goModPath, log, nil) assert.NoError(t, err) // Since Go 1.16 'go list' command won't automatically update go.mod and go.sum.