Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 1f6d6bb

Browse files
authored
cmd/dep: make checkErrors more lenient (#997)
checkErrors doesn't allow dep ensure to be used in a project that contains any Go files containing build errors. This commit update checkErrors to return a warning instead of an error as long as the project contains some compilable Go files. Fixes #995 Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent a917880 commit 1f6d6bb

File tree

4 files changed

+80
-24
lines changed

4 files changed

+80
-24
lines changed

cmd/dep/ensure.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,12 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
174174
return errors.Wrap(err, "ensure ListPackage for project")
175175
}
176176

177-
if err := checkErrors(params.RootPackageTree.Packages); err != nil {
178-
return err
177+
if fatal, err := checkErrors(params.RootPackageTree.Packages); err != nil {
178+
if fatal {
179+
return err
180+
} else if ctx.Verbose {
181+
ctx.Out.Println(err)
182+
}
179183
}
180184

181185
if cmd.add {
@@ -743,10 +747,10 @@ func getProjectConstraint(arg string, sm gps.SourceManager) (gps.ProjectConstrai
743747
return gps.ProjectConstraint{Ident: pi, Constraint: c}, arg, nil
744748
}
745749

746-
func checkErrors(m map[string]pkgtree.PackageOrErr) error {
750+
func checkErrors(m map[string]pkgtree.PackageOrErr) (fatal bool, err error) {
747751
var (
748-
buildErrors []string
749-
noGoErrors int
752+
noGoErrors int
753+
pkgtreeErrors = make(pkgtreeErrs, 0, len(m))
750754
)
751755

752756
for _, poe := range m {
@@ -755,18 +759,42 @@ func checkErrors(m map[string]pkgtree.PackageOrErr) error {
755759
case *build.NoGoError:
756760
noGoErrors++
757761
default:
758-
buildErrors = append(buildErrors, poe.Err.Error())
762+
pkgtreeErrors = append(pkgtreeErrors, poe.Err)
759763
}
760764
}
761765
}
762766

767+
// If pkgtree was empty or all dirs lacked any Go code, return an error.
763768
if len(m) == 0 || len(m) == noGoErrors {
764-
return errors.New("all dirs lacked any go code")
769+
return true, errors.New("no dirs contained any Go code")
765770
}
766771

767-
if len(buildErrors) > 0 {
768-
return errors.Errorf("Found %d errors:\n\n%s", len(buildErrors), strings.Join(buildErrors, "\n"))
772+
// If all dirs contained build errors, return an error.
773+
if len(m) == len(pkgtreeErrors) {
774+
return true, errors.New("all dirs contained build errors")
769775
}
770776

771-
return nil
777+
// If all directories either had no Go files or caused a build error, return an error.
778+
if len(m) == len(pkgtreeErrors)+noGoErrors {
779+
return true, pkgtreeErrors
780+
}
781+
782+
// If m contained some errors, return a warning with those errors.
783+
if len(pkgtreeErrors) > 0 {
784+
return false, pkgtreeErrors
785+
}
786+
787+
return false, nil
788+
}
789+
790+
type pkgtreeErrs []error
791+
792+
func (e pkgtreeErrs) Error() string {
793+
errs := make([]string, 0, len(e))
794+
795+
for _, err := range e {
796+
errs = append(errs, err.Error())
797+
}
798+
799+
return fmt.Sprintf("found %d errors in the package tree:\n%s", len(e), strings.Join(errs, "\n"))
772800
}

cmd/dep/ensure_test.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ func TestInvalidEnsureFlagCombinations(t *testing.T) {
5656
func TestCheckErrors(t *testing.T) {
5757
tt := []struct {
5858
name string
59-
hasErrs bool
59+
fatal bool
6060
pkgOrErrMap map[string]pkgtree.PackageOrErr
6161
}{
6262
{
63-
name: "noErrors",
64-
hasErrs: false,
63+
name: "noErrors",
64+
fatal: false,
6565
pkgOrErrMap: map[string]pkgtree.PackageOrErr{
6666
"mypkg": {
6767
P: pkgtree.Package{},
6868
},
6969
},
7070
},
7171
{
72-
name: "hasErrors",
73-
hasErrs: true,
72+
name: "hasErrors",
73+
fatal: true,
7474
pkgOrErrMap: map[string]pkgtree.PackageOrErr{
7575
"github.com/me/pkg": {
7676
Err: &build.NoGoError{},
@@ -81,8 +81,8 @@ func TestCheckErrors(t *testing.T) {
8181
},
8282
},
8383
{
84-
name: "onlyGoErrors",
85-
hasErrs: false,
84+
name: "onlyGoErrors",
85+
fatal: false,
8686
pkgOrErrMap: map[string]pkgtree.PackageOrErr{
8787
"github.com/me/pkg": {
8888
Err: &build.NoGoError{},
@@ -93,20 +93,48 @@ func TestCheckErrors(t *testing.T) {
9393
},
9494
},
9595
{
96-
name: "allGoErrors",
97-
hasErrs: true,
96+
name: "onlyBuildErrors",
97+
fatal: false,
9898
pkgOrErrMap: map[string]pkgtree.PackageOrErr{
9999
"github.com/me/pkg": {
100100
Err: &build.NoGoError{},
101101
},
102+
"github.com/someone/pkg": {
103+
P: pkgtree.Package{},
104+
},
105+
},
106+
},
107+
{
108+
name: "allGoErrors",
109+
fatal: true,
110+
pkgOrErrMap: map[string]pkgtree.PackageOrErr{
111+
"github.com/me/pkg": {
112+
Err: &build.NoGoError{},
113+
},
114+
},
115+
},
116+
{
117+
name: "allMixedErrors",
118+
fatal: true,
119+
pkgOrErrMap: map[string]pkgtree.PackageOrErr{
120+
"github.com/me/pkg": {
121+
Err: &build.NoGoError{},
122+
},
123+
"github.com/someone/pkg": {
124+
Err: errors.New("code is busted"),
125+
},
102126
},
103127
},
104128
}
105129

106130
for _, tc := range tt {
107131
t.Run(tc.name, func(t *testing.T) {
108-
if hasErrs := checkErrors(tc.pkgOrErrMap) != nil; hasErrs != tc.hasErrs {
109-
t.Fail()
132+
fatal, err := checkErrors(tc.pkgOrErrMap)
133+
if tc.fatal != fatal {
134+
t.Fatalf("expected fatal flag to be %T, got %T", tc.fatal, fatal)
135+
}
136+
if err == nil && fatal {
137+
t.Fatal("unexpected fatal flag value while err is nil")
110138
}
111139
})
112140
}

cmd/dep/testdata/harness_tests/ensure/pkg-errors/case1/testcase.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
["init", "-no-examples", "-skip-tools"],
44
["ensure", "-update"]
55
],
6-
"error-expected": "all dirs lacked any go code",
6+
"error-expected": "no dirs contained any Go code",
77
"vendor-final": []
88
}

cmd/dep/testdata/harness_tests/ensure/pkg-errors/case2/testcase.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"commands": [
33
["ensure"]
44
],
5-
"error-expected": "Found 1 errors",
5+
"error-expected": "found 1 errors",
66
"vendor-final": []
7-
}
7+
}

0 commit comments

Comments
 (0)