]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.16] cmd/go: remove hint when no module is suggested
authorConstantin Konstantinidis <constantinkonstantinidis@gmail.com>
Sat, 5 Jun 2021 05:48:30 +0000 (07:48 +0200)
committerCarlos Amedee <carlos@golang.org>
Mon, 2 Aug 2021 22:30:44 +0000 (22:30 +0000)
Updates #46528
Fixes #46551

Change-Id: I2453d321ece878ea7823865758aa4a16b3ed7fe8
Reviewed-on: https://go-review.googlesource.com/c/go/+/325430
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Trust: Heschi Kreinick <heschi@google.com>
Trust: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
(cherry picked from commit e552a6d31270c86064632af1d092e0db5a930250)
Reviewed-on: https://go-review.googlesource.com/c/go/+/334371
Trust: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
src/cmd/go/internal/modload/import.go
src/cmd/go/testdata/script/mod_install_hint.txt [new file with mode: 0644]

index 995641c9f1f36d8a65192f6448e0c6f18c502fe4..eda0568f07d6456e6159816d48e571d7919c85bb 100644 (file)
@@ -160,11 +160,13 @@ func (e *ImportMissingSumError) Error() string {
                // Importing package is unknown, or the missing package was named on the
                // command line. Recommend 'go mod download' for the modules that could
                // provide the package, since that shouldn't change go.mod.
-               args := make([]string, len(e.mods))
-               for i, mod := range e.mods {
-                       args[i] = mod.Path
+               if len(e.mods) > 0 {
+                       args := make([]string, len(e.mods))
+                       for i, mod := range e.mods {
+                               args[i] = mod.Path
+                       }
+                       hint = fmt.Sprintf("; to add:\n\tgo mod download %s", strings.Join(args, " "))
                }
-               hint = fmt.Sprintf("; to add:\n\tgo mod download %s", strings.Join(args, " "))
        } else {
                // Importing package is known (common case). Recommend 'go get' on the
                // current version of the importing package.
diff --git a/src/cmd/go/testdata/script/mod_install_hint.txt b/src/cmd/go/testdata/script/mod_install_hint.txt
new file mode 100644 (file)
index 0000000..ab02840
--- /dev/null
@@ -0,0 +1,5 @@
+# Module is replaced but not required. No hint appears as no module is suggested.\r
+go mod init m\r
+go mod edit -replace=github.com/notrequired@v0.5.0=github.com/doesnotexist@v0.5.0\r
+! go install github.com/notrequired\r
+! stderr 'to add it:'
\ No newline at end of file