Skip to content

Commit

Permalink
cmd/go/internal/modload: make PackageNotInModuleError reasonable for …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Jay Conrod <[email protected]>
  • Loading branch information
Bryan C. Mills committed Feb 27, 2020
1 parent 956f648 commit c739bc4
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/cmd/go/internal/modload/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c739bc4

Please sign in to comment.