Skip to content

Commit 8be0de1

Browse files
author
Jay Conrod
committed
cmd/go/internal/modload: document mvsReqs.Max
The version "" denotes the main module, which has no version. The mvs.Reqs interface documentation hints this is allowed, but it's not obvious from the implementation in modload.mvsReqs.Max. Also, replace a related TODO with a comment in mvs.Downgrade. Fixes #39042 Change-Id: I11e10908c9b3d8c2283eaa5c04bd8e1b936851fd Reviewed-on: https://go-review.googlesource.com/c/go/+/234003 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent f8662a5 commit 8be0de1

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/cmd/go/internal/modload/mvs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ func (r *mvsReqs) required(mod module.Version) ([]module.Version, error) {
157157
return r.modFileToList(f), nil
158158
}
159159

160+
// Max returns the maximum of v1 and v2 according to semver.Compare.
161+
//
162+
// As a special case, the version "" is considered higher than all other
163+
// versions. The main module (also known as the target) has no version and must
164+
// be chosen over other versions of the same module in the module dependency
165+
// graph.
160166
func (*mvsReqs) Max(v1, v2 string) string {
161167
if v1 != "" && semver.Compare(v1, v2) == -1 {
162168
return v2

src/cmd/go/internal/mvs/mvs.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,21 @@ func (e *BuildListError) Error() string {
115115
}
116116

117117
// BuildList returns the build list for the target module.
118-
// The first element is the target itself, with the remainder of the list sorted by path.
118+
//
119+
// target is the root vertex of a module requirement graph. For cmd/go, this is
120+
// typically the main module, but note that this algorithm is not intended to
121+
// be Go-specific: module paths and versions are treated as opaque values.
122+
//
123+
// reqs describes the module requirement graph and provides an opaque method
124+
// for comparing versions.
125+
//
126+
// BuildList traverses the graph and returns a list containing the highest
127+
// version for each visited module. The first element of the returned list is
128+
// target itself; reqs.Max requires target.Version to compare higher than all
129+
// other versions, so no other version can be selected. The remaining elements
130+
// of the list are sorted by path.
131+
//
132+
// See https://research.swtch.com/vgo-mvs for details.
119133
func BuildList(target module.Version, reqs Reqs) ([]module.Version, error) {
120134
return buildList(target, reqs, nil)
121135
}
@@ -220,10 +234,9 @@ func buildList(target module.Version, reqs Reqs, upgrade func(module.Version) (m
220234
// The final list is the minimum version of each module found in the graph.
221235

222236
if v := min[target.Path]; v != target.Version {
223-
// TODO(jayconrod): there is a special case in modload.mvsReqs.Max
224-
// that prevents us from selecting a newer version of a module
225-
// when the module has no version. This may only be the case for target.
226-
// Should we always panic when target has a version?
237+
// target.Version will be "" for modload, the main client of MVS.
238+
// "" denotes the main module, which has no version. However, MVS treats
239+
// version strings as opaque, so "" is not a special value here.
227240
// See golang.org/issue/31491, golang.org/issue/29773.
228241
panic(fmt.Sprintf("mistake: chose version %q instead of target %+v", v, target)) // TODO: Don't panic.
229242
}

0 commit comments

Comments
 (0)