]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: make PackageNotInModuleError reasonable for the Target module
authorBryan C. Mills <bcmills@google.com>
Wed, 3 Jul 2019 21:12:32 +0000 (17:12 -0400)
committerBryan C. Mills <bcmills@google.com>
Thu, 27 Feb 2020 20:07:49 +0000 (20:07 +0000)
Updates #28459
Updates #32917

Change-Id: Iced562cb7c2e0ac075d8345f1e4ad3b073842dcf
Reviewed-on: https://go-review.googlesource.com/c/go/+/185343
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/modload/query.go

index 031e45938a5d7bf18f74016222c0a8571fd12591..cf0dd3ff6e9b3d5a15e8331ee5474d399a04bee4 100644 (file)
@@ -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