]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: make module suggestion more friendly
authorBaokun Lee <bk@golangcn.org>
Thu, 31 Dec 2020 03:42:39 +0000 (11:42 +0800)
committerJay Conrod <jayconrod@google.com>
Wed, 6 Jan 2021 18:54:25 +0000 (18:54 +0000)
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 <bk@golangcn.org>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Trust: Jay Conrod <jayconrod@google.com>
Trust: Bryan C. Mills <bcmills@google.com>

14 files changed:
src/cmd/go/internal/modload/import.go
src/cmd/go/internal/modload/init.go
src/cmd/go/internal/modload/load.go
src/cmd/go/internal/modload/modfile.go
src/cmd/go/testdata/script/mod_bad_domain.txt
src/cmd/go/testdata/script/mod_get_replaced.txt
src/cmd/go/testdata/script/mod_gobuild_import.txt
src/cmd/go/testdata/script/mod_init_tidy.txt
src/cmd/go/testdata/script/mod_install_pkg_version.txt
src/cmd/go/testdata/script/mod_list_bad_import.txt
src/cmd/go/testdata/script/mod_readonly.txt
src/cmd/go/testdata/script/mod_replace_readonly.txt
src/cmd/go/testdata/script/mod_sum_ambiguous.txt
src/cmd/go/testdata/script/mod_sum_readonly.txt

index c16531e2f447286d35aca70d3b0d1ff63c8b343b..055878c528a9689aa017c5ca6de99e3def59cf52 100644 (file)
@@ -31,10 +31,6 @@ type ImportMissingError struct {
        Module   module.Version
        QueryErr error
 
-       // inAll indicates whether Path is in the "all" package pattern,
-       // and thus would be added by 'go mod tidy'.
-       inAll bool
-
        // isStd indicates whether we would expect to find the package in the standard
        // library. This is normally true for all dotless import paths, but replace
        // directives can cause us to treat the replaced paths as also being in
@@ -67,16 +63,14 @@ func (e *ImportMissingError) Error() string {
                        if !modfetch.IsZeroPseudoVersion(e.replaced.Version) {
                                suggestArg = e.replaced.String()
                        }
-                       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)
+                       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)
                }
 
                suggestion := ""
                if !HasModRoot() {
                        suggestion = ": working directory is not part of a module"
-               } else if e.inAll {
-                       suggestion = "; try 'go mod tidy' to add it"
                } else {
-                       suggestion = fmt.Sprintf("; try 'go get -d %s' to add it", e.Path)
+                       suggestion = fmt.Sprintf("; to add it:\n\tgo get %s", e.Path)
                }
                return fmt.Sprintf("no required module provides package %s%s", e.Path, suggestion)
        }
@@ -151,7 +145,7 @@ func (e *ImportMissingSumError) Error() string {
                message = fmt.Sprintf("missing go.sum entry for module providing package %s", e.importPath)
        }
        if e.inAll {
-               return message + "; try 'go mod tidy' to add it"
+               return message + "; to add it:\n\tgo mod tidy"
        }
        return message
 }
index b0acb7b25d99bb3048edc4c315ace40df4542e3a..348c8e66c9d79f330e124f104e761eed8c6ff4e9 100644 (file)
@@ -458,7 +458,7 @@ func CreateModFile(ctx context.Context, modPath string) {
                }
        }
        if !empty {
-               fmt.Fprintf(os.Stderr, "go: run 'go mod tidy' to add module requirements and sums\n")
+               fmt.Fprintf(os.Stderr, "go: to add module requirements and sums:\n\tgo mod tidy\n")
        }
 }
 
@@ -907,7 +907,7 @@ func WriteGoMod() {
                } else if cfg.BuildModReason != "" {
                        base.Fatalf("go: updates to go.mod needed, disabled by -mod=readonly\n\t(%s)", cfg.BuildModReason)
                } else {
-                       base.Fatalf("go: updates to go.mod needed; try 'go mod tidy' first")
+                       base.Fatalf("go: updates to go.mod needed; to update it:\n\tgo mod tidy")
                }
        }
 
