]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/mvs: indicate the actual version when printing a mismatched ModuleError
authorBryan C. Mills <bcmills@google.com>
Wed, 5 Aug 2020 03:53:01 +0000 (23:53 -0400)
committerBryan C. Mills <bcmills@google.com>
Mon, 24 Aug 2020 20:33:41 +0000 (20:33 +0000)
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 <jayconrod@google.com>
src/cmd/go/internal/mvs/errors.go
src/cmd/go/testdata/script/mod_load_replace_mismatch.txt
src/cmd/go/testdata/script/mod_replace_gopkgin.txt

index 85779028789b0faf25bb1e8541fd6efe58d25993..5564965fb51675a33dfd7c303efbacd1009fecda 100644 (file)
@@ -78,16 +78,21 @@ func (e *BuildListError) Error() string {
                b.WriteString(e.Err.Error())
        } else {
                for _, elem := range stack[:len(stack)-1] {
-                       fmt.Fprintf(b, "%s@%s %s\n\t", elem.m.Path, elem.m.Version, elem.nextReason)
+                       fmt.Fprintf(b, "%s %s\n\t", elem.m, elem.nextReason)
                }
                // Ensure that the final module path and version are included as part of the
                // error message.
                m := stack[len(stack)-1].m
-               if _, ok := e.Err.(*module.ModuleError); ok {
-                       // TODO(bcmills): Also ensure that the module path and version match.
-                       // (Otherwise, we may be reporting an error from a replacement without
-                       // indicating the replacement path.)
-                       fmt.Fprintf(b, "%v", e.Err)
+               if mErr, ok := e.Err.(*module.ModuleError); ok {
+                       actual := module.Version{Path: mErr.Path, Version: mErr.Version}
+                       if v, ok := mErr.Err.(*module.InvalidVersionError); ok {
+                               actual.Version = v.Version
+                       }
+                       if actual == m {
+                               fmt.Fprintf(b, "%v", e.Err)
+                       } else {
+                               fmt.Fprintf(b, "%s (replaced by %s): %v", m, actual, mErr.Err)
+                       }
                } else {
                        fmt.Fprintf(b, "%v", module.VersionError(m, e.Err))
                }
index 74dbb34b8a79114854f6506f82b0689b4141584e..067e209b01825f060ffc4c1505c0a7106628434d 100644 (file)
@@ -18,6 +18,6 @@ package use
 import _ "rsc.io/quote"
 
 -- want --
-go: example.com/quote@v1.5.2: parsing go.mod:
+go: rsc.io/quote@v1.5.2 (replaced by example.com/quote@v1.5.2): parsing go.mod:
        module declares its path as: rsc.io/Quote
                but was required as: rsc.io/quote
index 28c1196284d9a5e2264a7125425d7248badcad9f..674c99cb0cdeba95d276b21771e00d20b39d81a1 100644 (file)
@@ -34,7 +34,7 @@ go list -m gopkg.in/src-d/go-git.v4
 # A mismatched gopkg.in path should not be able to replace a different major version.
 cd ../3-to-gomod-4
 ! go list -m gopkg.in/src-d/go-git.v3
-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$'
+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$'
 
 -- 4-to-4/go.mod --
 module golang.org/issue/34254