Skip to content

Commit f7c9672

Browse files
author
Jay Conrod
committed
cmd/go: describe dependencies in build list error messages
mvs.BuildList reports errors with a chain of modules to make it clear why the module where the error occurred was part of the build. This is a little confusing with "go get -u" since there are edges in the module graph for requirements and for updates. With this change, we now print "requires" or "updates to" between each module version in the chain. Updates #30661 Change-Id: Ie689500ea86857e715b250b9e0cae0bc6686dc32 Reviewed-on: https://go-review.googlesource.com/c/go/+/171150 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 6997671 commit f7c9672

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

src/cmd/go/internal/mvs/mvs.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,41 @@ type Reqs interface {
6565
// while constructing a build list. BuildListError prints the chain
6666
// of requirements to the module where the error occurred.
6767
type BuildListError struct {
68-
Err error
69-
Stack []module.Version
68+
err error
69+
stack []buildListErrorElem
70+
}
71+
72+
type buildListErrorElem struct {
73+
m module.Version
74+
75+
// nextReason is the reason this module depends on the next module in the
76+
// stack. Typically either "requires", or "upgraded to".
77+
nextReason string
7078
}
7179

7280
func (e *BuildListError) Error() string {
7381
b := &strings.Builder{}
74-
errMsg := e.Err.Error()
75-
stack := e.Stack
82+
errMsg := e.err.Error()
83+
stack := e.stack
7684

7785
// Don't print modules at the beginning of the chain without a
7886
// version. These always seem to be the main module or a
7987
// synthetic module ("target@").
80-
for len(stack) > 0 && stack[len(stack)-1].Version == "" {
88+
for len(stack) > 0 && stack[len(stack)-1].m.Version == "" {
8189
stack = stack[:len(stack)-1]
8290
}
8391

8492
// Don't print the last module if the error message already
8593
// starts with module path and version.
86-
if len(stack) > 0 && strings.HasPrefix(errMsg, fmt.Sprintf("%s@%s: ", stack[0].Path, stack[0].Version)) {
87-
// error already mentions module
88-
stack = stack[1:]
94+
errMentionsLast := len(stack) > 0 && strings.HasPrefix(errMsg, fmt.Sprintf("%s@%s: ", stack[0].m.Path, stack[0].m.Version))
95+
for i := len(stack) - 1; i >= 1; i-- {
96+
fmt.Fprintf(b, "%s@%s %s\n\t", stack[i].m.Path, stack[i].m.Version, stack[i].nextReason)
8997
}
90-
91-
for i := len(stack) - 1; i >= 0; i-- {
92-
fmt.Fprintf(b, "%s@%s ->\n\t", stack[i].Path, stack[i].Version)
98+
if errMentionsLast || len(stack) == 0 {
99+
b.WriteString(errMsg)
100+
} else {
101+
fmt.Fprintf(b, "%s@%s: %s", stack[0].m.Path, stack[0].m.Version, errMsg)
93102
}
94-
b.WriteString(errMsg)
95103
return b.String()
96104
}
97105

@@ -168,9 +176,16 @@ func buildList(target module.Version, reqs Reqs, upgrade func(module.Version) mo
168176
q = q[1:]
169177

170178
if node.err != nil {
171-
err := &BuildListError{Err: node.err}
172-
for n := node; n != nil; n = neededBy[n] {
173-
err.Stack = append(err.Stack, n.m)
179+
err := &BuildListError{
180+
err: node.err,
181+
stack: []buildListErrorElem{{m: node.m}},
182+
}
183+
for n, prev := neededBy[node], node; n != nil; n, prev = neededBy[n], n {
184+
reason := "requires"
185+
if n.upgrade == prev.m {
186+
reason = "updating to"
187+
}
188+
err.stack = append(err.stack, buildListErrorElem{m: n.m, nextReason: reason})
174189
}
175190
return nil, err
176191
}

src/cmd/go/testdata/script/mod_load_badchain.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ go mod download example.com/badchain/[email protected]
88
go mod download example.com/badchain/[email protected]
99
go mod download example.com/badchain/[email protected]
1010

11-
# Try to upgrade example.com/badchain/a (and its dependencies).
11+
# Try to update example.com/badchain/a (and its dependencies).
1212
! go get -u example.com/badchain/a
13-
cmp stderr upgrade-a-expected
13+
cmp stderr update-a-expected
1414
cmp go.mod go.mod.orig
1515

16-
# Try to upgrade the main module. This upgrades everything, including
16+
# Try to update the main module. This updates everything, including
1717
# modules that aren't direct requirements, so the error stack is shorter.
1818
! go get -u
19-
cmp stderr upgrade-main-expected
19+
cmp stderr update-main-expected
2020
cmp go.mod go.mod.orig
2121

22-
# Upgrade manually. Listing modules should produce an error.
22+
# update manually. Listing modules should produce an error.
2323
go mod edit -require=example.com/badchain/[email protected]
2424
! go list -m
2525
cmp stderr list-expected
@@ -28,14 +28,14 @@ cmp stderr list-expected
2828
module m
2929

3030
require example.com/badchain/a v1.0.0
31-
-- upgrade-main-expected --
32-
go get: example.com/badchain/[email protected] ->
31+
-- update-main-expected --
32+
go get: example.com/badchain/[email protected] updating to
3333
example.com/badchain/[email protected]: parsing go.mod: unexpected module path "example.com/badchain/wrong"
34-
-- upgrade-a-expected --
35-
go get: example.com/badchain/[email protected] ->
36-
example.com/badchain/[email protected] ->
34+
-- update-a-expected --
35+
go get: example.com/badchain/[email protected] requires
36+
example.com/badchain/[email protected] requires
3737
example.com/badchain/[email protected]: parsing go.mod: unexpected module path "example.com/badchain/wrong"
3838
-- list-expected --
39-
go: example.com/badchain/[email protected] ->
40-
example.com/badchain/[email protected] ->
39+
go: example.com/badchain/[email protected] requires
40+
example.com/badchain/[email protected] requires
4141
example.com/badchain/[email protected]: parsing go.mod: unexpected module path "example.com/badchain/wrong"

0 commit comments

Comments
 (0)