aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJay Conrod <jayconrod@google.com>2019-10-08 15:16:41 -0400
committerJay Conrod <jayconrod@google.com>2019-10-09 23:03:40 +0000
commitaa09e751ff8e1adeebbd4dcd562e998b33d1b4fa (patch)
tree9326a527773793832fcd6b4886750b24ea7cab8f /src
parent99b9ee3e44e53b91200a9feb6f22b206580656b0 (diff)
downloadgo-aa09e751ff8e1adeebbd4dcd562e998b33d1b4fa.tar.xz
cmd/doc: show original import error when package cannot be found
Updates #34669 Change-Id: I8d0ee68885e804e131f42a512080486f9b25e9dd Reviewed-on: https://go-review.googlesource.com/c/go/+/199819 Run-TryBot: Jay Conrod <jayconrod@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/doc/main.go17
-rw-r--r--src/cmd/go/testdata/script/mod_doc.txt5
2 files changed, 19 insertions, 3 deletions
diff --git a/src/cmd/doc/main.go b/src/cmd/doc/main.go
index dd15e801fb..0f817b612b 100644
--- a/src/cmd/doc/main.go
+++ b/src/cmd/doc/main.go
@@ -231,8 +231,8 @@ func parseArgs(args []string) (pkg *build.Package, path, symbol string, more boo
// First, is it a complete package path as it is? If so, we are done.
// This avoids confusion over package paths that have other
// package paths as their prefix.
- pkg, err = build.Import(arg, wd, build.ImportComment)
- if err == nil {
+ pkg, importErr := build.Import(arg, wd, build.ImportComment)
+ if importErr == nil {
return pkg, arg, "", false
}
// Another disambiguator: If the symbol starts with an upper
@@ -286,7 +286,18 @@ func parseArgs(args []string) (pkg *build.Package, path, symbol string, more boo
}
// If it has a slash, we've failed.
if slash >= 0 {
- log.Fatalf("no such package %s", arg[0:period])
+ // build.Import should always include the path in its error message,
+ // and we should avoid repeating it. Unfortunately, build.Import doesn't
+ // return a structured error. That can't easily be fixed, since it
+ // invokes 'go list' and returns the error text from the loaded package.
+ // TODO(golang.org/issue/34750): load using golang.org/x/tools/go/packages
+ // instead of go/build.
+ importErrStr := importErr.Error()
+ if strings.Contains(importErrStr, arg[:period]) {
+ log.Fatal(importErrStr)
+ } else {
+ log.Fatalf("no such package %s: %s", arg[:period], importErrStr)
+ }
}
// Guess it's a symbol in the current directory.
return importDir(wd), "", arg, false
diff --git a/src/cmd/go/testdata/script/mod_doc.txt b/src/cmd/go/testdata/script/mod_doc.txt
index d7aa553c1d..047e9f46eb 100644
--- a/src/cmd/go/testdata/script/mod_doc.txt
+++ b/src/cmd/go/testdata/script/mod_doc.txt
@@ -36,6 +36,11 @@ go doc rsc.io/quote
! stdout 'Package quote is located in a GOPATH workspace.'
stdout 'Package quote collects pithy sayings.'
+# Check that a sensible error message is printed when a package is not found.
+env GOPROXY=off
+! go doc example.com/hello
+stderr '^doc: cannot find module providing package example.com/hello: module lookup disabled by GOPROXY=off$'
+
-- go.mod --
module x
require rsc.io/quote v1.5.2