Skip to content

Commit 521393e

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modget: move MVS code to a separate file
For #36460 Change-Id: Ie81c03df18c6987527da765d5f6575556340cb01 Reviewed-on: https://go-review.googlesource.com/c/go/+/249877 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 363fb4b commit 521393e

File tree

2 files changed

+203
-187
lines changed

2 files changed

+203
-187
lines changed

src/cmd/go/internal/modget/get.go

Lines changed: 1 addition & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
290290
// what was requested.
291291
modload.DisallowWriteGoMod()
292292

293-
// Allow looking up modules for import paths outside of a module.
293+
// Allow looking up modules for import paths when outside of a module.
294294
// 'go get' is expected to do this, unlike other commands.
295295
modload.AllowMissingModuleImports()
296296

@@ -885,192 +885,6 @@ func getQuery(ctx context.Context, path, vers string, prevM module.Version, forc
885885
return m, nil
886886
}
887887

888-
// An upgrader adapts an underlying mvs.Reqs to apply an
889-
// upgrade policy to a list of targets and their dependencies.
890-
type upgrader struct {
891-
mvs.Reqs
892-
893-
// cmdline maps a module path to a query made for that module at a
894-
// specific target version. Each query corresponds to a module
895-
// matched by a command line argument.
896-
cmdline map[string]*query
897-
898-
// upgrade is a set of modules providing dependencies of packages
899-
// matched by command line arguments. If -u or -u=patch is set,
900-
// these modules are upgraded accordingly.
901-
upgrade map[string]bool
902-
}
903-
904-
// newUpgrader creates an upgrader. cmdline contains queries made at
905-
// specific versions for modules matched by command line arguments. pkgs
906-
// is the set of packages matched by command line arguments. If -u or -u=patch
907-
// is set, modules providing dependencies of pkgs are upgraded accordingly.
908-
func newUpgrader(cmdline map[string]*query, pkgs map[string]bool) *upgrader {
909-
u := &upgrader{
910-
Reqs: modload.Reqs(),
911-
cmdline: cmdline,
912-
}
913-
if getU != "" {
914-
u.upgrade = make(map[string]bool)
915-
916-
// Traverse package import graph.
917-
// Initialize work queue with root packages.
918-
seen := make(map[string]bool)
919-
var work []string
920-
add := func(path string) {
921-
if !seen[path] {
922-
seen[path] = true
923-
work = append(work, path)
924-
}
925-
}
926-
for pkg := range pkgs {
927-
add(pkg)
928-
}
929-
for len(work) > 0 {
930-
pkg := work[0]
931-
work = work[1:]
932-
m := modload.PackageModule(pkg)
933-
u.upgrade[m.Path] = true
934-
935-
// testImports is empty unless test imports were actually loaded,
936-
// i.e., -t was set or "all" was one of the arguments.
937-
imports, testImports := modload.PackageImports(pkg)
938-
for _, imp := range imports {
939-
add(imp)
940-
}
941-
for _, imp := range testImports {
942-
add(imp)
943-
}
944-
}
945-
}
946-
return u
947-
}
948-
949-
// Required returns the requirement list for m.
950-
// For the main module, we override requirements with the modules named
951-
// one the command line, and we include new requirements. Otherwise,
952-
// we defer to u.Reqs.
953-
func (u *upgrader) Required(m module.Version) ([]module.Version, error) {
954-
rs, err := u.Reqs.Required(m)
955-
if err != nil {
956-
return nil, err
957-
}
958-
if m != modload.Target {
959-
return rs, nil
960-
}
961-
962-
overridden := make(map[string]bool)
963-
for i, m := range rs {
964-
if q := u.cmdline[m.Path]; q != nil && q.m.Version != "none" {
965-
rs[i] = q.m
966-
overridden[q.m.Path] = true
967-
}
968-
}
969-
for _, q := range u.cmdline {
970-
if !overridden[q.m.Path] && q.m.Path != modload.Target.Path && q.m.Version != "none" {
971-
rs = append(rs, q.m)
972-
}
973-
}
974-
return rs, nil
975-
}
976-
977-
// Upgrade returns the desired upgrade for m.
978-
//
979-
// If m was requested at a specific version on the command line, then
980-
// Upgrade returns that version.
981-
//
982-
// If -u is set and m provides a dependency of a package matched by
983-
// command line arguments, then Upgrade may provider a newer tagged version.
984-
// If m is a tagged version, then Upgrade will return the latest tagged
985-
// version (with the same minor version number if -u=patch).
986-
// If m is a pseudo-version, then Upgrade returns the latest tagged version
987-
// only if that version has a time-stamp newer than m. This special case
988-
// prevents accidental downgrades when already using a pseudo-version
989-
// newer than the latest tagged version.
990-
//
991-
// If none of the above cases apply, then Upgrade returns m.
992-
func (u *upgrader) Upgrade(m module.Version) (module.Version, error) {
993-
// Allow pkg@vers on the command line to override the upgrade choice v.
994-
// If q's version is < m.Version, then we're going to downgrade anyway,
995-
// and it's cleaner to avoid moving back and forth and picking up
996-
// extraneous other newer dependencies.
997-
// If q's version is > m.Version, then we're going to upgrade past
998-
// m.Version anyway, and again it's cleaner to avoid moving back and forth
999-
// picking up extraneous other newer dependencies.
1000-
if q := u.cmdline[m.Path]; q != nil {
1001-
return q.m, nil
1002-
}
1003-
1004-
if !u.upgrade[m.Path] {
1005-
// Not involved in upgrade. Leave alone.
1006-
return m, nil
1007-
}
1008-
1009-
// Run query required by upgrade semantics.
1010-
// Note that Query "latest" is not the same as using repo.Latest,
1011-
// which may return a pseudoversion for the latest commit.
1012-
// Query "latest" returns the newest tagged version or the newest
1013-
// prerelease version if there are no non-prereleases, or repo.Latest
1014-
// if there aren't any tagged versions.
1015-
// If we're querying "upgrade" or "patch", Query will compare the current
1016-
// version against the chosen version and will return the current version
1017-
// if it is newer.
1018-
info, err := modload.Query(context.TODO(), m.Path, string(getU), m.Version, modload.CheckAllowed)
1019-
if err != nil {
1020-
// Report error but return m, to let version selection continue.
1021-
// (Reporting the error will fail the command at the next base.ExitIfErrors.)
1022-
1023-
// Special case: if the error is for m.Version itself and m.Version has a
1024-
// replacement, then keep it and don't report the error: the fact that the
1025-
// version is invalid is likely the reason it was replaced to begin with.
1026-
var vErr *module.InvalidVersionError
1027-
if errors.As(err, &vErr) && vErr.Version == m.Version && modload.Replacement(m).Path != "" {
1028-
return m, nil
1029-
}
1030-
1031-
// Special case: if the error is "no matching versions" then don't
1032-
// even report the error. Because Query does not consider pseudo-versions,
1033-
// it may happen that we have a pseudo-version but during -u=patch
1034-
// the query v0.0 matches no versions (not even the one we're using).
1035-
var noMatch *modload.NoMatchingVersionError
1036-
if !errors.As(err, &noMatch) {
1037-
base.Errorf("go get: upgrading %s@%s: %v", m.Path, m.Version, err)
1038-
}
1039-
return m, nil
1040-
}
1041-
1042-
if info.Version != m.Version {
1043-
logOncef("go: %s %s => %s", m.Path, getU, info.Version)
1044-
}
1045-
return module.Version{Path: m.Path, Version: info.Version}, nil
1046-
}
1047-
1048-
// buildListForLostUpgrade returns the build list for the module graph
1049-
// rooted at lost. Unlike mvs.BuildList, the target module (lost) is not
1050-
// treated specially. The returned build list may contain a newer version
1051-
// of lost.
1052-
//
1053-
// buildListForLostUpgrade is used after a downgrade has removed a module
1054-
// requested at a specific version. This helps us understand the requirements
1055-
// implied by each downgrade.
1056-
func buildListForLostUpgrade(lost module.Version, reqs mvs.Reqs) ([]module.Version, error) {
1057-
return mvs.BuildList(lostUpgradeRoot, &lostUpgradeReqs{Reqs: reqs, lost: lost})
1058-
}
1059-
1060-
var lostUpgradeRoot = module.Version{Path: "lost-upgrade-root", Version: ""}
1061-
1062-
type lostUpgradeReqs struct {
1063-
mvs.Reqs
1064-
lost module.Version
1065-
}
1066-
1067-
func (r *lostUpgradeReqs) Required(mod module.Version) ([]module.Version, error) {
1068-
if mod == lostUpgradeRoot {
1069-
return []module.Version{r.lost}, nil
1070-
}
1071-
return r.Reqs.Required(mod)
1072-
}
1073-
1074888
// reportRetractions prints warnings if any modules in the build list are
1075889
// retracted.
1076890
func reportRetractions(ctx context.Context) {

0 commit comments

Comments
 (0)