forked from sdboyer/gps
/
satisfy.go
279 lines (248 loc) · 7.68 KB
/
satisfy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package vsolver
// checkProject performs all constraint checks on a new project (with packages)
// that we want to select. It determines if selecting the atom would result in
// a state where all solver requirements are still satisfied.
func (s *solver) checkProject(a atomWithPackages) error {
pa := a.a
if nilpa == pa {
// This shouldn't be able to happen, but if it does, it unequivocally
// indicates a logical bug somewhere, so blowing up is preferable
panic("canary - checking version of empty ProjectAtom")
}
if err := s.checkAtomAllowable(pa); err != nil {
return err
}
if err := s.checkRequiredPackagesExist(a); err != nil {
return err
}
deps, err := s.getImportsAndConstraintsOf(a)
if err != nil {
// An err here would be from the package fetcher; pass it straight back
return err
}
for _, dep := range deps {
if err := s.checkIdentMatches(a, dep); err != nil {
return err
}
if err := s.checkDepsConstraintsAllowable(a, dep); err != nil {
return err
}
if err := s.checkDepsDisallowsSelected(a, dep); err != nil {
return err
}
if err := s.checkPackageImportsFromDepExist(a, dep); err != nil {
return err
}
// TODO add check that fails if adding this atom would create a loop
}
return nil
}
// checkPackages performs all constraint checks new packages being added to an
// already-selected project. It determines if selecting the packages would
// result in a state where all solver requirements are still satisfied.
func (s *solver) checkPackage(a atomWithPackages) error {
if nilpa == a.a {
// This shouldn't be able to happen, but if it does, it unequivocally
// indicates a logical bug somewhere, so blowing up is preferable
panic("canary - checking version of empty ProjectAtom")
}
// The base atom was already validated, so we can skip the
// checkAtomAllowable step.
deps, err := s.getImportsAndConstraintsOf(a)
if err != nil {
// An err here would be from the package fetcher; pass it straight back
return err
}
for _, dep := range deps {
if err := s.checkIdentMatches(a, dep); err != nil {
return err
}
if err := s.checkDepsConstraintsAllowable(a, dep); err != nil {
return err
}
if err := s.checkDepsDisallowsSelected(a, dep); err != nil {
return err
}
if err := s.checkPackageImportsFromDepExist(a, dep); err != nil {
return err
}
}
return nil
}
// checkAtomAllowable ensures that an atom itself is acceptable with respect to
// the constraints established by the current solution.
func (s *solver) checkAtomAllowable(pa atom) error {
constraint := s.sel.getConstraint(pa.id)
if s.b.matches(pa.id, constraint, pa.v) {
return nil
}
// TODO collect constraint failure reason (wait...aren't we, below?)
deps := s.sel.getDependenciesOn(pa.id)
var failparent []dependency
for _, dep := range deps {
if !s.b.matches(pa.id, dep.dep.Constraint, pa.v) {
s.fail(dep.depender.id)
failparent = append(failparent, dep)
}
}
err := &versionNotAllowedFailure{
goal: pa,
failparent: failparent,
c: constraint,
}
s.logSolve(err)
return err
}
// checkRequiredPackagesExist ensures that all required packages enumerated by
// existing dependencies on this atom are actually present in the atom.
func (s *solver) checkRequiredPackagesExist(a atomWithPackages) error {
ptree, err := s.b.listPackages(a.a.id, a.a.v)
if err != nil {
// TODO handle this more gracefully
return err
}
deps := s.sel.getDependenciesOn(a.a.id)
fp := make(map[string]errDeppers)
// We inspect these in a bit of a roundabout way, in order to incrementally
// build up the failure we'd return if there is, indeed, a missing package.
// TODO rechecking all of these every time is wasteful. Is there a shortcut?
for _, dep := range deps {
for _, pkg := range dep.dep.pl {
if errdep, seen := fp[pkg]; seen {
errdep.deppers = append(errdep.deppers, dep.depender)
fp[pkg] = errdep
} else {
perr, has := ptree.Packages[pkg]
if !has || perr.Err != nil {
fp[pkg] = errDeppers{
err: perr.Err,
deppers: []atom{dep.depender},
}
}
}
}
}
if len(fp) > 0 {
e := &checkeeHasProblemPackagesFailure{
goal: a.a,
failpkg: fp,
}
s.logSolve(e)
return e
}
return nil
}
// checkDepsConstraintsAllowable checks that the constraints of an atom on a
// given dep are valid with respect to existing constraints.
func (s *solver) checkDepsConstraintsAllowable(a atomWithPackages, cdep completeDep) error {
dep := cdep.ProjectDep
constraint := s.sel.getConstraint(dep.Ident)
// Ensure the constraint expressed by the dep has at least some possible
// intersection with the intersection of existing constraints.
if s.b.matchesAny(dep.Ident, constraint, dep.Constraint) {
return nil
}
siblings := s.sel.getDependenciesOn(dep.Ident)
// No admissible versions - visit all siblings and identify the disagreement(s)
var failsib []dependency
var nofailsib []dependency
for _, sibling := range siblings {
if !s.b.matchesAny(dep.Ident, sibling.dep.Constraint, dep.Constraint) {
s.fail(sibling.depender.id)
failsib = append(failsib, sibling)
} else {
nofailsib = append(nofailsib, sibling)
}
}
err := &disjointConstraintFailure{
goal: dependency{depender: a.a, dep: cdep},
failsib: failsib,
nofailsib: nofailsib,
c: constraint,
}
s.logSolve(err)
return err
}
// checkDepsDisallowsSelected ensures that an atom's constraints on a particular
// dep are not incompatible with the version of that dep that's already been
// selected.
func (s *solver) checkDepsDisallowsSelected(a atomWithPackages, cdep completeDep) error {
dep := cdep.ProjectDep
selected, exists := s.sel.selected(dep.Ident)
if exists && !s.b.matches(dep.Ident, dep.Constraint, selected.a.v) {
s.fail(dep.Ident)
err := &constraintNotAllowedFailure{
goal: dependency{depender: a.a, dep: cdep},
v: selected.a.v,
}
s.logSolve(err)
return err
}
return nil
}
// checkIdentMatches ensures that the LocalName of a dep introduced by an atom,
// has the same NetworkName as what's already been selected (assuming anything's
// been selected).
//
// In other words, this ensures that the solver never simultaneously selects two
// identifiers with the same local name, but that disagree about where their
// network source is.
func (s *solver) checkIdentMatches(a atomWithPackages, cdep completeDep) error {
dep := cdep.ProjectDep
if cur, exists := s.names[dep.Ident.LocalName]; exists {
if cur != dep.Ident.netName() {
deps := s.sel.getDependenciesOn(a.a.id)
// Fail all the other deps, as there's no way atom can ever be
// compatible with them
for _, d := range deps {
s.fail(d.depender.id)
}
err := &sourceMismatchFailure{
shared: dep.Ident.LocalName,
sel: deps,
current: cur,
mismatch: dep.Ident.netName(),
prob: a.a,
}
s.logSolve(err)
return err
}
}
return nil
}
// checkPackageImportsFromDepExist ensures that, if the dep is already selected,
// the newly-required set of packages being placed on it exist and are valid.
func (s *solver) checkPackageImportsFromDepExist(a atomWithPackages, cdep completeDep) error {
sel, is := s.sel.selected(cdep.ProjectDep.Ident)
if !is {
// dep is not already selected; nothing to do
return nil
}
ptree, err := s.b.listPackages(sel.a.id, sel.a.v)
if err != nil {
// TODO handle this more gracefully
return err
}
e := &depHasProblemPackagesFailure{
goal: dependency{
depender: a.a,
dep: cdep,
},
v: sel.a.v,
prob: make(map[string]error),
}
for _, pkg := range cdep.pl {
perr, has := ptree.Packages[pkg]
if !has || perr.Err != nil {
e.pl = append(e.pl, pkg)
if has {
e.prob[pkg] = perr.Err
}
}
}
if len(e.pl) > 0 {
s.logSolve(e)
return e
}
return nil
}