@@ -19,6 +19,7 @@ import (
19
19
"cmd/go/internal/semver"
20
20
"cmd/go/internal/str"
21
21
"cmd/go/internal/work"
22
+ "errors"
22
23
"fmt"
23
24
"os"
24
25
"path/filepath"
@@ -351,10 +352,17 @@ func runGet(cmd *base.Command, args []string) {
351
352
match := search .MatchPattern (path )
352
353
matched := false
353
354
for _ , m := range modload .BuildList () {
354
- // TODO(bcmills): Patterns that don't contain the module path but do
355
- // contain partial package paths will not match here. For example,
356
- // ...html/... would not match html/template or golang.org/x/net/html.
357
- // Related golang.org/issue/26902.
355
+ // TODO: If we have matching packages already in the build list and we
356
+ // know which module(s) they are in, then we should not upgrade the
357
+ // modules that do *not* contain those packages, even if the module path
358
+ // is a prefix of the pattern.
359
+ //
360
+ // For example, if we have modules golang.org/x/tools and
361
+ // golang.org/x/tools/playground, and all of the packages matching
362
+ // golang.org/x/tools/playground... are in the
363
+ // golang.org/x/tools/playground module, then we should not *also* try
364
+ // to upgrade golang.org/x/tools if the user says 'go get
365
+ // golang.org/x/tools/playground...@latest'.
358
366
if match (m .Path ) || str .HasPathPrefix (path , m .Path ) {
359
367
tasks = append (tasks , & task {arg : arg , path : m .Path , vers : vers , prevM : m , forceModulePath : true })
360
368
matched = true
@@ -650,29 +658,31 @@ func getQuery(path, vers string, prevM module.Version, forceModulePath bool) (mo
650
658
}
651
659
}
652
660
653
- // If the path has a wildcard, search for a module that matches the pattern.
654
- if strings .Contains (path , "..." ) {
655
- if forceModulePath {
656
- panic ("forceModulePath is true for path with wildcard " + path )
661
+ if forceModulePath || * getM || ! strings .Contains (path , "..." ) {
662
+ if path == modload .Target .Path {
663
+ if vers != "latest" {
664
+ return module.Version {}, fmt .Errorf ("can't get a specific version of the main module" )
665
+ }
666
+ }
667
+
668
+ // If the path doesn't contain a wildcard, try interpreting it as a module path.
669
+ info , err := modload .Query (path , vers , modload .Allowed )
670
+ if err == nil {
671
+ return module.Version {Path : path , Version : info .Version }, nil
657
672
}
658
- _ , m , _ , err := modload .QueryPattern (path , vers , modload .Allowed )
659
- return m , err
660
- }
661
673
662
- // Try interpreting the path as a module path .
663
- info , err := modload . Query ( path , vers , modload . Allowed )
664
- if err == nil {
665
- return module. Version { Path : path , Version : info . Version }, nil
674
+ // If the query fails, and the path must be a real module, report the query error .
675
+ if forceModulePath || * getM {
676
+ return module. Version {}, err
677
+ }
666
678
}
667
679
668
- // If the query fails, and the path must be a real module, report the query error.
669
- if forceModulePath || * getM {
680
+ // Otherwise, try a package path or pattern.
681
+ results , err := modload .QueryPattern (path , vers , modload .Allowed )
682
+ if err != nil {
670
683
return module.Version {}, err
671
684
}
672
-
673
- // Otherwise, try a package path.
674
- m , _ , err := modload .QueryPackage (path , vers , modload .Allowed )
675
- return m , err
685
+ return results [0 ].Mod , nil
676
686
}
677
687
678
688
// An upgrader adapts an underlying mvs.Reqs to apply an
@@ -736,7 +746,8 @@ func (u *upgrader) Upgrade(m module.Version) (module.Version, error) {
736
746
// even report the error. Because Query does not consider pseudo-versions,
737
747
// it may happen that we have a pseudo-version but during -u=patch
738
748
// the query v0.0 matches no versions (not even the one we're using).
739
- if ! strings .Contains (err .Error (), "no matching versions" ) {
749
+ var noMatch * modload.NoMatchingVersionError
750
+ if ! errors .As (err , & noMatch ) {
740
751
base .Errorf ("go get: upgrading %s@%s: %v" , m .Path , m .Version , err )
741
752
}
742
753
return m , nil
0 commit comments