Skip to content

Commit c9658be

Browse files
oioojJay Conrod
authored and
Jay Conrod
committed
cmd/go: make module suggestion more friendly
We are trying to avoid by not automatically updating go.mod. The suggestion should be that users actually add the dependencies they need, and the command in an easily copy-pastable form now. Fixes: #43430 Change-Id: I2227dab498fcd8d66184c94ebe9e776629ccadfd Reviewed-on: https://go-review.googlesource.com/c/go/+/280713 Run-TryBot: Baokun Lee <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> Trust: Jay Conrod <[email protected]> Trust: Bryan C. Mills <[email protected]>
1 parent 4c668b2 commit c9658be

14 files changed

+31
-39
lines changed

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ type ImportMissingError struct {
3131
Module module.Version
3232
QueryErr error
3333

34-
// inAll indicates whether Path is in the "all" package pattern,
35-
// and thus would be added by 'go mod tidy'.
36-
inAll bool
37-
3834
// isStd indicates whether we would expect to find the package in the standard
3935
// library. This is normally true for all dotless import paths, but replace
4036
// directives can cause us to treat the replaced paths as also being in
@@ -67,16 +63,14 @@ func (e *ImportMissingError) Error() string {
6763
if !modfetch.IsZeroPseudoVersion(e.replaced.Version) {
6864
suggestArg = e.replaced.String()
6965
}
70-
return fmt.Sprintf("module %s provides package %s and is replaced but not required; try 'go get -d %s' to add it", e.replaced.Path, e.Path, suggestArg)
66+
return fmt.Sprintf("module %s provides package %s and is replaced but not required; to add it:\n\tgo get %s", e.replaced.Path, e.Path, suggestArg)
7167
}
7268

7369
suggestion := ""
7470
if !HasModRoot() {
7571
suggestion = ": working directory is not part of a module"
76-
} else if e.inAll {
77-
suggestion = "; try 'go mod tidy' to add it"
7872
} else {
79-
suggestion = fmt.Sprintf("; try 'go get -d %s' to add it", e.Path)
73+
suggestion = fmt.Sprintf("; to add it:\n\tgo get %s", e.Path)
8074
}
8175
return fmt.Sprintf("no required module provides package %s%s", e.Path, suggestion)
8276
}
@@ -151,7 +145,7 @@ func (e *ImportMissingSumError) Error() string {
151145
message = fmt.Sprintf("missing go.sum entry for module providing package %s", e.importPath)
152146
}
153147
if e.inAll {
154-
return message + "; try 'go mod tidy' to add it"
148+
return message + "; to add it:\n\tgo mod tidy"
155149
}
156150
return message
157151
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ func CreateModFile(ctx context.Context, modPath string) {
458458
}
459459
}
460460
if !empty {
461-
fmt.Fprintf(os.Stderr, "go: run 'go mod tidy' to add module requirements and sums\n")
461+
fmt.Fprintf(os.Stderr, "go: to add module requirements and sums:\n\tgo mod tidy\n")
462462
}
463463
}
464464

