Skip to content

Commit 204665c

Browse files
committed
internal/mod/mvs: copy from cue/load/internal/mvs
This is an experimental API and the docs are updated to reflect that. The original code is left in place so that we can avoid updating the code that currently depends on it. It will be removed in a CL later in this chain (https://cuelang.org/cl/1168508). For #2330. Signed-off-by: Roger Peppe <[email protected]> Change-Id: I523288d96d18fae7260d12bde0c881a12262235e Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/557456 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent c6e91b1 commit 204665c

File tree

12 files changed

+1992
-2
lines changed

12 files changed

+1992
-2
lines changed

cue/load/module.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"path/filepath"
77
"strings"
88

9+
"golang.org/x/mod/module"
10+
"golang.org/x/mod/semver"
11+
912
"cuelang.org/go/cue/errors"
1013
"cuelang.org/go/cue/load/internal/mvs"
1114
"cuelang.org/go/cue/token"
12-
"golang.org/x/mod/module"
13-
"golang.org/x/mod/semver"
1415
)
1516

1617
//go:embed moduleschema.cue

internal/mod/mvs/errors.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//go:build ignore
2+
3+
// Copyright 2020 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package mvs
8+
9+
import (
10+
"fmt"
11+
"strings"
12+
13+
"golang.org/x/mod/module"
14+
)
15+
16+
// BuildListError decorates an error that occurred gathering requirements
17+
// while constructing a build list. BuildListError prints the chain
18+
// of requirements to the module where the error occurred.
19+
type BuildListError struct {
20+
Err error
21+
stack []buildListErrorElem
22+
}
23+
24+
type buildListErrorElem struct {
25+
m module.Version
26+
27+
// nextReason is the reason this module depends on the next module in the
28+
// stack. Typically either "requires", or "updating to".
29+
nextReason string
30+
}
31+
32+
// NewBuildListError returns a new BuildListError wrapping an error that
33+
// occurred at a module found along the given path of requirements and/or
34+
// upgrades, which must be non-empty.
35+
//
36+
// The isVersionChange function reports whether a path step is due to an
37+
// explicit upgrade or downgrade (as opposed to an existing requirement in a
38+
// go.mod file). A nil isVersionChange function indicates that none of the path
39+
// steps are due to explicit version changes.
40+
func NewBuildListError(err error, path []module.Version, isVersionChange func(from, to module.Version) bool) *BuildListError {
41+
stack := make([]buildListErrorElem, 0, len(path))
42+
for len(path) > 1 {
43+
reason := "requires"
44+
if isVersionChange != nil && isVersionChange(path[0], path[1]) {
45+
reason = "updating to"
46+
}
47+
stack = append(stack, buildListErrorElem{
48+
m: path[0],
49+
nextReason: reason,
50+
})
51+
path = path[1:]
52+
}
53+
stack = append(stack, buildListErrorElem{m: path[0]})
54+
55+
return &BuildListError{
56+
Err: err,
57+
stack: stack,
58+
}
59+
}
60+
61+
// Module returns the module where the error occurred. If the module stack
62+
// is empty, this returns a zero value.
63+
func (e *BuildListError) Module() module.Version {
64+
if len(e.stack) == 0 {
65+
return module.Version{}
66+
}
67+
return e.stack[len(e.stack)-1].m
68+
}
69+
70+
func (e *BuildListError) Error() string {
71+
b := &strings.Builder{}
72+
stack := e.stack
73+
74+
// Don't print modules at the beginning of the chain without a
75+
// version. These always seem to be the main module or a
76+
// synthetic module ("target@").
77+
for len(stack) > 0 && stack[0].m.Version == "" {
78+
stack = stack[1:]
79+
}
80+
81+
if len(stack) == 0 {
82+
b.WriteString(e.Err.Error())
83+
} else {
84+
for _, elem := range stack[:len(stack)-1] {
85+
fmt.Fprintf(b, "%s %s\n\t", elem.m, elem.nextReason)
86+
}
87+
// Ensure that the final module path and version are included as part of the
88+
// error message.
89+
m := stack[len(stack)-1].m
90+
if mErr, ok := e.Err.(*module.ModuleError); ok {
91+
actual := module.Version{Path: mErr.Path, Version: mErr.Version}
92+
if v, ok := mErr.Err.(*module.InvalidVersionError); ok {
93+
actual.Version = v.Version
94+
}
95+
if actual == m {
96+
fmt.Fprintf(b, "%v", e.Err)
97+
} else {
98+
fmt.Fprintf(b, "%s (replaced by %s): %v", m, actual, mErr.Err)
99+
}
100+
} else {
101+
fmt.Fprintf(b, "%v", module.VersionError(m, e.Err))
102+
}
103+
}
104+
return b.String()
105+
}