index 9a8b0cf177abe0090dbd31929f70324d15d588f5..ae5b8ef6abd3386ca101ead354393d80794c4157 100644 (file)
@@ -281,9 +281,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
        for _, pkg := range loaded.pkgs {
                if pkg.err != nil {
                        if pkg.flags.has(pkgInAll) {
-                               if imErr := (*ImportMissingError)(nil); errors.As(pkg.err, &imErr) {
-                                       imErr.inAll = true
-                               } else if sumErr := (*ImportMissingSumError)(nil); errors.As(pkg.err, &sumErr) {
+                               if sumErr := (*ImportMissingSumError)(nil); errors.As(pkg.err, &sumErr) {
                                        sumErr.inAll = true
                                }
                        }
index d5a17236cdfa929f021ef6c9b4df79eed98212d9..c6667d0bf7944b2dcae558005363d648a05c4c5f 100644 (file)
@@ -449,7 +449,7 @@ func goModSummary(m module.Version) (*modFileSummary, error) {
        if HasModRoot() && cfg.BuildMod == "readonly" && actual.Version != "" {
                key := module.Version{Path: actual.Path, Version: actual.Version + "/go.mod"}
                if !modfetch.HaveSum(key) {
-                       suggestion := fmt.Sprintf("; try 'go mod download %s' to add it", m.Path)
+                       suggestion := fmt.Sprintf("; to add it:\n\tgo mod download %s", m.Path)
                        return nil, module.VersionError(actual, &sumMissingError{suggestion: suggestion})
                }
        }
index 20199c1c2ca8e8c61ef73af5df7d59aea17bd1da..7a270d0f07cac7fa76a670037dbb6678536db7d6 100644 (file)
@@ -19,7 +19,7 @@ stderr 'malformed module path "x/y.z": missing dot in first path element'
 ! go build ./useappengine
 stderr '^useappengine[/\\]x.go:2:8: cannot find package$'
 ! go build ./usenonexistent
-stderr '^usenonexistent[/\\]x.go:2:8: no required module provides package nonexistent.rsc.io; try ''go mod tidy'' to add it$'
+stderr '^usenonexistent[/\\]x.go:2:8: no required module provides package nonexistent.rsc.io; to add it:\n\tgo get nonexistent.rsc.io$'
 
 
 # 'get -d' should be similarly definitive
index 76d0793ffef7c48587ede6d584636b8b85a12089..d97f3f1a40117242fba478162505e0abb9d6eb97 100644 (file)
@@ -87,7 +87,7 @@ stderr '^go get: malformed module path "example": missing dot in first path elem
 go mod edit -replace example@v0.1.0=./example
 
 ! go list example
-stderr '^module example provides package example and is replaced but not required; try ''go get -d example@v0.1.0'' to add it$'
+stderr '^module example provides package example and is replaced but not required; to add it:\n\tgo get example@v0.1.0$'
 
 go get -d example
 go list -m example
index 3a133663ec78818b540361233a5ae58e9a3fe831..c13ae844b56739d1a265d3cc60b1ee39b2d926ac 100644 (file)
@@ -19,7 +19,7 @@ exec $WORK/testimport$GOEXE other/x/y/z/w .
 stdout w2.go
 
 ! exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
-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'
+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'
 
 cd z
 exec $WORK/testimport$GOEXE other/x/y/z/w .
index 6a37edd960a68f0a6aa4d0e33e9887cbf9562579..4a525903b23f11fc0f6cfb2c40af5042f818f41e 100644 (file)
@@ -8,14 +8,14 @@ cd ..
 # 'go mod init' should recommend 'go mod tidy' if the directory has a .go file.
 cd pkginroot
 go mod init m
-stderr '^go: run ''go mod tidy'' to add module requirements and sums$'
+stderr '^go: to add module requirements and sums:\n\tgo mod tidy$'
 cd ..
 
 # 'go mod init' should recommend 'go mod tidy' if the directory has a
 # subdirectory. We don't walk the tree to see if it has .go files.
 cd subdir
 go mod init m
-stderr '^go: run ''go mod tidy'' to add module requirements and sums$'
+stderr '^go: to add module requirements and sums:\n\tgo mod tidy$'
 cd ..
 
 -- empty/empty.txt --
index 93896d45933598ecbb02e4e5c31bc7f22b1f0bb1..e27ebc5cc5095d352329f5188066e1d954216f2a 100644 (file)
@@ -16,7 +16,7 @@ env GO111MODULE=auto
 cd m
 cp go.mod go.mod.orig
 ! go list -m all
-stderr '^go: example.com/cmd@v1.1.0-doesnotexist: missing go.sum entry; try ''go mod download example.com/cmd'' to add it$'
+stderr '^go: example.com/cmd@v1.1.0-doesnotexist: missing go.sum entry; to add it:\n\tgo mod download example.com/cmd$'
 go install example.com/cmd/a@latest
 cmp go.mod go.mod.orig
 exists $GOPATH/bin/a$GOEXE
@@ -67,9 +67,9 @@ cd tmp
 go mod init tmp
 go mod edit -require=rsc.io/fortune@v1.0.0
 ! go install -mod=readonly $GOPATH/pkg/mod/rsc.io/fortune@v1.0.0
-stderr '^go: rsc.io/fortune@v1.0.0: missing go.sum entry; try ''go mod download rsc.io/fortune'' to add it$'
+stderr '^go: rsc.io/fortune@v1.0.0: missing go.sum entry; to add it:\n\tgo mod download rsc.io/fortune$'
 ! go install -mod=readonly ../../pkg/mod/rsc.io/fortune@v1.0.0
-stderr '^go: rsc.io/fortune@v1.0.0: missing go.sum entry; try ''go mod download rsc.io/fortune'' to add it$'
+stderr '^go: rsc.io/fortune@v1.0.0: missing go.sum entry; to add it:\n\tgo mod download rsc.io/fortune$'
 go get -d rsc.io/fortune@v1.0.0
 go install -mod=readonly $GOPATH/pkg/mod/rsc.io/fortune@v1.0.0
 exists $GOPATH/bin/fortune$GOEXE
index 3cd50b0de2544450563ad81c92cfe6ced23bf0dd..b128408a612a4163ed58d9470e1f63a929b28063 100644 (file)
@@ -39,7 +39,7 @@ stdout example.com/notfound
 
 # Listing the missing dependency directly should fail outright...
 ! go list -f '{{if .Error}}error{{end}} {{if .Incomplete}}incomplete{{end}}' example.com/notfound
-stderr 'no required module provides package example.com/notfound; try ''go get -d example.com/notfound'' to add it'
+stderr 'no required module provides package example.com/notfound; to add it:\n\tgo get example.com/notfound'
 ! stdout error
 ! stdout incomplete
 
index ca8cd6e068938cb64fb372b85b11af1e7eaf8a2e..176be7296794a7ebf38b1be8c8f1188840ec2a80 100644 (file)
@@ -13,7 +13,7 @@ cmp go.mod go.mod.empty
 # -mod=readonly should be set by default.
 env GOFLAGS=
 ! go list all
-stderr '^x.go:2:8: no required module provides package rsc\.io/quote; try ''go mod tidy'' to add it$'
+stderr '^x.go:2:8: no required module provides package rsc\.io/quote; to add it:\n\tgo get rsc\.io/quote$'
 cmp go.mod go.mod.empty
 
 env GOFLAGS=-mod=readonly
@@ -51,7 +51,7 @@ cmp go.mod go.mod.inconsistent
 # We get a different message when -mod=readonly is used by default.
 env GOFLAGS=
 ! go list
-stderr '^go: updates to go.mod needed; try ''go mod tidy'' first$'
+stderr '^go: updates to go.mod needed; to update it:\n\tgo mod tidy'
 
 # However, it should not reject files missing a 'go' directive,
 # since that was not always required.
@@ -75,15 +75,15 @@ cmp go.mod go.mod.indirect
 
 cp go.mod.untidy go.mod
 ! go list all
-stderr '^x.go:2:8: no required module provides package rsc.io/quote; try ''go mod tidy'' to add it$'
+stderr '^x.go:2:8: no required module provides package rsc.io/quote; to add it:\n\tgo get rsc.io/quote$'
 
 ! go list -deps .
-stderr '^x.go:2:8: no required module provides package rsc.io/quote; try ''go mod tidy'' to add it$'
+stderr '^x.go:2:8: no required module provides package rsc.io/quote; to add it:\n\tgo get rsc.io/quote$'
 
 # However, if we didn't see an import from the main module, we should suggest
 # 'go get -d' instead, because we don't know whether 'go mod tidy' would add it.
 ! go list rsc.io/quote
-stderr '^no required module provides package rsc.io/quote; try ''go get -d rsc.io/quote'' to add it$'
+stderr '^no required module provides package rsc.io/quote; to add it:\n\tgo get rsc.io/quote$'
 
 
 -- go.mod --
index 882c755337a9180a1fbb64ca707c4af7b2b15ba9..d950d78bd3c621b7ebf429e33458214bb0f6cb30 100644 (file)
@@ -9,7 +9,7 @@ cp go.mod go.mod.orig
 # can't in readonly mode, since its go.mod may alter the build list.
 go mod edit -replace rsc.io/quote=./quote
 ! go list rsc.io/quote
-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$'
+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$'
 go get -d rsc.io/quote
 cmp go.mod go.mod.latest
 go list rsc.io/quote
@@ -18,7 +18,7 @@ cp go.mod.orig go.mod
 # Same test with a specific version.
 go mod edit -replace rsc.io/quote@v1.0.0-doesnotexist=./quote
 ! go list rsc.io/quote
-stderr '^module rsc.io/quote provides package rsc.io/quote and is replaced but not required; try ''go get -d rsc.io/quote@v1.0.0-doesnotexist'' to add it$'
+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@v1.0.0-doesnotexist$'
 go get -d rsc.io/quote@v1.0.0-doesnotexist
 cmp go.mod go.mod.specific
 go list rsc.io/quote
@@ -28,7 +28,7 @@ cp go.mod.orig go.mod
 go mod edit -replace rsc.io/quote@v1.0.0-doesnotexist=./quote
 go mod edit -replace rsc.io/quote@v1.1.0-doesnotexist=./quote
 ! go list rsc.io/quote
-stderr '^module rsc.io/quote provides package rsc.io/quote and is replaced but not required; try ''go get -d rsc.io/quote@v1.1.0-doesnotexist'' to add it$'
+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@v1.1.0-doesnotexist$'
 
 -- go.mod --
 module m
index 999257c419f8e0c28ec0ccff70f8071ec6fa7465..08107bf37ce64eb93748d8bb6842fd5860254d9b 100644 (file)
@@ -17,13 +17,13 @@ cp go.sum.a-only go.sum
 ! go list example.com/ambiguous/a/b
 stderr '^missing go.sum entry needed to verify package example.com/ambiguous/a/b is provided by exactly one module$'
 ! go list -deps .
-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$'
+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$'
 
 cp go.sum.b-only go.sum
 ! go list example.com/ambiguous/a/b
 stderr '^missing go.sum entry for module providing package example.com/ambiguous/a/b$'
 ! go list -deps .
-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$'
+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$'
 
 -- go.mod --
 module m
index 4d6e8aae6a9d77119aaca4f32ce7543b32a09b54..866f4c1ae45bf2235fc6fc393e1f5a27bb64d240 100644 (file)
@@ -4,7 +4,7 @@ env GO111MODULE=on
 # When a sum is needed to load the build list, we get an error for the
 # specific module. The .mod file is not downloaded, and go.sum is not written.
 ! go list -m all
-stderr '^go: rsc.io/quote@v1.5.2: missing go.sum entry; try ''go mod download rsc.io/quote'' to add it$'
+stderr '^go: rsc.io/quote@v1.5.2: missing go.sum entry; to add it:\n\tgo mod download rsc.io/quote$'
 ! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.mod
 ! exists go.sum
 
@@ -12,7 +12,7 @@ stderr '^go: rsc.io/quote@v1.5.2: missing go.sum entry; try ''go mod download rs
 # we should see the same error.
 cp go.sum.h2only go.sum
 ! go list -m all
-stderr '^go: rsc.io/quote@v1.5.2: missing go.sum entry; try ''go mod download rsc.io/quote'' to add it$'
+stderr '^go: rsc.io/quote@v1.5.2: missing go.sum entry; to add it:\n\tgo mod download rsc.io/quote$'
 ! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.mod
 cmp go.sum go.sum.h2only
 rm go.sum
@@ -21,7 +21,7 @@ rm go.sum
 cp go.mod go.mod.orig
 go mod edit -replace rsc.io/quote@v1.5.2=rsc.io/quote@v1.5.1
 ! go list -m all
-stderr '^go: rsc.io/quote@v1.5.2 \(replaced by rsc.io/quote@v1.5.1\): missing go.sum entry; try ''go mod download rsc.io/quote'' to add it$'
+stderr '^go: rsc.io/quote@v1.5.2 \(replaced by rsc.io/quote@v1.5.1\): missing go.sum entry; to add it:\n\tgo mod download rsc.io/quote$'
 ! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.1.mod
 ! exists go.sum
 cp go.mod.orig go.mod
@@ -35,7 +35,7 @@ exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.mod
 # When a sum is needed to load a .mod file for a package outside the build list,
 # we get a generic missing import error.
 ! go list example.com/doesnotexist
-stderr '^no required module provides package example.com/doesnotexist; try ''go get -d example.com/doesnotexist'' to add it$'
+stderr '^no required module provides package example.com/doesnotexist; to add it:\n\tgo get example.com/doesnotexist$'
 
 # When a sum is needed to load a .zip file, we get a more specific error.
 # The .zip file is not downloaded.
@@ -47,7 +47,7 @@ stderr '^missing go.sum entry for module providing package rsc.io/quote$'
 # a package that imports it without that error.
 go list -e -deps -f '{{.ImportPath}}{{with .Error}} {{.Err}}{{end}}' .
 stdout '^m$'
-stdout '^rsc.io/quote missing go.sum entry for module providing package rsc.io/quote; try ''go mod tidy'' to add it$'
+stdout '^rsc.io/quote missing go.sum entry for module providing package rsc.io/quote; to add it:\n\tgo mod tidy$'
 ! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.zip
 
 # go.sum should not have been written.