1
- //go:build ignore
2
-
3
1
// Copyright 2020 The Go Authors. All rights reserved.
4
2
// Use of this source code is governed by a BSD-style
5
3
// license that can be found in the LICENSE file.
@@ -9,20 +7,19 @@ package mvs
9
7
import (
10
8
"fmt"
11
9
"strings"
12
-
13
- "golang.org/x/mod/module"
14
10
)
15
11
16
12
// BuildListError decorates an error that occurred gathering requirements
17
13
// while constructing a build list. BuildListError prints the chain
18
14
// of requirements to the module where the error occurred.
19
- type BuildListError struct {
15
+ type BuildListError [ V comparable ] struct {
20
16
Err error
21
- stack []buildListErrorElem
17
+ stack []buildListErrorElem [V ]
18
+ vs Versions [V ]
22
19
}
23
20
24
- type buildListErrorElem struct {
25
- m module. Version
21
+ type buildListErrorElem [ V comparable ] struct {
22
+ m V
26
23
27
24
// nextReason is the reason this module depends on the next module in the
28
25
// stack. Typically either "requires", or "updating to".
@@ -37,69 +34,61 @@ type buildListErrorElem struct {
37
34
// explicit upgrade or downgrade (as opposed to an existing requirement in a
38
35
// go.mod file). A nil isVersionChange function indicates that none of the path
39
36
// steps are due to explicit version changes.
40
- func NewBuildListError (err error , path []module. Version , isVersionChange func (from , to module. Version ) bool ) * BuildListError {
41
- stack := make ([]buildListErrorElem , 0 , len (path ))
37
+ func NewBuildListError [ V comparable ] (err error , path []V , vs Versions [ V ], isVersionChange func (from , to V ) bool ) * BuildListError [ V ] {
38
+ stack := make ([]buildListErrorElem [ V ] , 0 , len (path ))
42
39
for len (path ) > 1 {
43
40
reason := "requires"
44
41
if isVersionChange != nil && isVersionChange (path [0 ], path [1 ]) {
45
42
reason = "updating to"
46
43
}
47
- stack = append (stack , buildListErrorElem {
44
+ stack = append (stack , buildListErrorElem [ V ] {
48
45
m : path [0 ],
49
46
nextReason : reason ,
50
47
})
51
48
path = path [1 :]
52
49
}
53
- stack = append (stack , buildListErrorElem {m : path [0 ]})
50
+ stack = append (stack , buildListErrorElem [ V ] {m : path [0 ]})
54
51
55
- return & BuildListError {
52
+ return & BuildListError [ V ] {
56
53
Err : err ,
57
54
stack : stack ,
55
+ vs : vs ,
58
56
}
59
57
}
60
58
61
59
// Module returns the module where the error occurred. If the module stack
62
60
// is empty, this returns a zero value.
63
- func (e * BuildListError ) Module () module. Version {
61
+ func (e * BuildListError [ V ] ) Module () V {
64
62
if len (e .stack ) == 0 {
65
- return module. Version {}
63
+ return * new ( V )
66
64
}
67
65
return e .stack [len (e .stack )- 1 ].m
68
66
}
69
67
70
- func (e * BuildListError ) Error () string {
68
+ func (e * BuildListError [ V ] ) Error () string {
71
69
b := & strings.Builder {}
72
70
stack := e .stack
73
71
74
72
// Don't print modules at the beginning of the chain without a
75
73
// version. These always seem to be the main module or a
76
74
// synthetic module ("target@").
77
- for len (stack ) > 0 && stack [0 ].m . Version == "" {
75
+ for len (stack ) > 0 && e . vs . Version ( stack [0 ].m ) == "" {
78
76
stack = stack [1 :]
79
77
}
80
78
81
79
if len (stack ) == 0 {
82
80
b .WriteString (e .Err .Error ())
83
81
} else {
84
82
for _ , elem := range stack [:len (stack )- 1 ] {
85
- fmt .Fprintf (b , "%s %s\n \t " , elem .m , elem .nextReason )
83
+ fmt .Fprintf (b , "%v %s\n \t " , elem .m , elem .nextReason )
86
84
}
87
- // Ensure that the final module path and version are included as part of the
88
- // error message.
89
85
m := stack [len (stack )- 1 ].m
90
- if mErr , ok := e .Err .(* module.ModuleError ); ok {
91
- actual := module.Version {Path : mErr .Path , Version : mErr .Version }
92
- if v , ok := mErr .Err .(* module.InvalidVersionError ); ok {
93
- actual .Version = v .Version
94
- }
95
- if actual == m {
96
- fmt .Fprintf (b , "%v" , e .Err )
97
- } else {
98
- fmt .Fprintf (b , "%s (replaced by %s): %v" , m , actual , mErr .Err )
99
- }
100
- } else {
101
- fmt .Fprintf (b , "%v" , module .VersionError (m , e .Err ))
102
- }
86
+ fmt .Fprintf (b , "%v: %v" , m , e .Err )
87
+ // TODO the original mvs code was careful to ensure that the final module path
88
+ // and version were included as part of the error message, but it did that
89
+ // by checking for mod/module-specific error types, but we don't want this
90
+ // package to depend on module. We could potentially do it by making those
91
+ // errors implement interface types defined in this package.
103
92
}
104
93
return b .String ()
105
94
}
0 commit comments