Skip to content

Commit c777863

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/mvs: indicate the actual version when printing a mismatched ModuleError
Previously, we suppressed the module version annotation if the last error in the stack was a *module.ModuleError, regardless of its path. However, if the error is for a replacement module, that produces a confusing error message: the error is attributed to the last module in the error path, but actually originates in the replacement (which is not otherwise indicated). Now, we print both the original and the replacement modules when they differ, which may add some unfortunate redundancy in the output but at least doesn't drop the very relevant information about replacements. Fixes #35039 Change-Id: I631a7398033602b1bd5656150a4fad4945a87ade Reviewed-on: https://go-review.googlesource.com/c/go/+/247765 Reviewed-by: Jay Conrod <[email protected]>
1 parent 6a71817 commit c777863

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,21 @@ func (e *BuildListError) Error() string {
7878
b.WriteString(e.Err.Error())
7979
} else {
8080
for _, elem := range stack[:len(stack)-1] {
81-
fmt.Fprintf(b, "%s@%s %s\n\t", elem.m.Path, elem.m.Version, elem.nextReason)
81+
fmt.Fprintf(b, "%s %s\n\t", elem.m, elem.nextReason)
8282
}
8383
// Ensure that the final module path and version are included as part of the
8484
// error message.
8585
m := stack[len(stack)-1].m
86-
if _, ok := e.Err.(*module.ModuleError); ok {
87-
// TODO(bcmills): Also ensure that the module path and version match.
88-
// (Otherwise, we may be reporting an error from a replacement without
89-
// indicating the replacement path.)
90-
fmt.Fprintf(b, "%v", e.Err)
86+
if mErr, ok := e.Err.(*module.ModuleError); ok {
87+
actual := module.Version{Path: mErr.Path, Version: mErr.Version}
88+
if v, ok := mErr.Err.(*module.InvalidVersionError); ok {
89+
actual.Version = v.Version
90+
}
91+
if actual == m {
92+
fmt.Fprintf(b, "%v", e.Err)
93+
} else {
94+
fmt.Fprintf(b, "%s (replaced by %s): %v", m, actual, mErr.Err)
95+
}
9196
} else {
9297
fmt.Fprintf(b, "%v", module.VersionError(m, e.Err))
9398
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ package use
1818
import _ "rsc.io/quote"
1919

2020
-- want --
21-
go: example.com/[email protected]: parsing go.mod:
21+
go: rsc.io/[email protected] (replaced by example.com/[email protected]): parsing go.mod:
2222
module declares its path as: rsc.io/Quote
2323
but was required as: rsc.io/quote

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ go list -m gopkg.in/src-d/go-git.v4
3434
# A mismatched gopkg.in path should not be able to replace a different major version.
3535
cd ../3-to-gomod-4
3636
! go list -m gopkg.in/src-d/go-git.v3
37-
stderr '^go: gopkg\.in/src-d/go-git\.v3@v3.0.0-20190801152248-0d1a009cbb60: invalid version: go\.mod has non-\.\.\.\.v3 module path "gopkg\.in/src-d/go-git\.v4" at revision 0d1a009cbb60$'
37+
stderr '^go: gopkg\.in/src-d/go-git\.v3@v3\.2\.0 \(replaced by gopkg\.in/src-d/go-git\.v3@v3\.0\.0-20190801152248-0d1a009cbb60\): version "v3\.0\.0-20190801152248-0d1a009cbb60" invalid: go\.mod has non-\.\.\.\.v3 module path "gopkg\.in/src-d/go-git\.v4" at revision 0d1a009cbb60$'
3838

3939
-- 4-to-4/go.mod --
4040
module golang.org/issue/34254

0 commit comments

Comments
 (0)