@@ -907,7 +907,7 @@ func WriteGoMod() {
907907
} else if cfg.BuildModReason != "" {
908908
base.Fatalf("go: updates to go.mod needed, disabled by -mod=readonly\n\t(%s)", cfg.BuildModReason)
909909
} else {
910-
base.Fatalf("go: updates to go.mod needed; try 'go mod tidy' first")
910+
base.Fatalf("go: updates to go.mod needed; to update it:\n\tgo mod tidy")
911911
}
912912
}
913913

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
281281
for _, pkg := range loaded.pkgs {
282282
if pkg.err != nil {
283283
if pkg.flags.has(pkgInAll) {
284-
if imErr := (*ImportMissingError)(nil); errors.As(pkg.err, &imErr) {
285-
imErr.inAll = true
286-
} else if sumErr := (*ImportMissingSumError)(nil); errors.As(pkg.err, &sumErr) {
284+
if sumErr := (*ImportMissingSumError)(nil); errors.As(pkg.err, &sumErr) {
287285
sumErr.inAll = true
288286
}
289287
}

src/cmd/go/internal/modload/modfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ func goModSummary(m module.Version) (*modFileSummary, error) {
449449
if HasModRoot() && cfg.BuildMod == "readonly" && actual.Version != "" {
450450
key := module.Version{Path: actual.Path, Version: actual.Version + "/go.mod"}
451451
if !modfetch.HaveSum(key) {
452-
suggestion := fmt.Sprintf("; try 'go mod download %s' to add it", m.Path)
452+
suggestion := fmt.Sprintf("; to add it:\n\tgo mod download %s", m.Path)
453453
return nil, module.VersionError(actual, &sumMissingError{suggestion: suggestion})
454454
}
455455
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ stderr 'malformed module path "x/y.z": missing dot in first path element'
1919
! go build ./useappengine
2020
stderr '^useappengine[/\\]x.go:2:8: cannot find package$'
2121
! go build ./usenonexistent
22-
stderr '^usenonexistent[/\\]x.go:2:8: no required module provides package nonexistent.rsc.io; try ''go mod tidy'' to add it$'
22+
stderr '^usenonexistent[/\\]x.go:2:8: no required module provides package nonexistent.rsc.io; to add it:\n\tgo get nonexistent.rsc.io$'
2323

2424

2525
# 'get -d' should be similarly definitive

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ stderr '^go get: malformed module path "example": missing dot in first path elem
8787
go mod edit -replace [email protected]=./example
8888

8989
! go list example
90-
stderr '^module example provides package example and is replaced but not required; try ''go get -d [email protected]'' to add it$'
90+
stderr '^module example provides package example and is replaced but not required; to add it:\n\tgo get [email protected]$'
9191

9292
go get -d example
9393
go list -m example

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ exec $WORK/testimport$GOEXE other/x/y/z/w .
1919
stdout w2.go
2020

2121
! exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
22-
stderr 'no required module provides package gobuild.example.com/x/y/z/w; try ''go get -d gobuild.example.com/x/y/z/w'' to add it'
22+
stderr 'no required module provides package gobuild.example.com/x/y/z/w; to add it:\n\tgo get gobuild.example.com/x/y/z/w'
2323

2424
cd z
2525
exec $WORK/testimport$GOEXE other/x/y/z/w .

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ cd ..
88
# 'go mod init' should recommend 'go mod tidy' if the directory has a .go file.
99
cd pkginroot
1010
go mod init m
11-
stderr '^go: run ''go mod tidy'' to add module requirements and sums$'
11+
stderr '^go: to add module requirements and sums:\n\tgo mod tidy$'
1212
cd ..
1313

1414
# 'go mod init' should recommend 'go mod tidy' if the directory has a
1515
# subdirectory. We don't walk the tree to see if it has .go files.
1616
cd subdir
1717
go mod init m
18-
stderr '^go: run ''go mod tidy'' to add module requirements and sums$'
18+
stderr '^go: to add module requirements and sums:\n\tgo mod tidy$'
1919
cd ..
2020

2121
-- empty/empty.txt --

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ env GO111MODULE=auto
1616
cd m
1717
cp go.mod go.mod.orig
1818
! go list -m all
19-
stderr '^go: example.com/[email protected]: missing go.sum entry; try ''go mod download example.com/cmd'' to add it$'
19+
stderr '^go: example.com/[email protected]: missing go.sum entry; to add it:\n\tgo mod download example.com/cmd$'
2020
go install example.com/cmd/a@latest
2121
cmp go.mod go.mod.orig
2222
exists $GOPATH/bin/a$GOEXE
@@ -67,9 +67,9 @@ cd tmp
6767
go mod init tmp
6868
go mod edit -require=rsc.io/[email protected]
6969
! go install -mod=readonly $GOPATH/pkg/mod/rsc.io/[email protected]
70-
stderr '^go: rsc.io/[email protected]: missing go.sum entry; try ''go mod download rsc.io/fortune'' to add it$'
70+
stderr '^go: rsc.io/[email protected]: missing go.sum entry; to add it:\n\tgo mod download rsc.io/fortune$'
7171
! go install -mod=readonly ../../pkg/mod/rsc.io/[email protected]
72-
stderr '^go: rsc.io/[email protected]: missing go.sum entry; try ''go mod download rsc.io/fortune'' to add it$'
72+
stderr '^go: rsc.io/[email protected]: missing go.sum entry; to add it:\n\tgo mod download rsc.io/fortune$'
7373
go get -d rsc.io/[email protected]
7474
go install -mod=readonly $GOPATH/pkg/mod/rsc.io/[email protected]
7575
exists $GOPATH/bin/fortune$GOEXE

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ stdout example.com/notfound
3939

4040
# Listing the missing dependency directly should fail outright...
4141
! go list -f '{{if .Error}}error{{end}} {{if .Incomplete}}incomplete{{end}}' example.com/notfound
42-
stderr 'no required module provides package example.com/notfound; try ''go get -d example.com/notfound'' to add it'
42+
stderr 'no required module provides package example.com/notfound; to add it:\n\tgo get example.com/notfound'
4343
! stdout error
4444
! stdout incomplete
4545

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cmp go.mod go.mod.empty
1313
# -mod=readonly should be set by default.
1414
env GOFLAGS=
1515
! go list all
16-
stderr '^x.go:2:8: no required module provides package rsc\.io/quote; try ''go mod tidy'' to add it$'
16+
stderr '^x.go:2:8: no required module provides package rsc\.io/quote; to add it:\n\tgo get rsc\.io/quote$'
1717
cmp go.mod go.mod.empty
1818

1919
env GOFLAGS=-mod=readonly
@@ -51,7 +51,7 @@ cmp go.mod go.mod.inconsistent
5151
# We get a different message when -mod=readonly is used by default.
5252
env GOFLAGS=
5353
! go list
54-
stderr '^go: updates to go.mod needed; try ''go mod tidy'' first$'
54+
stderr '^go: updates to go.mod needed; to update it:\n\tgo mod tidy'
5555

5656
# However, it should not reject files missing a 'go' directive,
5757
# since that was not always required.
@@ -75,15 +75,15 @@ cmp go.mod go.mod.indirect
7575

7676
cp go.mod.untidy go.mod
7777
! go list all
78-
stderr '^x.go:2:8: no required module provides package rsc.io/quote; try ''go mod tidy'' to add it$'
78+
stderr '^x.go:2:8: no required module provides package rsc.io/quote; to add it:\n\tgo get rsc.io/quote$'
7979

8080
! go list -deps .
81-
stderr '^x.go:2:8: no required module provides package rsc.io/quote; try ''go mod tidy'' to add it$'
81+
stderr '^x.go:2:8: no required module provides package rsc.io/quote; to add it:\n\tgo get rsc.io/quote$'
8282

8383
# However, if we didn't see an import from the main module, we should suggest
8484
# 'go get -d' instead, because we don't know whether 'go mod tidy' would add it.
8585
! go list rsc.io/quote
86-
stderr '^no required module provides package rsc.io/quote; try ''go get -d rsc.io/quote'' to add it$'
86+
stderr '^no required module provides package rsc.io/quote; to add it:\n\tgo get rsc.io/quote$'
8787

8888

8989
-- go.mod --

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cp go.mod go.mod.orig
99
# can't in readonly mode, since its go.mod may alter the build list.
1010
go mod edit -replace rsc.io/quote=./quote
1111
! go list rsc.io/quote
12-
stderr '^module rsc.io/quote provides package rsc.io/quote and is replaced but not required; try ''go get -d rsc.io/quote'' to add it$'
12+
stderr '^module rsc.io/quote provides package rsc.io/quote and is replaced but not required; to add it:\n\tgo get rsc.io/quote$'
1313
go get -d rsc.io/quote
1414
cmp go.mod go.mod.latest
1515
go list rsc.io/quote
@@ -18,7 +18,7 @@ cp go.mod.orig go.mod
1818
# Same test with a specific version.
1919
go mod edit -replace rsc.io/[email protected]=./quote
2020
! go list rsc.io/quote
21-
stderr '^module rsc.io/quote provides package rsc.io/quote and is replaced but not required; try ''go get -d rsc.io/[email protected]'' to add it$'
21+
stderr '^module rsc.io/quote provides package rsc.io/quote and is replaced but not required; to add it:\n\tgo get rsc.io/[email protected]$'
2222
go get -d rsc.io/[email protected]
2323
cmp go.mod go.mod.specific
2424
go list rsc.io/quote
@@ -28,7 +28,7 @@ cp go.mod.orig go.mod
2828
go mod edit -replace rsc.io/[email protected]=./quote
2929
go mod edit -replace rsc.io/[email protected]=./quote
3030
! go list rsc.io/quote
31-
stderr '^module rsc.io/quote provides package rsc.io/quote and is replaced but not required; try ''go get -d rsc.io/[email protected]'' to add it$'
31+
stderr '^module rsc.io/quote provides package rsc.io/quote and is replaced but not required; to add it:\n\tgo get rsc.io/[email protected]$'
3232

3333
-- go.mod --
3434
module m

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ cp go.sum.a-only go.sum
1717
! go list example.com/ambiguous/a/b
1818
stderr '^missing go.sum entry needed to verify package example.com/ambiguous/a/b is provided by exactly one module$'
1919
! go list -deps .
20-
stderr '^use.go:3:8: missing go.sum entry needed to verify package example.com/ambiguous/a/b is provided by exactly one module; try ''go mod tidy'' to add it$'
20+
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$'
2121

2222
cp go.sum.b-only go.sum
2323
! go list example.com/ambiguous/a/b
2424
stderr '^missing go.sum entry for module providing package example.com/ambiguous/a/b$'
2525
! go list -deps .
26-
stderr '^use.go:3:8: missing go.sum entry for module providing package example.com/ambiguous/a/b; try ''go mod tidy'' to add it$'
26+
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$'
2727

2828
-- go.mod --
2929
module m

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ env GO111MODULE=on
44
# When a sum is needed to load the build list, we get an error for the
55
# specific module. The .mod file is not downloaded, and go.sum is not written.
66
! go list -m all
7-
stderr '^go: rsc.io/[email protected]: missing go.sum entry; try ''go mod download rsc.io/quote'' to add it$'
7+
stderr '^go: rsc.io/[email protected]: missing go.sum entry; to add it:\n\tgo mod download rsc.io/quote$'
88
! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.mod
99
! exists go.sum
1010

1111
# If go.sum exists but contains hashes from an algorithm we don't know about,
1212
# we should see the same error.
1313
cp go.sum.h2only go.sum
1414
! go list -m all
15-
stderr '^go: rsc.io/[email protected]: missing go.sum entry; try ''go mod download rsc.io/quote'' to add it$'
15+
stderr '^go: rsc.io/[email protected]: missing go.sum entry; to add it:\n\tgo mod download rsc.io/quote$'
1616
! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.mod
1717
cmp go.sum go.sum.h2only
1818
rm go.sum
@@ -21,7 +21,7 @@ rm go.sum
2121
cp go.mod go.mod.orig
2222
go mod edit -replace rsc.io/[email protected]=rsc.io/[email protected]
2323
! go list -m all
24-
stderr '^go: rsc.io/[email protected] \(replaced by rsc.io/[email protected]\): missing go.sum entry; try ''go mod download rsc.io/quote'' to add it$'
24+
stderr '^go: rsc.io/[email protected] \(replaced by rsc.io/[email protected]\): missing go.sum entry; to add it:\n\tgo mod download rsc.io/quote$'
2525
! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.1.mod
2626
! exists go.sum
2727
cp go.mod.orig go.mod
@@ -35,7 +35,7 @@ exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.mod
3535
# When a sum is needed to load a .mod file for a package outside the build list,
3636
# we get a generic missing import error.
3737
! go list example.com/doesnotexist
38-
stderr '^no required module provides package example.com/doesnotexist; try ''go get -d example.com/doesnotexist'' to add it$'
38+
stderr '^no required module provides package example.com/doesnotexist; to add it:\n\tgo get example.com/doesnotexist$'
3939

4040
# When a sum is needed to load a .zip file, we get a more specific error.
4141
# The .zip file is not downloaded.
@@ -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; try ''go mod tidy'' to add it$'
50+
stdout '^rsc.io/quote missing go.sum entry for module providing package rsc.io/quote; to add it:\n\tgo mod tidy$'
5151
! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.zip
5252

5353
# go.sum should not have been written.

0 commit comments

Comments
 (0)