internal/mod/mvs/graph.go

+227
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
//go:build ignore
2+
3+
// Copyright 2020 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package mvs
8+
9+
import (
10+
"fmt"
11+
12+
"cuelang.org/go/mod/mvs/internal/slices"
13+
14+
"golang.org/x/mod/module"
15+
)
16+
17+
// Graph implements an incremental version of the MVS algorithm, with the
18+
// requirements pushed by the caller instead of pulled by the MVS traversal.
19+
type Graph struct {
20+
cmp func(v1, v2 string) int
21+
roots []module.Version
22+
23+
required map[module.Version][]module.Version
24+
25+
isRoot map[module.Version]bool // contains true for roots and false for reachable non-roots
26+
selected map[string]string // path → version
27+
}
28+
29+
// NewGraph returns an incremental MVS graph containing only a set of root
30+
// dependencies and using the given max function for version strings.
31+
//
32+
// The caller must ensure that the root slice is not modified while the Graph
33+
// may be in use.
34+
func NewGraph(cmp func(v1, v2 string) int, roots []module.Version) *Graph {
35+
g := &Graph{
36+
cmp: cmp,
37+
roots: slices.Clip(roots),
38+
required: make(map[module.Version][]module.Version),
39+
isRoot: make(map[module.Version]bool),
40+
selected: make(map[string]string),
41+
}
42+
43+
for _, m := range roots {
44+
g.isRoot[m] = true
45+
if g.cmp(g.Selected(m.Path), m.Version) < 0 {
46+
g.selected[m.Path] = m.Version
47+
}
48+
}
49+
50+
return g
51+
}
52+
53+
// Require adds the information that module m requires all modules in reqs.
54+
// The reqs slice must not be modified after it is passed to Require.
55+
//
56+
// m must be reachable by some existing chain of requirements from g's target,
57+
// and Require must not have been called for it already.
58+
//
59+
// If any of the modules in reqs has the same path as g's target,
60+
// the target must have higher precedence than the version in req.
61+
func (g *Graph) Require(m module.Version, reqs []module.Version) {
62+
// To help catch disconnected-graph bugs, enforce that all required versions
63+
// are actually reachable from the roots (and therefore should affect the
64+
// selected versions of the modules they name).
65+
if _, reachable := g.isRoot[m]; !reachable {
66+
panic(fmt.Sprintf("%v is not reachable from any root", m))
67+
}
68+
69+
// Truncate reqs to its capacity to avoid aliasing bugs if it is later
70+
// returned from RequiredBy and appended to.
71+
reqs = slices.Clip(reqs)
72+
73+
if _, dup := g.required[m]; dup {
74+
panic(fmt.Sprintf("requirements of %v have already been set", m))
75+
}
76+
g.required[m] = reqs
77+
78+
for _, dep := range reqs {
79+
// Mark dep reachable, regardless of whether it is selected.
80+
if _, ok := g.isRoot[dep]; !ok {
81+
g.isRoot[dep] = false
82+
}
83+
84+
if g.cmp(g.Selected(dep.Path), dep.Version) < 0 {
85+
g.selected[dep.Path] = dep.Version
86+
}
87+
}
88+
}
89+
90+
// RequiredBy returns the slice of requirements passed to Require for m, if any,
91+
// with its capacity reduced to its length.
92+
// If Require has not been called for m, RequiredBy(m) returns ok=false.
93+
//
94+
// The caller must not modify the returned slice, but may safely append to it
95+
// and may rely on it not to be modified.
96+
func (g *Graph) RequiredBy(m module.Version) (reqs []module.Version, ok bool) {
97+
reqs, ok = g.required[m]
98+
return reqs, ok
99+
}
100+
101+
// Selected returns the selected version of the given module path.
102+
//
103+
// If no version is selected, Selected returns version "none".
104+
func (g *Graph) Selected(path string) (version string) {
105+
v, ok := g.selected[path]
106+
if !ok {
107+
return "none"
108+
}
109+
return v
110+
}
111+
112+
// BuildList returns the selected versions of all modules present in the Graph,
113+
// beginning with the selected versions of each module path in the roots of g.
114+
//
115+
// The order of the remaining elements in the list is deterministic
116+
// but arbitrary.
117+
func (g *Graph) BuildList() []module.Version {
118+
seenRoot := make(map[string]bool, len(g.roots))
119+
120+
var list []module.Version
121+
for _, r := range g.roots {
122+
if seenRoot[r.Path] {
123+
// Multiple copies of the same root, with the same or different versions,
124+
// are a bit of a degenerate case: we will take the transitive
125+
// requirements of both roots into account, but only the higher one can
126+
// possibly be selected. However — especially given that we need the
127+
// seenRoot map for later anyway — it is simpler to support this
128+
// degenerate case than to forbid it.
129+
continue
130+
}
131+
132+
if v := g.Selected(r.Path); v != "none" {
133+
list = append(list, module.Version{Path: r.Path, Version: v})
134+
}
135+
seenRoot[r.Path] = true
136+
}
137+
uniqueRoots := list
138+
139+
for path, version := range g.selected {
140+
if !seenRoot[path] {
141+
list = append(list, module.Version{Path: path, Version: version})
142+
}
143+
}
144+
module.Sort(list[len(uniqueRoots):])
145+
146+
return list
147+
}
148+
149+
// WalkBreadthFirst invokes f once, in breadth-first order, for each module
150+
// version other than "none" that appears in the graph, regardless of whether
151+
// that version is selected.
152+
func (g *Graph) WalkBreadthFirst(f func(m module.Version)) {
153+
var queue []module.Version
154+
enqueued := make(map[module.Version]bool)
155+
for _, m := range g.roots {
156+
if m.Version != "none" {
157+
queue = append(queue, m)
158+
enqueued[m] = true
159+
}
160+
}
161+
162+
for len(queue) > 0 {
163+
m := queue[0]
164+
queue = queue[1:]
165+
166+
f(m)
167+
168+
reqs, _ := g.RequiredBy(m)
169+
for _, r := range reqs {
170+
if !enqueued[r] && r.Version != "none" {
171+
queue = append(queue, r)
172+
enqueued[r] = true
173+
}
174+
}
175+
}
176+
}
177+
178+
// FindPath reports a shortest requirement path starting at one of the roots of
179+
// the graph and ending at a module version m for which f(m) returns true, or
180+
// nil if no such path exists.
181+
func (g *Graph) FindPath(f func(module.Version) bool) []module.Version {
182+
// firstRequires[a] = b means that in a breadth-first traversal of the
183+
// requirement graph, the module version a was first required by b.
184+
firstRequires := make(map[module.Version]module.Version)
185+
186+
queue := g.roots
187+
for _, m := range g.roots {
188+
firstRequires[m] = module.Version{}
189+
}
190+
191+
for len(queue) > 0 {
192+
m := queue[0]
193+
queue = queue[1:]
194+
195+
if f(m) {
196+
// Construct the path reversed (because we're starting from the far
197+
// endpoint), then reverse it.
198+
path := []module.Version{m}
199+
for {
200+
m = firstRequires[m]
201+
if m.Path == "" {
202+
break
203+
}
204+
path = append(path, m)
205+
}
206+
207+
i, j := 0, len(path)-1
208+
for i < j {
209+
path[i], path[j] = path[j], path[i]
210+
i++
211+
j--
212+
}
213+
214+
return path
215+
}
216+
217+
reqs, _ := g.RequiredBy(m)
218+
for _, r := range reqs {
219+
if _, seen := firstRequires[r]; !seen {
220+
queue = append(queue, r)
221+
firstRequires[r] = m
222+
}
223+
}
224+
}
225+
226+
return nil
227+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build ignore && !go1.19
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package par
8+
9+
import "sync/atomic"
10+
11+
// atomicBool implements the atomic.Bool type for Go versions before go
12+
// 1.19. It's a copy of the relevant parts of the Go 1.19 atomic.Bool
13+
// code as of commit a4d5fbc3a48b63f19fcd2a4d040a85c75a2709b5.
14+
type atomicBool struct {
15+
v uint32
16+
}
17+
18+
// Load atomically loads and returns the value stored in x.
19+
func (x *atomicBool) Load() bool { return atomic.LoadUint32(&x.v) != 0 }
20+
21+
// Store atomically stores val into x.
22+
func (x *atomicBool) Store(val bool) { atomic.StoreUint32(&x.v, b32(val)) }
23+
24+
// b32 returns a uint32 0 or 1 representing b.
25+
func b32(b bool) uint32 {
26+
if b {
27+
return 1
28+
}
29+
return 0
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build ignore && go1.19
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package par
8+
9+
import "sync/atomic"
10+
11+
type atomicBool = atomic.Bool

0 commit comments

Comments
 (0)