Skip to content

Commit 0c86172

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modload: add structured errors for queries matching the main module
For #37438 Change-Id: I7df80ae0917b0b4ecad98947da39ddf8554b07c7 Reviewed-on: https://go-review.googlesource.com/c/go/+/266717 Trust: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
1 parent 04b5b4f commit 0c86172

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ func queryProxy(ctx context.Context, proxy, path, query, current string, allowed
109109
}
110110

111111
if path == Target.Path {
112-
if query != "latest" {
113-
return nil, fmt.Errorf("can't query specific version (%q) for the main module (%s)", query, path)
112+
if query != "latest" && query != "upgrade" && query != "patch" {
113+
return nil, &QueryMatchesMainModuleError{Pattern: path, Query: query}
114114
}
115115
if err := allowed(ctx, Target); err != nil {
116116
return nil, fmt.Errorf("internal error: main module version is not allowed: %w", err)
@@ -579,7 +579,11 @@ func QueryPattern(ctx context.Context, pattern, query string, current func(strin
579579
m := match(Target, modRoot, true)
580580
if len(m.Pkgs) > 0 {
581581
if query != "latest" && query != "upgrade" && query != "patch" {
582-
return nil, nil, fmt.Errorf("can't query version %q for package %s in the main module (%s)", query, pattern, Target.Path)
582+
return nil, nil, &QueryMatchesPackagesInMainModuleError{
583+
Pattern: pattern,
584+
Query: query,
585+
Packages: m.Pkgs,
586+
}
583587
}
584588
if err := allowed(ctx, Target); err != nil {
585589
return nil, nil, fmt.Errorf("internal error: package %s is in the main module (%s), but version is not allowed: %w", pattern, Target.Path, err)
@@ -1023,3 +1027,39 @@ func (rr *replacementRepo) replacementStat(v string) (*modfetch.RevInfo, error)
10231027
}
10241028
return rev, nil
10251029
}
1030+
1031+
// A QueryMatchesMainModuleError indicates that a query requests
1032+
// a version of the main module that cannot be satisfied.
1033+
// (The main module's version cannot be changed.)
1034+
type QueryMatchesMainModuleError struct {
1035+
Pattern string
1036+
Query string
1037+
}
1038+
1039+
func (e *QueryMatchesMainModuleError) Error() string {
1040+
if e.Pattern == Target.Path {
1041+
return fmt.Sprintf("can't request version %q of the main module (%s)", e.Query, e.Pattern)
1042+
}
1043+
1044+
return fmt.Sprintf("can't request version %q of pattern %q that includes the main module (%s)", e.Query, e.Pattern, Target.Path)
1045+
}
1046+
1047+
// A QueryMatchesPackagesInMainModuleError indicates that a query cannot be
1048+
// satisfied because it matches one or more packages found in the main module.
1049+
type QueryMatchesPackagesInMainModuleError struct {
1050+
Pattern string
1051+
Query string
1052+
Packages []string
1053+
}
1054+
1055+
func (e *QueryMatchesPackagesInMainModuleError) Error() string {
1056+
if len(e.Packages) > 1 {
1057+
return fmt.Sprintf("pattern %s matches %d packages in the main module, so can't request version %s", e.Pattern, len(e.Packages), e.Query)
1058+
}
1059+
1060+
if search.IsMetaPackage(e.Pattern) || strings.Contains(e.Pattern, "...") {
1061+
return fmt.Sprintf("pattern %s matches package %s in the main module, so can't request version %s", e.Pattern, e.Packages[0], e.Query)
1062+
}
1063+
1064+
return fmt.Sprintf("package %s is in the main module, so can't request version %s", e.Packages[0], e.Query)
1065+
}

0 commit comments

Comments
 (0)