diff options
Diffstat (limited to 'libgo/go/cmd/go/internal/modget/get.go')
-rw-r--r-- | libgo/go/cmd/go/internal/modget/get.go | 85 |
1 files changed, 58 insertions, 27 deletions
diff --git a/libgo/go/cmd/go/internal/modget/get.go b/libgo/go/cmd/go/internal/modget/get.go index 1cae311c4c1..2a0f6346581 100644 --- a/libgo/go/cmd/go/internal/modget/get.go +++ b/libgo/go/cmd/go/internal/modget/get.go @@ -6,25 +6,26 @@ package modget import ( + "errors" + "fmt" + "os" + "path/filepath" + "sort" + "strings" + "sync" + "cmd/go/internal/base" - "cmd/go/internal/cfg" "cmd/go/internal/get" "cmd/go/internal/imports" "cmd/go/internal/load" "cmd/go/internal/modload" - "cmd/go/internal/module" "cmd/go/internal/mvs" "cmd/go/internal/par" "cmd/go/internal/search" - "cmd/go/internal/semver" "cmd/go/internal/work" - "errors" - "fmt" - "os" - "path/filepath" - "sort" - "strings" - "sync" + + "golang.org/x/mod/module" + "golang.org/x/mod/semver" ) var CmdGet = &base.Command{ @@ -199,7 +200,7 @@ func (v *upgradeFlag) Set(s string) error { func (v *upgradeFlag) String() string { return "" } func init() { - work.AddBuildFlags(CmdGet) + work.AddBuildFlags(CmdGet, work.OmitModFlag) CmdGet.Run = runGet // break init loop CmdGet.Flag.BoolVar(&get.Insecure, "insecure", get.Insecure, "") CmdGet.Flag.Var(&getU, "u", "") @@ -256,11 +257,6 @@ type query struct { } func runGet(cmd *base.Command, args []string) { - // -mod=readonly has no effect on "go get". - if cfg.BuildMod == "readonly" { - cfg.BuildMod = "" - } - switch getU { case "", "upgrade", "patch": // ok @@ -278,10 +274,6 @@ func runGet(cmd *base.Command, args []string) { } modload.LoadTests = *getT - if cfg.BuildMod == "vendor" { - base.Fatalf("go get: disabled by -mod=%s", cfg.BuildMod) - } - buildList := modload.LoadBuildList() buildList = buildList[:len(buildList):len(buildList)] // copy on append versionByPath := make(map[string]string) @@ -294,6 +286,10 @@ func runGet(cmd *base.Command, args []string) { // what was requested. modload.DisallowWriteGoMod() + // Allow looking up modules for import paths outside of a module. + // 'go get' is expected to do this, unlike other commands. + modload.AllowMissingModuleImports() + // Parse command-line arguments and report errors. The command-line // arguments are of the form path@version or simply path, with implicit // @upgrade. path@none is "downgrade away". @@ -364,6 +360,10 @@ func runGet(cmd *base.Command, args []string) { // upgrade golang.org/x/tools. case path == "all": + // If there is no main module, "all" is not meaningful. + if !modload.HasModRoot() { + base.Errorf(`go get %s: cannot match "all": working directory is not part of a module`, arg) + } // Don't query modules until we load packages. We'll automatically // look up any missing modules. @@ -372,13 +372,15 @@ func runGet(cmd *base.Command, args []string) { continue default: - // The argument is a package path. - if pkgs := modload.TargetPackages(path); len(pkgs) != 0 { - // The path is in the main module. Nothing to query. - if vers != "upgrade" && vers != "patch" { - base.Errorf("go get %s: can't request explicit version of path in main module", arg) + // The argument is a package or module path. + if modload.HasModRoot() { + if pkgs := modload.TargetPackages(path); len(pkgs) != 0 { + // The path is in the main module. Nothing to query. + if vers != "upgrade" && vers != "patch" { + base.Errorf("go get %s: can't request explicit version of path in main module", arg) + } + continue } - continue } first := path @@ -452,10 +454,13 @@ func runGet(cmd *base.Command, args []string) { // This includes explicitly requested modules that don't have a root package // and modules with a target version of "none". var wg sync.WaitGroup + var modOnlyMu sync.Mutex modOnly := make(map[string]*query) for _, q := range queries { if q.m.Version == "none" { + modOnlyMu.Lock() modOnly[q.m.Path] = q + modOnlyMu.Unlock() continue } if q.path == q.m.Path { @@ -464,7 +469,9 @@ func runGet(cmd *base.Command, args []string) { if hasPkg, err := modload.ModuleHasRootPackage(q.m); err != nil { base.Errorf("go get: %v", err) } else if !hasPkg { + modOnlyMu.Lock() modOnly[q.m.Path] = q + modOnlyMu.Unlock() } wg.Done() }(q) @@ -763,6 +770,9 @@ func getQuery(path, vers string, prevM module.Version, forceModulePath bool) (mo info, err := modload.Query(path, vers, prevM.Version, modload.Allowed) if err == nil { + if info.Version != vers && info.Version != prevM.Version { + logOncef("go: %s %s => %s", path, vers, info.Version) + } return module.Version{Path: path, Version: info.Version}, nil } @@ -791,6 +801,9 @@ func getQuery(path, vers string, prevM module.Version, forceModulePath bool) (mo if !strings.Contains(path, "...") { var modErr *modload.PackageNotInModuleError if errors.As(err, &modErr) && modErr.Mod.Path == path { + if modErr.Mod.Version != vers { + logOncef("go: %s %s => %s", path, vers, modErr.Mod.Version) + } return modErr.Mod, nil } } @@ -798,7 +811,13 @@ func getQuery(path, vers string, prevM module.Version, forceModulePath bool) (mo return module.Version{}, err } - return results[0].Mod, nil + m := results[0].Mod + if m.Path != path { + logOncef("go: found %s in %s %s", path, m.Path, m.Version) + } else if m.Version != vers { + logOncef("go: %s %s => %s", path, vers, m.Version) + } + return m, nil } // An upgrader adapts an underlying mvs.Reqs to apply an @@ -955,6 +974,9 @@ func (u *upgrader) Upgrade(m module.Version) (module.Version, error) { return m, nil } + if info.Version != m.Version { + logOncef("go: %s %s => %s", m.Path, getU, info.Version) + } return module.Version{Path: m.Path, Version: info.Version}, nil } @@ -983,3 +1005,12 @@ func (r *lostUpgradeReqs) Required(mod module.Version) ([]module.Version, error) } return r.Reqs.Required(mod) } + +var loggedLines sync.Map + +func logOncef(format string, args ...interface{}) { + msg := fmt.Sprintf(format, args...) + if _, dup := loggedLines.LoadOrStore(msg, true); !dup { + fmt.Fprintln(os.Stderr, msg) + } +} |