From c739bc487d94c0617b3e4454f29271d08d66613c Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 3 Jul 2019 17:12:32 -0400 Subject: [PATCH] cmd/go/internal/modload: make PackageNotInModuleError reasonable for the Target module Updates #28459 Updates #32917 Change-Id: Iced562cb7c2e0ac075d8345f1e4ad3b073842dcf Reviewed-on: https://go-review.googlesource.com/c/go/+/185343 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot Reviewed-by: Jay Conrod --- src/cmd/go/internal/modload/query.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/internal/modload/query.go b/src/cmd/go/internal/modload/query.go index 031e45938a5d7b..cf0dd3ff6e9b3d 100644 --- a/src/cmd/go/internal/modload/query.go +++ b/src/cmd/go/internal/modload/query.go @@ -445,7 +445,11 @@ func QueryPattern(pattern, query string, allowed func(module.Version) bool) ([]Q candidateModules = modulePrefixesExcludingTarget(base) ) if len(candidateModules) == 0 { - return nil, fmt.Errorf("package %s is not in the main module (%s)", pattern, Target.Path) + return nil, &PackageNotInModuleError{ + Mod: Target, + Query: query, + Pattern: pattern, + } } err := modfetch.TryProxies(func(proxy string) error { @@ -541,7 +545,9 @@ func queryPrefixModules(candidateModules []string, queryModule func(path string) case nil: found = append(found, r.QueryResult) case *PackageNotInModuleError: - if noPackage == nil { + // Given the option, prefer to attribute “package not in module” + // to modules other than the main one. + if noPackage == nil || noPackage.Mod == Target { noPackage = rErr } case *NoMatchingVersionError: @@ -626,6 +632,13 @@ type PackageNotInModuleError struct { } func (e *PackageNotInModuleError) Error() string { + if e.Mod == Target { + if strings.Contains(e.Pattern, "...") { + return fmt.Sprintf("main module (%s) does not contain packages matching %s", Target.Path, e.Pattern) + } + return fmt.Sprintf("main module (%s) does not contain package %s", Target.Path, e.Pattern) + } + found := "" if r := e.Replacement; r.Path != "" { replacement := r.Path