Skip to content

Commit 1dc0110

Browse files
author
Jay Conrod
committed
cmd/go: improve 'go mod download' and 'go list -m' error messages
modload.ListModules now wraps errors as module.ModuleError as appropriate. The resulting errors always include the module path and will include the version, if known. 'go mod download' no longer ignores errors reported by ListModules. Previously, it started requesting module info, go.mod, and zip. Those requests would fail, overwriting the original failure. They were usually less descriptive. 'go mod download' with a module not in the build list (and no version query) is now an error. Previously, this was silently ignored. Fixes #30743 Change-Id: Icee8c1c6c5240de135a8b6ba42d6bbcdb757cdac Reviewed-on: https://go-review.googlesource.com/c/go/+/189323 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent c5178ef commit 1dc0110

File tree

6 files changed

+55
-22
lines changed

6 files changed

+55
-22
lines changed

src/cmd/go/internal/list/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func runList(cmd *base.Command, args []string) {
390390
if !*listE {
391391
for _, m := range mods {
392392
if m.Error != nil {
393-
base.Errorf("go list -m %s: %v", m.Path, m.Error.Err)
393+
base.Errorf("go list -m: %v", m.Error.Err)
394394
}
395395
}
396396
base.ExitIfErrors()

src/cmd/go/internal/modcmd/download.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,19 @@ func runDownload(cmd *base.Command, args []string) {
8989
if info.Replace != nil {
9090
info = info.Replace
9191
}
92-
if info.Version == "" {
92+
if info.Version == "" && info.Error == nil {
93+
// main module
9394
continue
9495
}
9596
m := &moduleJSON{
9697
Path: info.Path,
9798
Version: info.Version,
9899
}
99100
mods = append(mods, m)
101+
if info.Error != nil {
102+
m.Error = info.Error.Err
103+
continue
104+
}
100105
work.Add(m)
101106
}
102107

@@ -110,12 +115,17 @@ func runDownload(cmd *base.Command, args []string) {
110115
// downloading the modules.
111116
var latestArgs []string
112117
for _, m := range mods {
118+
if m.Error != "" {
119+
continue
120+
}
113121
latestArgs = append(latestArgs, m.Path+"@latest")
114122
}
115123

116-
for _, info := range modload.ListModules(latestArgs, listU, listVersions) {
117-
if info.Version != "" {
118-
latest[info.Path] = info.Version
124+
if len(latestArgs) > 0 {
125+
for _, info := range modload.ListModules(latestArgs, listU, listVersions) {
126+
if info.Version != "" {
127+
latest[info.Path] = info.Version
128+
}
119129
}
120130
}
121131
}
@@ -169,7 +179,7 @@ func runDownload(cmd *base.Command, args []string) {
169179
} else {
170180
for _, m := range mods {
171181
if m.Error != "" {
172-
base.Errorf("%s@%s: %s\n", m.Path, m.Version, m.Error)
182+
base.Errorf("%s", m.Error)
173183
}
174184
}
175185
base.ExitIfErrors()

src/cmd/go/internal/modload/list.go

+24-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package modload
66

77
import (
8+
"errors"
89
"fmt"
910
"os"
1011
"strings"
@@ -70,9 +71,7 @@ func listModules(args []string, listVersions bool) []*modinfo.ModulePublic {
7071
mods = append(mods, &modinfo.ModulePublic{
7172
Path: path,
7273
Version: vers,
73-
Error: &modinfo.ModuleError{
74-
Err: err.Error(),
75-
},
74+
Error: modinfoError(path, vers, err),
7675
})
7776
continue
7877
}
@@ -116,19 +115,15 @@ func listModules(args []string, listVersions bool) []*modinfo.ModulePublic {
116115
mods = append(mods, moduleInfo(module.Version{Path: arg, Version: info.Version}, false))
117116
} else {
118117
mods = append(mods, &modinfo.ModulePublic{
119-
Path: arg,
120-
Error: &modinfo.ModuleError{
121-
Err: err.Error(),
122-
},
118+
Path: arg,
119+
Error: modinfoError(arg, "", err),
123120
})
124121
}
125122
continue
126123
}
127124
mods = append(mods, &modinfo.ModulePublic{
128-
Path: arg,
129-
Error: &modinfo.ModuleError{
130-
Err: fmt.Sprintf("module %q is not a known dependency", arg),
131-
},
125+
Path: arg,
126+
Error: modinfoError(arg, "", errors.New("not a known dependency")),
132127
})
133128
} else {
134129
fmt.Fprintf(os.Stderr, "warning: pattern %q matched no module dependencies\n", arg)
@@ -138,3 +133,21 @@ func listModules(args []string, listVersions bool) []*modinfo.ModulePublic {
138133

139134
return mods
140135
}
136+
137+
// modinfoError wraps an error to create an error message in
138+
// modinfo.ModuleError with minimal redundancy.
139+
func modinfoError(path, vers string, err error) *modinfo.ModuleError {
140+
var nerr *NoMatchingVersionError
141+
var merr *module.ModuleError
142+
if errors.As(err, &nerr) {
143+
// NoMatchingVersionError contains the query, so we don't mention the
144+
// query again in ModuleError.
145+
err = &module.ModuleError{Path: path, Err: err}
146+
} else if !errors.As(err, &merr) {
147+
// If the error does not contain path and version, wrap it in a
148+
// module.ModuleError.
149+
err = &module.ModuleError{Path: path, Version: vers, Err: err}
150+
}
151+
152+
return &modinfo.ModuleError{Err: err.Error()}
153+
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.3-pre1.zip
8585
go mod download -json rsc.io/[email protected]
8686
exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.1.zip
8787

88+
# download reports errors encountered when locating modules
89+
! go mod download bad/path
90+
stderr '^module bad/path: not a known dependency$'
91+
! go mod download bad/path@latest
92+
stderr '^bad/path@latest: malformed module path "bad/path": missing dot in first path element$'
93+
! go mod download rsc.io/[email protected]
94+
stderr '^rsc.io/[email protected]: reading .*/v1.999.999.info: 404 Not Found$'
95+
! go mod download -json bad/path
96+
stdout '^\t"Error": "module bad/path: not a known dependency"'
97+
8898
# allow go mod download without go.mod
8999
env GO111MODULE=auto
90100
rm go.mod

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ go list rsc.io/quote/buggy
3434

3535
# rsc.io/quote/buggy should not be listable as a module
3636
go list -m -e -f '{{.Error.Err}}' nonexist rsc.io/quote/buggy
37-
stdout '^module "nonexist" is not a known dependency'
38-
stdout '^module "rsc.io/quote/buggy" is not a known dependency'
37+
stdout '^module nonexist: not a known dependency$'
38+
stdout '^module rsc.io/quote/buggy: not a known dependency$'
3939

4040
! go list -m nonexist rsc.io/quote/buggy
41-
stderr '^go list -m nonexist: module "nonexist" is not a known dependency'
42-
stderr '^go list -m rsc.io/quote/buggy: module "rsc.io/quote/buggy" is not a known dependency'
41+
stderr '^go list -m: module nonexist: not a known dependency'
42+
stderr '^go list -m: module rsc.io/quote/buggy: not a known dependency'
4343

4444
# Module loader does not interfere with list -e (golang.org/issue/24149).
4545
go list -e -f '{{.Error.Err}}' database

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ go list -m rsc.io/quote@<v1.5.4
2222
stdout 'rsc.io/quote v1.5.2$'
2323

2424
! go list -m rsc.io/quote@>v1.5.3
25-
stderr 'go list -m rsc.io/quote: no matching versions for query ">v1.5.3"'
25+
stderr 'go list -m: module rsc.io/quote: no matching versions for query ">v1.5.3"'
2626

2727
go list -m -e -f '{{.Error.Err}}' rsc.io/quote@>v1.5.3
2828
stdout 'no matching versions for query ">v1.5.3"'

0 commit comments

Comments
 (0)