Commit b2a5893f authored by Jay Conrod's avatar Jay Conrod

cmd/go: reduce redundancy in direct mode lookup error messages

get.RepoRootForImportPath now returns errors that satisfy
load.ImportPathError in cases where the import path appears in the
messages. (The import path probably should appear in all errors from
this function, but this CL does not change these errors).

Changed modfetch.notExistError to be a wrapper (with an Unwrap method)
instead of a string. This means errors.As works with notFoundError and
ImportPathError.

ImportMissingError no longer prints the package path if it wraps an
ImportPathError.

TestMissingImportErrorRepetition no longer counts the package path
within a URL (like https://...?go-get=1).

Fixes #35986

Change-Id: I38f795191c46d04b542c553e705f23822260c790
Reviewed-on: https://go-review.googlesource.com/c/go/+/210338
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
parent a6c8fac7
...@@ -21,6 +21,7 @@ import ( ...@@ -21,6 +21,7 @@ import (
"cmd/go/internal/base" "cmd/go/internal/base"
"cmd/go/internal/cfg" "cmd/go/internal/cfg"
"cmd/go/internal/load"
"cmd/go/internal/web" "cmd/go/internal/web"
) )
...@@ -661,7 +662,7 @@ func RepoRootForImportPath(importPath string, mod ModuleMode, security web.Secur ...@@ -661,7 +662,7 @@ func RepoRootForImportPath(importPath string, mod ModuleMode, security web.Secur
if err == errUnknownSite { if err == errUnknownSite {
rr, err = repoRootForImportDynamic(importPath, mod, security) rr, err = repoRootForImportDynamic(importPath, mod, security)
if err != nil { if err != nil {
err = fmt.Errorf("unrecognized import path %q: %v", importPath, err) err = load.ImportErrorf(importPath, "unrecognized import path %q: %v", importPath, err)
} }
} }
if err != nil { if err != nil {
...@@ -676,7 +677,7 @@ func RepoRootForImportPath(importPath string, mod ModuleMode, security web.Secur ...@@ -676,7 +677,7 @@ func RepoRootForImportPath(importPath string, mod ModuleMode, security web.Secur
if err == nil && strings.Contains(importPath, "...") && strings.Contains(rr.Root, "...") { if err == nil && strings.Contains(importPath, "...") && strings.Contains(rr.Root, "...") {
// Do not allow wildcards in the repo root. // Do not allow wildcards in the repo root.
rr = nil rr = nil
err = fmt.Errorf("cannot expand ... in %q", importPath) err = load.ImportErrorf(importPath, "cannot expand ... in %q", importPath)
} }
return rr, err return rr, err
} }
...@@ -700,7 +701,7 @@ func repoRootFromVCSPaths(importPath string, security web.SecurityMode, vcsPaths ...@@ -700,7 +701,7 @@ func repoRootFromVCSPaths(importPath string, security web.SecurityMode, vcsPaths
m := srv.regexp.FindStringSubmatch(importPath) m := srv.regexp.FindStringSubmatch(importPath)
if m == nil { if m == nil {
if srv.prefix != "" { if srv.prefix != "" {
return nil, fmt.Errorf("invalid %s import path %q", srv.prefix, importPath) return nil, load.ImportErrorf(importPath, "invalid %s import path %q", srv.prefix, importPath)
} }
continue continue
} }
......
...@@ -359,7 +359,7 @@ func (r *codeRepo) convert(info *codehost.RevInfo, statVers string) (*RevInfo, e ...@@ -359,7 +359,7 @@ func (r *codeRepo) convert(info *codehost.RevInfo, statVers string) (*RevInfo, e
Path: r.modPath, Path: r.modPath,
Err: &module.InvalidVersionError{ Err: &module.InvalidVersionError{
Version: info2.Version, Version: info2.Version,
Err: notExistError(err.Error()), Err: notExistError{err: err},
}, },
} }
} }
......
...@@ -250,9 +250,9 @@ func (lookupDisabledError) Error() string { ...@@ -250,9 +250,9 @@ func (lookupDisabledError) Error() string {
var errLookupDisabled error = lookupDisabledError{} var errLookupDisabled error = lookupDisabledError{}
var ( var (
errProxyOff = notExistError("module lookup disabled by GOPROXY=off") errProxyOff = notExistErrorf("module lookup disabled by GOPROXY=off")
errNoproxy error = notExistError("disabled by GOPRIVATE/GONOPROXY") errNoproxy error = notExistErrorf("disabled by GOPRIVATE/GONOPROXY")
errUseProxy error = notExistError("path does not match GOPRIVATE/GONOPROXY") errUseProxy error = notExistErrorf("path does not match GOPRIVATE/GONOPROXY")
) )
func lookupDirect(path string) (Repo, error) { func lookupDirect(path string) (Repo, error) {
...@@ -264,7 +264,7 @@ func lookupDirect(path string) (Repo, error) { ...@@ -264,7 +264,7 @@ func lookupDirect(path string) (Repo, error) {
rr, err := get.RepoRootForImportPath(path, get.PreferMod, security) rr, err := get.RepoRootForImportPath(path, get.PreferMod, security)
if err != nil { if err != nil {
// We don't know where to find code for a module with this path. // We don't know where to find code for a module with this path.
return nil, notExistError(err.Error()) return nil, notExistError{err: err}
} }
if rr.VCS == "mod" { if rr.VCS == "mod" {
...@@ -408,11 +408,22 @@ func (l *loggingRepo) Zip(dst io.Writer, version string) error { ...@@ -408,11 +408,22 @@ func (l *loggingRepo) Zip(dst io.Writer, version string) error {
} }
// A notExistError is like os.ErrNotExist, but with a custom message // A notExistError is like os.ErrNotExist, but with a custom message
type notExistError string type notExistError struct {
err error
}
func notExistErrorf(format string, args ...interface{}) error {
return notExistError{fmt.Errorf(format, args...)}
}
func (e notExistError) Error() string { func (e notExistError) Error() string {
return string(e) return e.err.Error()
} }
func (notExistError) Is(target error) bool { func (notExistError) Is(target error) bool {
return target == os.ErrNotExist return target == os.ErrNotExist
} }
func (e notExistError) Unwrap() error {
return e.err
}
...@@ -43,6 +43,9 @@ func (e *ImportMissingError) Error() string { ...@@ -43,6 +43,9 @@ func (e *ImportMissingError) Error() string {
if str.HasPathPrefix(e.Path, "cmd") { if str.HasPathPrefix(e.Path, "cmd") {
return fmt.Sprintf("package %s is not in GOROOT (%s)", e.Path, filepath.Join(cfg.GOROOT, "src", e.Path)) return fmt.Sprintf("package %s is not in GOROOT (%s)", e.Path, filepath.Join(cfg.GOROOT, "src", e.Path))
} }
if i := load.ImportPathError(nil); errors.As(e.QueryErr, &i) {
return fmt.Sprintf("cannot find module: %v", e.QueryErr)
}
if e.QueryErr != nil { if e.QueryErr != nil {
return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr) return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr)
} }
......
...@@ -515,14 +515,21 @@ func TestMissingImportErrorRepetition(t *testing.T) { ...@@ -515,14 +515,21 @@ func TestMissingImportErrorRepetition(t *testing.T) {
os.Setenv("GO111MODULE", "on") os.Setenv("GO111MODULE", "on")
defer os.Setenv("GOPROXY", os.Getenv("GOPROXY")) defer os.Setenv("GOPROXY", os.Getenv("GOPROXY"))
os.Setenv("GOPROXY", "off") os.Setenv("GOPROXY", "off")
defer os.Setenv("GONOPROXY", os.Getenv("GONOPROXY"))
os.Setenv("GONOPROXY", "none")
ctxt := Default ctxt := Default
ctxt.WorkingDir = tmp ctxt.WorkingDir = tmp
pkgPath := "example.com/hello" pkgPath := "example.com/hello"
if _, err = ctxt.Import(pkgPath, tmp, FindOnly); err == nil { _, err = ctxt.Import(pkgPath, tmp, FindOnly)
if err == nil {
t.Fatal("unexpected success") t.Fatal("unexpected success")
} else if n := strings.Count(err.Error(), pkgPath); n != 1 { }
// Don't count the package path with a URL like https://...?go-get=1.
// See golang.org/issue/35986.
errStr := strings.ReplaceAll(err.Error(), "://"+pkgPath+"?go-get=1", "://...?go-get=1")
if n := strings.Count(errStr, pkgPath); n != 1 {
t.Fatalf("package path %q appears in error %d times; should appear once\nerror: %v", pkgPath, n, err) t.Fatalf("package path %q appears in error %d times; should appear once\nerror: %v", pkgPath, n, err)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment