Skip to content

Commit c739bc4

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

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/cmd/go/internal/modload/query.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,11 @@ func QueryPattern(pattern, query string, allowed func(module.Version) bool) ([]Q
445445
candidateModules = modulePrefixesExcludingTarget(base)
446446
)
447447
if len(candidateModules) == 0 {
448-
return nil, fmt.Errorf("package %s is not in the main module (%s)", pattern, Target.Path)
448+
return nil, &PackageNotInModuleError{
449+
Mod: Target,
450+
Query: query,
451+
Pattern: pattern,
452+
}
449453
}
450454

451455
err := modfetch.TryProxies(func(proxy string) error {
@@ -541,7 +545,9 @@ func queryPrefixModules(candidateModules []string, queryModule func(path string)
541545
case nil:
542546
found = append(found, r.QueryResult)
543547
case *PackageNotInModuleError:
544-
if noPackage == nil {
548+
// Given the option, prefer to attribute “package not in module”
549+
// to modules other than the main one.
550+
if noPackage == nil || noPackage.Mod == Target {
545551
noPackage = rErr
546552
}
547553
case *NoMatchingVersionError:
@@ -626,6 +632,13 @@ type PackageNotInModuleError struct {
626632
}
627633

628634
func (e *PackageNotInModuleError) Error() string {
635+
if e.Mod == Target {
636+
if strings.Contains(e.Pattern, "...") {
637+
return fmt.Sprintf("main module (%s) does not contain packages matching %s", Target.Path, e.Pattern)
638+
}
639+
return fmt.Sprintf("main module (%s) does not contain package %s", Target.Path, e.Pattern)
640+
}
641+
629642
found := ""
630643
if r := e.Replacement; r.Path != "" {
631644
replacement := r.Path

0 commit comments

Comments
 (0)