@@ -109,8 +109,8 @@ func queryProxy(ctx context.Context, proxy, path, query, current string, allowed
109
109
}
110
110
111
111
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 }
114
114
}
115
115
if err := allowed (ctx , Target ); err != nil {
116
116
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
579
579
m := match (Target , modRoot , true )
580
580
if len (m .Pkgs ) > 0 {
581
581
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
+ }
583
587
}
584
588
if err := allowed (ctx , Target ); err != nil {
585
589
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)
1023
1027
}
1024
1028
return rev , nil
1025
1029
}
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