Skip to content

Commit 6192b98

Browse files
author
Jay Conrod
committed
cmd/go: make hints in error messages more consistent
* All commands the user can run to fix the problem now appear alone on a separate line after a tab. * Removed -d from 'go get' commands. * Replaced 'go mod tidy' with 'go mod download $modpath' when a package might be provided by a module missing a sum. * Errors about 'path@version' syntax are more explicit. Fixes #29415 Fixes #42087 Fixes #43430 Fixes #43523 Change-Id: I4427c2c4506a727a2c727d652fd2d506bb134d3b Reviewed-on: https://go-review.googlesource.com/c/go/+/282121 Trust: Jay Conrod <[email protected]> Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 25886cf commit 6192b98

17 files changed

+42
-36
lines changed

src/cmd/go/go_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2655,12 +2655,12 @@ func TestBadCommandLines(t *testing.T) {
26552655
tg.tempFile("src/@x/x.go", "package x\n")
26562656
tg.setenv("GOPATH", tg.path("."))
26572657
tg.runFail("build", "@x")
2658-
tg.grepStderr("invalid input directory name \"@x\"|cannot use path@version syntax", "did not reject @x directory")
2658+
tg.grepStderr("invalid input directory name \"@x\"|can only use path@version syntax with 'go get' and 'go install' in module-aware mode", "did not reject @x directory")
26592659

26602660
tg.tempFile("src/@x/y/y.go", "package y\n")
26612661
tg.setenv("GOPATH", tg.path("."))
26622662
tg.runFail("build", "@x/y")
2663-
tg.grepStderr("invalid import path \"@x/y\"|cannot use path@version syntax", "did not reject @x/y import path")
2663+
tg.grepStderr("invalid import path \"@x/y\"|can only use path@version syntax with 'go get' and 'go install' in module-aware mode", "did not reject @x/y import path")
26642664

26652665
tg.tempFile("src/-x/x.go", "package x\n")
26662666
tg.setenv("GOPATH", tg.path("."))

src/cmd/go/internal/get/get.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
202202
func downloadPaths(patterns []string) []string {
203203
for _, arg := range patterns {
204204
if strings.Contains(arg, "@") {
205-
base.Fatalf("go: cannot use path@version syntax in GOPATH mode")
205+
base.Fatalf("go: can only use path@version syntax with 'go get' and 'go install' in module-aware mode")
206206
continue
207207
}
208208

src/cmd/go/internal/load/pkg.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -769,11 +769,7 @@ func loadPackageData(path, parentPath, parentDir, parentRoot string, parentIsStd
769769
}
770770

771771
if strings.Contains(path, "@") {
772-
if cfg.ModulesEnabled {
773-
return nil, false, errors.New("can only use path@version syntax with 'go get'")
774-
} else {
775-
return nil, false, errors.New("cannot use path@version syntax in GOPATH mode")
776-
}
772+
return nil, false, errors.New("can only use path@version syntax with 'go get' and 'go install' in module-aware mode")
777773
}
778774

779775
// Determine canonical package path and directory.

src/cmd/go/internal/modget/get.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ func (r *resolver) checkPackagesAndRetractions(ctx context.Context, pkgPatterns
15581558
}
15591559
}
15601560
if retractPath != "" {
1561-
fmt.Fprintf(os.Stderr, "go: run 'go get %s@latest' to switch to the latest unretracted version\n", retractPath)
1561+
fmt.Fprintf(os.Stderr, "go: to switch to the latest unretracted version, run:\n\tgo get %s@latest", retractPath)
15621562
}
15631563
}
15641564

src/cmd/go/internal/modload/import.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func (e *AmbiguousImportError) Error() string {
134134
// for its .zip file.
135135
type ImportMissingSumError struct {
136136
importPath string
137+
modPaths []string
137138
found, inAll bool
138139
}
139140

@@ -145,7 +146,7 @@ func (e *ImportMissingSumError) Error() string {
145146
message = fmt.Sprintf("missing go.sum entry for module providing package %s", e.importPath)
146147
}
147148
if e.inAll {
148-
return message + "; to add it:\n\tgo mod tidy"
149+
return message + fmt.Sprintf("; to add it:\n\tgo mod download %s", strings.Join(e.modPaths, " "))
149150
}
150151
return message
151152
}
@@ -238,7 +239,7 @@ func importFromBuildList(ctx context.Context, path string, buildList []module.Ve
238239
// Check each module on the build list.
239240
var dirs []string
240241
var mods []module.Version
241-
haveSumErr := false
242+
var sumErrModPaths []string
242243
for _, m := range buildList {
243244
if !maybeInModule(path, m.Path) {
244245
// Avoid possibly downloading irrelevant modules.
@@ -251,8 +252,9 @@ func importFromBuildList(ctx context.Context, path string, buildList []module.Ve
251252
// We are missing a sum needed to fetch a module in the build list.
252253
// We can't verify that the package is unique, and we may not find
253254
// the package at all. Keep checking other modules to decide which
254-
// error to report.
255-
haveSumErr = true
255+
// error to report. Multiple sums may be missing if we need to look in
256+
// multiple nested modules to resolve the import; we'll report them all.
257+
sumErrModPaths = append(sumErrModPaths, m.Path)
256258
continue
257259
}
258260
// Report fetch error.
@@ -273,8 +275,8 @@ func importFromBuildList(ctx context.Context, path string, buildList []module.Ve
273275
if len(mods) > 1 {
274276
return module.Version{}, "", &AmbiguousImportError{importPath: path, Dirs: dirs, Modules: mods}
275277
}
276-
if haveSumErr {
277-
return module.Version{}, "", &ImportMissingSumError{importPath: path, found: len(mods) > 0}
278+
if len(sumErrModPaths) > 0 {
279+
return module.Version{}, "", &ImportMissingSumError{importPath: path, modPaths: sumErrModPaths, found: len(mods) > 0}
278280
}
279281
if len(mods) == 1 {
280282
return mods[0], dirs[0], nil

src/cmd/go/internal/modload/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func LoadModFile(ctx context.Context) {
380380

381381
if f.Module == nil {
382382
// No module declaration. Must add module path.
383-
base.Fatalf("go: no module declaration in go.mod.\n\tRun 'go mod edit -module=example.com/mod' to specify the module path.")
383+
base.Fatalf("go: no module declaration in go.mod. To specify the module path:\n\tgo mod edit -module=example.com/mod")
384384
}
385385

386386
if err := checkModulePathLax(f.Module.Mod.Path); err != nil {

src/cmd/go/internal/modload/load.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ func loadFromRoots(params loaderParams) *loader {
868868
// base.Errorf. Ideally, 'go list' should not fail because of this,
869869
// but today, LoadPackages calls WriteGoMod unconditionally, which
870870
// would fail with a less clear message.
871-
base.Errorf("go: %[1]s: package %[2]s imported from implicitly required module; try 'go get -d %[1]s' to add missing requirements", pkg.path, dep.path)
871+
base.Errorf("go: %[1]s: package %[2]s imported from implicitly required module; to add missing requirements, run:\n\tgo get %[2]s@%[3]s", pkg.path, dep.path, dep.mod.Version)
872872
}
873873
ld.direct[dep.mod.Path] = true
874874
}

src/cmd/go/internal/modload/vendor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,6 @@ func checkVendorConsistency() {
214214
}
215215

216216
if vendErrors.Len() > 0 {
217-
base.Fatalf("go: inconsistent vendoring in %s:%s\n\nrun 'go mod vendor' to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory", modRoot, vendErrors)
217+
base.Fatalf("go: inconsistent vendoring in %s:%s\n\n\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor", modRoot, vendErrors)
218218
}
219219
}

src/cmd/go/internal/test/testflag.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func testFlags(args []string) (packageNames, passToTest []string) {
325325
if !testC {
326326
buildFlag = "-i"
327327
}
328-
fmt.Fprintf(os.Stderr, "flag %s is not a 'go test' flag (unknown flags cannot be used with %s)\n", firstUnknownFlag, buildFlag)
328+
fmt.Fprintf(os.Stderr, "go test: unknown flag %s cannot be used with %s\n", firstUnknownFlag, buildFlag)
329329
exitWithUsage()
330330
}
331331

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ cp go.mod.orig go.mod
66
go list -m indirect-with-pkg
77
stdout '^indirect-with-pkg v1.0.0 => ./indirect-with-pkg$'
88
! go list ./use-indirect
9-
stderr '^go: m/use-indirect: package indirect-with-pkg imported from implicitly required module; try ''go get -d m/use-indirect'' to add missing requirements$'
9+
stderr '^go: m/use-indirect: package indirect-with-pkg imported from implicitly required module; to add missing requirements, run:\n\tgo get [email protected]$'
1010

11-
# We can promote the implicit requirement by getting the importing package,
12-
# as hinted.
11+
# We can promote the implicit requirement by getting the importing package.
12+
# NOTE: the hint recommends getting the imported package (tested below) since
13+
# it's more obvious and doesn't require -d. However, that adds an '// indirect'
14+
# comment on the requirement.
1315
go get -d m/use-indirect
1416
cmp go.mod go.mod.use
1517
cp go.mod.orig go.mod
1618

1719
# We can also promote implicit requirements using 'go get' on them, or their
1820
# packages. This gives us "// indirect" requirements, since 'go get' doesn't
1921
# know they're needed by the main module. See #43131 for the rationale.
22+
# The hint above recommends this because it's more obvious usage and doesn't
23+
# require the -d flag.
2024
go get -d indirect-with-pkg indirect-without-pkg
2125
cmp go.mod go.mod.indirect
2226

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ cp go.mod.orig go.mod
1111
go mod edit -require example.com/retract/self/[email protected]
1212
go get -d example.com/retract/self/prev
1313
stderr '^go: warning: example.com/retract/self/[email protected]: retracted by module author: self$'
14-
stderr '^go: run ''go get example.com/retract/self/prev@latest'' to switch to the latest unretracted version$'
14+
stderr '^go: to switch to the latest unretracted version, run:\n\tgo get example.com/retract/self/prev@latest$'
1515
go list -m example.com/retract/self/prev
1616
stdout '^example.com/retract/self/prev v1.9.0$'
1717

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Test that go list fails on a go.mod with no module declaration.
44
cd $WORK/gopath/src/mod
55
! go list .
6-
stderr '^go: no module declaration in go.mod.\n\tRun ''go mod edit -module=example.com/mod'' to specify the module path.$'
6+
stderr '^go: no module declaration in go.mod. To specify the module path:\n\tgo mod edit -module=example.com/mod$'
77

88
# Test that go mod init in GOPATH doesn't add a module declaration
99
# with a path that can't possibly be a module path, because

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ cp go.sum.a-only go.sum
2525
! go list example.com/ambiguous/a/b
2626
stderr '^missing go.sum entry needed to verify package example.com/ambiguous/a/b is provided by exactly one module$'
2727
! go list -deps .
28-
stderr '^use.go:3:8: missing go.sum entry needed to verify package example.com/ambiguous/a/b is provided by exactly one module; to add it:\n\tgo mod tidy$'
28+
stderr '^use.go:3:8: missing go.sum entry needed to verify package example.com/ambiguous/a/b is provided by exactly one module; to add it:\n\tgo mod download example.com/ambiguous/a/b$'
2929

3030
cp go.sum.b-only go.sum
3131
! go list example.com/ambiguous/a/b
3232
stderr '^missing go.sum entry for module providing package example.com/ambiguous/a/b$'
3333
! go list -deps .
34-
stderr '^use.go:3:8: missing go.sum entry for module providing package example.com/ambiguous/a/b; to add it:\n\tgo mod tidy$'
34+
stderr '^use.go:3:8: missing go.sum entry for module providing package example.com/ambiguous/a/b; to add it:\n\tgo mod download example.com/ambiguous/a$'
35+
36+
cp go.sum.buildlist-only go.sum
37+
! go list -deps .
38+
stderr '^use.go:3:8: missing go.sum entry for module providing package example.com/ambiguous/a/b; to add it:\n\tgo mod download example.com/ambiguous/a example.com/ambiguous/a/b$'
3539

3640
-- go.mod --
3741
module m

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ stderr '^missing go.sum entry for module providing package rsc.io/quote$'
4747
# a package that imports it without that error.
4848
go list -e -deps -f '{{.ImportPath}}{{with .Error}} {{.Err}}{{end}}' .
4949
stdout '^m$'
50-
stdout '^rsc.io/quote missing go.sum entry for module providing package rsc.io/quote; to add it:\n\tgo mod tidy$'
50+
stdout '^rsc.io/quote missing go.sum entry for module providing package rsc.io/quote; to add it:\n\tgo mod download rsc.io/quote$'
5151
! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.zip
5252

5353
# go.sum should not have been written.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
6666
stderr '^\texample.com/[email protected]: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt'
6767
stderr '^\texample.com/unused: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
6868
stderr '^\texample.com/[email protected]: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
69-
stderr '\n\nrun .go mod vendor. to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory$'
69+
stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
7070

7171
# Module-specific subcommands should continue to load the full module graph.
7272
go mod graph
@@ -135,7 +135,7 @@ stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
135135
stderr '^\texample.com/[email protected]: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt'
136136
stderr '^\texample.com/unused: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
137137
stderr '^\texample.com/[email protected]: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
138-
stderr '\n\nrun .go mod vendor. to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory$'
138+
stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
139139

140140
# If -mod=vendor is set, limited consistency checks should apply even when
141141
# the go version is 1.13 or earlier.
@@ -151,7 +151,7 @@ cp $WORK/modules-bad-1.13.txt vendor/modules.txt
151151
! go list -mod=vendor -f {{.Dir}} -tags tools all
152152
stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
153153
stderr '^\texample.com/[email protected]: is explicitly required in go.mod, but vendor/modules.txt indicates example.com/[email protected]$'
154-
stderr '\n\nrun .go mod vendor. to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory$'
154+
stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
155155

156156
# If the go version is still 1.13, 'go mod vendor' should write a
157157
# matching vendor/modules.txt containing the corrected 1.13 data.
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Test rejection of pkg@version in GOPATH mode.
22
env GO111MODULE=off
33
! go get rsc.io/[email protected]
4-
stderr 'cannot use path@version syntax in GOPATH mode'
4+
stderr '^go: can only use path@version syntax with ''go get'' and ''go install'' in module-aware mode$'
55
! go build rsc.io/[email protected]
6-
stderr 'cannot use path@version syntax in GOPATH mode'
6+
stderr '^package rsc.io/[email protected]: can only use path@version syntax with ''go get'' and ''go install'' in module-aware mode$'
77

88
env GO111MODULE=on
99
cd x
1010
! go build rsc.io/[email protected]
11-
stderr 'can only use path@version syntax with ''go get'''
11+
stderr '^package rsc.io/[email protected]: can only use path@version syntax with ''go get'' and ''go install'' in module-aware mode$'
1212

1313
-- x/go.mod --
1414
module x

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ go test -count=1 -custom -args -v=7
99
# However, it should be an error to use custom flags when -i or -c are used,
1010
# since we know for sure that no test binary will run at all.
1111
! go test -i -custom
12-
stderr '^flag -custom is not a ''go test'' flag \(unknown flags cannot be used with -i\)$'
12+
stderr '^go test: unknown flag -custom cannot be used with -i$'
1313
! go test -c -custom
14-
stderr '^flag -custom is not a ''go test'' flag \(unknown flags cannot be used with -c\)$'
14+
stderr '^go test: unknown flag -custom cannot be used with -c$'
1515

1616
# The same should apply even if -c or -i come after a custom flag.
1717
! go test -custom -c
18-
stderr '^flag -custom is not a ''go test'' flag \(unknown flags cannot be used with -c\)$'
18+
stderr '^go test: unknown flag -custom cannot be used with -c$'
1919

2020
-- go.mod --
2121
module m

0 commit comments

Comments
 (0)