Skip to content

Commit aa09e75

Browse files
author
Jay Conrod
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 99b9ee3 commit aa09e75

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/cmd/doc/main.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ func parseArgs(args []string) (pkg *build.Package, path, symbol string, more boo
231231
// First, is it a complete package path as it is? If so, we are done.
232232
// This avoids confusion over package paths that have other
233233
// package paths as their prefix.
234-
pkg, err = build.Import(arg, wd, build.ImportComment)
235-
if err == nil {
234+
pkg, importErr := build.Import(arg, wd, build.ImportComment)
235+
if importErr == nil {
236236
return pkg, arg, "", false
237237
}
238238
// 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
286286
}
287287
// If it has a slash, we've failed.
288288
if slash >= 0 {
289-
log.Fatalf("no such package %s", arg[0:period])
289+
// build.Import should always include the path in its error message,
290+
// and we should avoid repeating it. Unfortunately, build.Import doesn't
291+
// return a structured error. That can't easily be fixed, since it
292+
// invokes 'go list' and returns the error text from the loaded package.
293+
// TODO(golang.org/issue/34750): load using golang.org/x/tools/go/packages
294+
// instead of go/build.
295+
importErrStr := importErr.Error()
296+
if strings.Contains(importErrStr, arg[:period]) {
297+
log.Fatal(importErrStr)
298+
} else {
299+
log.Fatalf("no such package %s: %s", arg[:period], importErrStr)
300+
}
290301
}
291302
// Guess it's a symbol in the current directory.
292303
return importDir(wd), "", arg, false

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

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ go doc rsc.io/quote
3636
! stdout 'Package quote is located in a GOPATH workspace.'
3737
stdout 'Package quote collects pithy sayings.'
3838

39+
# Check that a sensible error message is printed when a package is not found.
40+
env GOPROXY=off
41+
! go doc example.com/hello
42+
stderr '^doc: cannot find module providing package example.com/hello: module lookup disabled by GOPROXY=off$'
43+
3944
-- go.mod --
4045
module x
4146
require rsc.io/quote v1.5.2

0 commit comments

Comments
 (0)