Skip to content

Commit

Permalink
Curation go support - Add the ability to recognize error from curatio…
Browse files Browse the repository at this point in the history
…n when building go tree
  • Loading branch information
asafambar committed May 23, 2024
1 parent 0093dee commit 9b8c055
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 9 additions & 2 deletions utils/goutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion utils/goutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9b8c055

Please sign in to comment.