2016-03-15 06:36:42 +03:00
|
|
|
package vsolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"fmt"
|
2016-04-01 07:29:16 +03:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-03-15 06:36:42 +03:00
|
|
|
)
|
|
|
|
|
2016-04-01 07:29:16 +03:00
|
|
|
func NewSolver(sm SourceManager, l *logrus.Logger) Solver {
|
|
|
|
if l == nil {
|
|
|
|
l = logrus.New()
|
|
|
|
}
|
|
|
|
|
2016-03-15 06:36:42 +03:00
|
|
|
return &solver{
|
2016-03-17 18:13:59 +03:00
|
|
|
sm: sm,
|
2016-04-01 07:29:16 +03:00
|
|
|
l: l,
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:34:09 +03:00
|
|
|
// solver is a backtracking-style SAT solver.
|
2016-03-15 06:36:42 +03:00
|
|
|
type solver struct {
|
2016-04-01 07:29:16 +03:00
|
|
|
l *logrus.Logger
|
2016-03-16 23:34:09 +03:00
|
|
|
sm SourceManager
|
2016-03-30 20:51:23 +03:00
|
|
|
latest map[ProjectName]struct{}
|
2016-03-15 06:36:42 +03:00
|
|
|
sel *selection
|
|
|
|
unsel *unselected
|
2016-03-17 19:45:36 +03:00
|
|
|
versions []*versionQueue
|
2016-03-17 18:13:59 +03:00
|
|
|
rp ProjectInfo
|
2016-03-16 19:35:27 +03:00
|
|
|
attempts int
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
func (s *solver) Solve(root ProjectInfo, toUpgrade []ProjectName) Result {
|
2016-03-17 18:13:59 +03:00
|
|
|
// local overrides would need to be handled first.
|
|
|
|
// TODO local overrides! heh
|
|
|
|
s.rp = root
|
2016-03-15 06:36:42 +03:00
|
|
|
|
|
|
|
for _, v := range toUpgrade {
|
|
|
|
s.latest[v] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2016-03-17 18:13:59 +03:00
|
|
|
// Initialize queues
|
|
|
|
s.sel = &selection{
|
2016-03-30 20:51:23 +03:00
|
|
|
deps: make(map[ProjectName][]Dependency),
|
2016-03-17 18:13:59 +03:00
|
|
|
}
|
2016-03-15 06:36:42 +03:00
|
|
|
s.unsel = &unselected{
|
2016-03-30 20:51:23 +03:00
|
|
|
sl: make([]ProjectName, 0),
|
2016-03-15 06:36:42 +03:00
|
|
|
cmp: s.unselectedComparator,
|
|
|
|
}
|
|
|
|
heap.Init(s.unsel)
|
|
|
|
|
2016-03-17 18:13:59 +03:00
|
|
|
// Prime the queues with the root project
|
2016-03-30 20:51:23 +03:00
|
|
|
s.selectVersion(s.rp.pa)
|
2016-03-15 06:36:42 +03:00
|
|
|
|
2016-03-17 18:13:59 +03:00
|
|
|
// Prep is done; actually run the solver
|
|
|
|
var r Result
|
|
|
|
r.Projects, r.SolveFailure = s.solve()
|
|
|
|
return r
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
func (s *solver) solve() ([]ProjectAtom, error) {
|
2016-03-15 06:36:42 +03:00
|
|
|
for {
|
2016-03-16 03:27:19 +03:00
|
|
|
ref, has := s.nextUnselected()
|
2016-04-01 07:29:16 +03:00
|
|
|
|
2016-03-17 18:13:59 +03:00
|
|
|
if !has {
|
2016-03-15 06:36:42 +03:00
|
|
|
// no more packages to select - we're done. bail out
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-04-01 07:29:16 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"attempts": s.attempts,
|
|
|
|
"name": ref,
|
|
|
|
"selcount": len(s.sel.projects),
|
|
|
|
}).Debug("Beginning step in solve loop")
|
|
|
|
}
|
|
|
|
|
2016-03-15 06:36:42 +03:00
|
|
|
queue, err := s.createVersionQueue(ref)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// Err means a failure somewhere down the line; try backtracking.
|
|
|
|
if s.backtrack() {
|
|
|
|
// backtracking succeeded, move to the next unselected ref
|
|
|
|
continue
|
|
|
|
}
|
2016-03-16 23:34:09 +03:00
|
|
|
return nil, err
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
2016-03-16 03:27:19 +03:00
|
|
|
|
2016-03-31 08:25:23 +03:00
|
|
|
if queue.current() == emptyVersion {
|
2016-03-24 04:25:35 +03:00
|
|
|
panic("canary - queue is empty, but flow indicates success")
|
|
|
|
}
|
|
|
|
|
2016-04-01 07:29:16 +03:00
|
|
|
if s.l.Level >= logrus.InfoLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": queue.ref,
|
|
|
|
"version": queue.current().Info,
|
2016-04-01 18:34:52 +03:00
|
|
|
}).Info("Accepted project atom")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-31 08:25:23 +03:00
|
|
|
s.selectVersion(ProjectAtom{
|
|
|
|
Name: queue.ref,
|
|
|
|
Version: queue.current(),
|
|
|
|
})
|
2016-03-16 03:27:19 +03:00
|
|
|
s.versions = append(s.versions, queue)
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
2016-03-16 03:27:19 +03:00
|
|
|
|
2016-03-17 18:13:59 +03:00
|
|
|
// Getting this far means we successfully found a solution
|
2016-03-30 20:51:23 +03:00
|
|
|
var projs []ProjectAtom
|
2016-03-17 18:13:59 +03:00
|
|
|
for _, p := range s.sel.projects {
|
|
|
|
projs = append(projs, p)
|
|
|
|
}
|
|
|
|
return projs, nil
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
func (s *solver) createVersionQueue(ref ProjectName) (*versionQueue, error) {
|
2016-03-15 06:36:42 +03:00
|
|
|
// If on the root package, there's no queue to make
|
2016-03-30 20:51:23 +03:00
|
|
|
if ref == s.rp.Name() {
|
2016-03-17 19:45:36 +03:00
|
|
|
return newVersionQueue(ref, nil, s.sm)
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-04-05 22:19:14 +03:00
|
|
|
exists, err := s.sm.RepoExists(ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
exists, err = s.sm.VendorCodeExists(ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
// Project exists only in vendor (and in some manifest somewhere)
|
|
|
|
// TODO mark this for special handling, somehow?
|
|
|
|
if s.l.Level >= logrus.WarnLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": ref,
|
|
|
|
}).Warn("Code found in vendor for project, but no history was found upstream or in cache")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if s.l.Level >= logrus.WarnLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": ref,
|
|
|
|
}).Warn("Upstream project does not exist")
|
|
|
|
}
|
|
|
|
return nil, newSolveError(fmt.Sprintf("Project '%s' could not be located.", ref), cannotResolve)
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
2016-04-05 22:19:14 +03:00
|
|
|
|
2016-03-15 06:36:42 +03:00
|
|
|
lockv := s.getLockVersionIfValid(ref)
|
|
|
|
|
2016-03-17 19:45:36 +03:00
|
|
|
q, err := newVersionQueue(ref, lockv, s.sm)
|
2016-03-15 20:07:16 +03:00
|
|
|
if err != nil {
|
|
|
|
// TODO this particular err case needs to be improved to be ONLY for cases
|
|
|
|
// where there's absolutely nothing findable about a given project name
|
2016-04-01 07:29:16 +03:00
|
|
|
if s.l.Level >= logrus.WarnLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": ref,
|
|
|
|
"err": err,
|
|
|
|
}).Warn("Failed to create a version queue")
|
|
|
|
}
|
2016-03-15 20:07:16 +03:00
|
|
|
return nil, err
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-04-01 07:29:16 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
|
|
|
if lockv == nil {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
2016-04-02 20:44:46 +03:00
|
|
|
"name": ref,
|
|
|
|
"queue": q,
|
|
|
|
}).Debug("Created versionQueue, but no data in lock for project")
|
2016-04-01 07:29:16 +03:00
|
|
|
} else {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": ref,
|
2016-04-02 20:44:46 +03:00
|
|
|
"queue": q,
|
|
|
|
}).Debug("Created versionQueue using version found in lock")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-15 06:36:42 +03:00
|
|
|
return q, s.findValidVersion(q)
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:32:22 +03:00
|
|
|
// findValidVersion walks through a versionQueue until it finds a version that
|
|
|
|
// satisfies the constraints held in the current state of the solver.
|
2016-03-17 19:45:36 +03:00
|
|
|
func (s *solver) findValidVersion(q *versionQueue) error {
|
2016-03-31 08:25:23 +03:00
|
|
|
if emptyVersion == q.current() {
|
2016-03-15 06:36:42 +03:00
|
|
|
// TODO this case shouldn't be reachable, but panic here as a canary
|
|
|
|
panic("version queue is empty, should not happen")
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:32:22 +03:00
|
|
|
faillen := len(q.fails)
|
|
|
|
|
2016-04-01 07:29:16 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": q.ref,
|
|
|
|
"hasLock": q.hasLock,
|
|
|
|
"allLoaded": q.allLoaded,
|
2016-04-02 20:44:46 +03:00
|
|
|
}).Debug("Beginning search through versionQueue for a valid version")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
2016-03-15 06:36:42 +03:00
|
|
|
for {
|
2016-04-04 22:32:22 +03:00
|
|
|
cur := q.current()
|
|
|
|
err := s.satisfiable(ProjectAtom{
|
2016-03-31 08:25:23 +03:00
|
|
|
Name: q.ref,
|
2016-04-04 22:32:22 +03:00
|
|
|
Version: cur,
|
2016-03-31 08:25:23 +03:00
|
|
|
})
|
2016-03-15 06:36:42 +03:00
|
|
|
if err == nil {
|
|
|
|
// we have a good version, can return safely
|
2016-04-01 07:29:16 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": q.ref,
|
2016-04-04 22:32:22 +03:00
|
|
|
"version": cur.Info,
|
2016-04-01 07:29:16 +03:00
|
|
|
}).Debug("Found acceptable version, returning out")
|
|
|
|
}
|
2016-03-15 06:36:42 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:32:22 +03:00
|
|
|
if q.advance(err) != nil {
|
2016-03-24 04:26:18 +03:00
|
|
|
// Error on advance, have to bail out
|
2016-04-01 07:29:16 +03:00
|
|
|
if s.l.Level >= logrus.WarnLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": q.ref,
|
|
|
|
"err": err,
|
|
|
|
}).Warn("Advancing version queue returned unexpected error, marking project as failed")
|
|
|
|
}
|
2016-03-24 04:26:18 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if q.isExhausted() {
|
|
|
|
// Queue is empty, bail with error
|
2016-04-01 07:29:16 +03:00
|
|
|
if s.l.Level >= logrus.InfoLevel {
|
2016-04-04 22:32:22 +03:00
|
|
|
s.l.WithField("name", q.ref).Info("Version queue was completely exhausted, marking project as failed")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
2016-03-15 20:07:16 +03:00
|
|
|
break
|
|
|
|
}
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
s.fail(s.sel.getDependenciesOn(q.ref)[0].Depender.Name)
|
2016-04-04 22:32:22 +03:00
|
|
|
|
|
|
|
// Return a compound error of all the new errors encountered during this
|
|
|
|
// attempt to find a new, valid version
|
|
|
|
return &noVersionError{
|
|
|
|
pn: q.ref,
|
2016-04-05 06:15:08 +03:00
|
|
|
fails: q.fails[faillen:],
|
2016-04-04 22:32:22 +03:00
|
|
|
}
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
func (s *solver) getLockVersionIfValid(ref ProjectName) *ProjectAtom {
|
|
|
|
lockver := s.rp.GetProjectAtom(ref)
|
2016-03-15 06:36:42 +03:00
|
|
|
if lockver == nil {
|
2016-04-02 20:44:46 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
|
|
|
s.l.WithField("name", ref).Debug("Project not present in lock")
|
|
|
|
}
|
2016-03-15 06:36:42 +03:00
|
|
|
// Nothing in the lock about this version, so nothing to validate
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
constraint := s.sel.getConstraint(ref)
|
2016-03-30 17:39:22 +03:00
|
|
|
if !constraint.Admits(lockver.Version) {
|
2016-04-02 20:44:46 +03:00
|
|
|
if s.l.Level >= logrus.InfoLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": ref,
|
|
|
|
"version": lockver.Version.Info,
|
|
|
|
}).Info("Project found in lock, but version not allowed by current constraints")
|
|
|
|
}
|
2016-03-15 06:36:42 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-02 20:44:46 +03:00
|
|
|
if s.l.Level >= logrus.InfoLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": ref,
|
|
|
|
"version": lockver.Version.Info,
|
|
|
|
}).Info("Project found in lock")
|
|
|
|
}
|
|
|
|
|
|
|
|
return lockver
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-04-02 20:44:46 +03:00
|
|
|
// satisfiable is the main checking method - it determines if introducing a new
|
|
|
|
// project atom would result in a graph where all requirements are still
|
|
|
|
// satisfied.
|
|
|
|
func (s *solver) satisfiable(pi ProjectAtom) error {
|
2016-03-30 20:51:23 +03:00
|
|
|
if emptyProjectAtom == pi {
|
2016-03-15 06:36:42 +03:00
|
|
|
// TODO we should protect against this case elsewhere, but for now panic
|
|
|
|
// to canary when it's a problem
|
2016-03-30 20:51:23 +03:00
|
|
|
panic("checking version of empty ProjectAtom")
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-04-01 07:29:16 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": pi.Name,
|
|
|
|
"version": pi.Version.Info,
|
2016-04-02 20:44:46 +03:00
|
|
|
}).Debug("Checking satisfiability of project atom against current constraints")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
constraint := s.sel.getConstraint(pi.Name)
|
2016-03-30 17:39:22 +03:00
|
|
|
if !constraint.Admits(pi.Version) {
|
2016-04-01 07:29:16 +03:00
|
|
|
// TODO collect constraint failure reason
|
|
|
|
|
|
|
|
if s.l.Level >= logrus.InfoLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
2016-04-01 18:34:52 +03:00
|
|
|
"name": pi.Name,
|
|
|
|
"version": pi.Version.Info,
|
|
|
|
"curconstraint": constraint.Body(),
|
|
|
|
}).Info("Current constraints do not allow version")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
deps := s.sel.getDependenciesOn(pi.Name)
|
2016-04-04 22:32:22 +03:00
|
|
|
var failparent []Dependency
|
2016-03-15 06:36:42 +03:00
|
|
|
for _, dep := range deps {
|
2016-03-30 17:39:22 +03:00
|
|
|
if !dep.Dep.Constraint.Admits(pi.Version) {
|
2016-04-01 18:34:52 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
2016-04-01 07:29:16 +03:00
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": pi.Name,
|
|
|
|
"othername": dep.Depender.Name,
|
2016-04-01 18:34:52 +03:00
|
|
|
"constraint": dep.Dep.Constraint.Body(),
|
|
|
|
}).Debug("Marking other, selected project with conflicting constraint as failed")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
2016-03-30 20:51:23 +03:00
|
|
|
s.fail(dep.Depender.Name)
|
2016-04-04 22:32:22 +03:00
|
|
|
failparent = append(failparent, dep)
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:32:22 +03:00
|
|
|
return &versionNotAllowedFailure{
|
|
|
|
goal: pi,
|
|
|
|
failparent: failparent,
|
|
|
|
c: constraint,
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 18:13:59 +03:00
|
|
|
deps, err := s.getDependenciesOf(pi)
|
2016-03-15 06:36:42 +03:00
|
|
|
if err != nil {
|
|
|
|
// An err here would be from the package fetcher; pass it straight back
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dep := range deps {
|
|
|
|
// TODO dart skips "magic" deps here; do we need that?
|
|
|
|
|
2016-04-01 07:29:16 +03:00
|
|
|
siblings := s.sel.getDependenciesOn(dep.Name)
|
2016-03-15 06:36:42 +03:00
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
constraint = s.sel.getConstraint(dep.Name)
|
2016-03-15 06:36:42 +03:00
|
|
|
// Ensure the constraint expressed by the dep has at least some possible
|
2016-04-01 07:29:16 +03:00
|
|
|
// intersection with the intersection of existing constraints.
|
2016-03-30 17:39:22 +03:00
|
|
|
if !constraint.AdmitsAny(dep.Constraint) {
|
2016-04-01 18:34:52 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
2016-04-01 07:29:16 +03:00
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": pi.Name,
|
|
|
|
"version": pi.Version.Info,
|
|
|
|
"depname": dep.Name,
|
|
|
|
"curconstraint": constraint.Body(),
|
|
|
|
"newconstraint": dep.Constraint.Body(),
|
2016-04-01 18:34:52 +03:00
|
|
|
}).Debug("Project atom cannot be added; its constraints are disjoint with existing constraints")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// No admissible versions - visit all siblings and identify the disagreement(s)
|
2016-04-04 22:32:22 +03:00
|
|
|
var failsib []Dependency
|
|
|
|
var nofailsib []Dependency
|
2016-04-01 07:29:16 +03:00
|
|
|
for _, sibling := range siblings {
|
2016-03-30 17:39:22 +03:00
|
|
|
if !sibling.Dep.Constraint.AdmitsAny(dep.Constraint) {
|
2016-04-01 18:34:52 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
2016-04-01 07:29:16 +03:00
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": pi.Name,
|
|
|
|
"version": pi.Version.Info,
|
|
|
|
"depname": sibling.Depender.Name,
|
|
|
|
"sibconstraint": sibling.Dep.Constraint.Body(),
|
|
|
|
"newconstraint": dep.Constraint.Body(),
|
2016-04-04 22:32:22 +03:00
|
|
|
}).Debug("Marking other, selected project as failed because its constraint is disjoint with our testee")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
2016-03-30 20:51:23 +03:00
|
|
|
s.fail(sibling.Depender.Name)
|
2016-04-04 22:32:22 +03:00
|
|
|
failsib = append(failsib, sibling)
|
|
|
|
} else {
|
|
|
|
nofailsib = append(nofailsib, sibling)
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &disjointConstraintFailure{
|
2016-04-04 22:32:22 +03:00
|
|
|
goal: Dependency{Depender: pi, Dep: dep},
|
|
|
|
failsib: failsib,
|
|
|
|
nofailsib: nofailsib,
|
|
|
|
c: constraint,
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
selected, exists := s.sel.selected(dep.Name)
|
2016-03-30 17:39:22 +03:00
|
|
|
if exists && !dep.Constraint.Admits(selected.Version) {
|
2016-04-01 18:34:52 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
2016-04-01 07:29:16 +03:00
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": pi.Name,
|
|
|
|
"version": pi.Version.Info,
|
|
|
|
"depname": dep.Name,
|
|
|
|
"curversion": selected.Version.Info,
|
|
|
|
"newconstraint": dep.Constraint.Body(),
|
2016-04-02 20:44:46 +03:00
|
|
|
}).Debug("Project atom cannot be added; a constraint it introduces does not allow a currently selected version")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
2016-03-30 20:51:23 +03:00
|
|
|
s.fail(dep.Name)
|
2016-03-15 06:36:42 +03:00
|
|
|
|
2016-04-04 22:32:22 +03:00
|
|
|
return &constraintNotAllowedFailure{
|
|
|
|
goal: Dependency{Depender: pi, Dep: dep},
|
|
|
|
v: selected.Version,
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 20:44:46 +03:00
|
|
|
// TODO add check that fails if adding this atom would create a loop
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-04-01 07:29:16 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": pi.Name,
|
|
|
|
"version": pi.Version.Info,
|
|
|
|
}).Debug("Project atom passed satisfiability test against current state")
|
|
|
|
}
|
|
|
|
|
2016-03-15 06:36:42 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
// getDependenciesOf returns the dependencies of the given ProjectAtom, mediated
|
2016-03-15 06:36:42 +03:00
|
|
|
// through any overrides dictated by the root project.
|
|
|
|
//
|
|
|
|
// If it's the root project, also includes dev dependencies, etc.
|
2016-03-30 20:51:23 +03:00
|
|
|
func (s *solver) getDependenciesOf(pi ProjectAtom) ([]ProjectDep, error) {
|
2016-03-17 06:15:51 +03:00
|
|
|
info, err := s.sm.GetProjectInfo(pi)
|
2016-03-15 06:36:42 +03:00
|
|
|
if err != nil {
|
|
|
|
// TODO revisit this once a decision is made about better-formed errors;
|
|
|
|
// question is, do we expect the fetcher to pass back simple errors, or
|
|
|
|
// well-typed solver errors?
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
deps := info.GetDependencies()
|
2016-03-30 20:51:23 +03:00
|
|
|
if s.rp.Name() == pi.Name {
|
2016-03-15 06:36:42 +03:00
|
|
|
// Root package has more things to pull in
|
2016-03-16 03:27:19 +03:00
|
|
|
deps = append(deps, info.GetDevDependencies()...)
|
2016-03-15 06:36:42 +03:00
|
|
|
|
|
|
|
// TODO add overrides here...if we impl the concept (which we should)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO we have to validate well-formedness of a project's manifest
|
|
|
|
// somewhere. this may be a good spot. alternatively, the fetcher may
|
|
|
|
// validate well-formedness, whereas here we validate availability of the
|
|
|
|
// named deps here. (the latter is sorta what pub does here)
|
|
|
|
|
|
|
|
return deps, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// backtrack works backwards from the current failed solution to find the next
|
|
|
|
// solution to try.
|
|
|
|
func (s *solver) backtrack() bool {
|
|
|
|
if len(s.versions) == 0 {
|
|
|
|
// nothing to backtrack to
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-01 18:34:52 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
2016-04-01 07:29:16 +03:00
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"selcount": len(s.sel.projects),
|
|
|
|
"queuecount": len(s.versions),
|
|
|
|
"attempts": s.attempts,
|
2016-04-01 18:34:52 +03:00
|
|
|
}).Debug("Beginning backtracking")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-15 06:36:42 +03:00
|
|
|
for {
|
|
|
|
for {
|
2016-04-01 07:29:16 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
|
|
|
s.l.WithField("queuecount", len(s.versions)).Debug("Top of search loop for failed queues")
|
|
|
|
}
|
2016-04-01 18:34:52 +03:00
|
|
|
|
2016-03-15 06:36:42 +03:00
|
|
|
if len(s.versions) == 0 {
|
|
|
|
// no more versions, nowhere further to backtrack
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if s.versions[len(s.versions)-1].failed {
|
|
|
|
break
|
|
|
|
}
|
2016-04-01 18:34:52 +03:00
|
|
|
|
|
|
|
if s.l.Level >= logrus.InfoLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": s.versions[len(s.versions)-1].ref,
|
|
|
|
"wasfailed": false,
|
|
|
|
}).Info("Backtracking popped off project")
|
|
|
|
}
|
2016-03-15 06:36:42 +03:00
|
|
|
// pub asserts here that the last in s.sel's ids is == q.current
|
2016-04-01 07:29:35 +03:00
|
|
|
s.versions, s.versions[len(s.versions)-1] = s.versions[:len(s.versions)-1], nil
|
2016-03-16 03:27:19 +03:00
|
|
|
s.unselectLast()
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-04-02 20:44:46 +03:00
|
|
|
// Grab the last versionQueue off the list of queues
|
2016-03-16 19:35:27 +03:00
|
|
|
q := s.versions[len(s.versions)-1]
|
2016-04-01 07:29:16 +03:00
|
|
|
|
2016-04-01 18:34:52 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
2016-04-01 07:29:16 +03:00
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": q.ref,
|
|
|
|
"failver": q.current().Info,
|
2016-04-01 18:34:52 +03:00
|
|
|
}).Debug("Trying failed queue with next version")
|
2016-04-01 07:29:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-15 06:36:42 +03:00
|
|
|
// another assert that the last in s.sel's ids is == q.current
|
2016-03-16 03:27:19 +03:00
|
|
|
s.unselectLast()
|
2016-03-16 19:35:27 +03:00
|
|
|
|
2016-04-01 07:29:35 +03:00
|
|
|
// Advance the queue past the current version, which we know is bad
|
2016-04-04 22:32:22 +03:00
|
|
|
// TODO is it feasible to make available the failure reason here?
|
|
|
|
if q.advance(nil) == nil && !q.isExhausted() {
|
2016-04-01 07:29:35 +03:00
|
|
|
// Search for another acceptable version of this failed dep in its queue
|
|
|
|
if s.findValidVersion(q) == nil {
|
2016-04-01 18:34:52 +03:00
|
|
|
if s.l.Level >= logrus.InfoLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": q.ref,
|
|
|
|
"version": q.current().Info,
|
|
|
|
}).Info("Backtracking found valid version, attempting next solution")
|
|
|
|
}
|
|
|
|
|
2016-04-01 07:29:35 +03:00
|
|
|
// Found one! Put it back on the selected queue and stop
|
|
|
|
// backtracking
|
|
|
|
s.selectVersion(ProjectAtom{
|
|
|
|
Name: q.ref,
|
|
|
|
Version: q.current(),
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 18:34:52 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
2016-04-01 07:29:35 +03:00
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": q.ref,
|
2016-04-01 18:34:52 +03:00
|
|
|
}).Debug("Failed to find a valid version in queue, continuing backtrack")
|
2016-03-16 19:35:27 +03:00
|
|
|
}
|
|
|
|
|
2016-04-01 07:29:35 +03:00
|
|
|
// No solution found; continue backtracking after popping the queue
|
|
|
|
// we just inspected off the list
|
2016-04-01 18:34:52 +03:00
|
|
|
if s.l.Level >= logrus.InfoLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": s.versions[len(s.versions)-1].ref,
|
|
|
|
"wasfailed": true,
|
|
|
|
}).Info("Backtracking popped off project")
|
|
|
|
}
|
2016-03-16 19:35:27 +03:00
|
|
|
// GC-friendly pop pointer elem in slice
|
|
|
|
s.versions, s.versions[len(s.versions)-1] = s.versions[:len(s.versions)-1], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backtracking was successful if loop ended before running out of versions
|
|
|
|
if len(s.versions) == 0 {
|
|
|
|
return false
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
2016-03-16 19:35:27 +03:00
|
|
|
s.attempts++
|
|
|
|
return true
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
func (s *solver) nextUnselected() (ProjectName, bool) {
|
2016-03-16 03:27:19 +03:00
|
|
|
if len(s.unsel.sl) > 0 {
|
|
|
|
return s.unsel.sl[0], true
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2016-03-15 06:36:42 +03:00
|
|
|
func (s *solver) unselectedComparator(i, j int) bool {
|
|
|
|
iname, jname := s.unsel.sl[i], s.unsel.sl[j]
|
|
|
|
|
|
|
|
if iname == jname {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
rname := s.rp.Name()
|
2016-03-15 06:36:42 +03:00
|
|
|
// *always* put root project first
|
2016-03-30 20:51:23 +03:00
|
|
|
if iname == rname {
|
2016-03-15 06:36:42 +03:00
|
|
|
return true
|
|
|
|
}
|
2016-03-30 20:51:23 +03:00
|
|
|
if jname == rname {
|
2016-03-15 06:36:42 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
ilock, jlock := s.rp.GetProjectAtom(iname) == nil, s.rp.GetProjectAtom(jname) == nil
|
2016-03-15 06:36:42 +03:00
|
|
|
|
|
|
|
if ilock && !jlock {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !ilock && jlock {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
//if ilock && jlock {
|
|
|
|
//return iname < jname
|
|
|
|
//}
|
|
|
|
|
|
|
|
// TODO impl version-counting for next set of checks. but until then...
|
|
|
|
return iname < jname
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
func (s *solver) fail(name ProjectName) {
|
2016-03-16 03:27:19 +03:00
|
|
|
// skip if the root project
|
2016-03-30 20:51:23 +03:00
|
|
|
if s.rp.Name() == name {
|
2016-04-01 23:40:27 +03:00
|
|
|
s.l.Debug("Not marking the root project as failed")
|
2016-03-16 03:27:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vq := range s.versions {
|
2016-03-30 20:51:23 +03:00
|
|
|
if vq.ref == name {
|
2016-03-16 03:27:19 +03:00
|
|
|
vq.failed = true
|
|
|
|
// just look for the first (oldest) one; the backtracker will
|
|
|
|
// necessarily traverse through and pop off any earlier ones
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
func (s *solver) selectVersion(pa ProjectAtom) {
|
|
|
|
s.unsel.remove(pa.Name)
|
|
|
|
s.sel.projects = append(s.sel.projects, pa)
|
2016-03-15 06:36:42 +03:00
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
deps, err := s.getDependenciesOf(pa)
|
2016-03-16 03:27:19 +03:00
|
|
|
if err != nil {
|
|
|
|
// if we're choosing a package that has errors getting its deps, there's
|
|
|
|
// a bigger problem
|
|
|
|
// TODO try to create a test that hits this
|
|
|
|
panic("shouldn't be possible")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dep := range deps {
|
2016-03-30 20:51:23 +03:00
|
|
|
siblingsAndSelf := append(s.sel.getDependenciesOn(dep.Name), Dependency{Depender: pa, Dep: dep})
|
|
|
|
s.sel.deps[dep.Name] = siblingsAndSelf
|
2016-03-16 03:27:19 +03:00
|
|
|
|
|
|
|
// add project to unselected queue if this is the first dep on it -
|
|
|
|
// otherwise it's already in there, or been selected
|
|
|
|
if len(siblingsAndSelf) == 1 {
|
2016-03-30 20:51:23 +03:00
|
|
|
heap.Push(s.unsel, dep.Name)
|
2016-03-16 03:27:19 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|
|
|
|
|
2016-03-16 03:27:19 +03:00
|
|
|
func (s *solver) unselectLast() {
|
2016-03-30 20:51:23 +03:00
|
|
|
var pa ProjectAtom
|
|
|
|
pa, s.sel.projects = s.sel.projects[len(s.sel.projects)-1], s.sel.projects[:len(s.sel.projects)-1]
|
|
|
|
heap.Push(s.unsel, pa.Name)
|
2016-03-15 06:36:42 +03:00
|
|
|
|
2016-03-30 20:51:23 +03:00
|
|
|
deps, err := s.getDependenciesOf(pa)
|
2016-03-16 03:27:19 +03:00
|
|
|
if err != nil {
|
|
|
|
// if we're choosing a package that has errors getting its deps, there's
|
|
|
|
// a bigger problem
|
|
|
|
// TODO try to create a test that hits this
|
|
|
|
panic("shouldn't be possible")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dep := range deps {
|
2016-04-01 07:29:16 +03:00
|
|
|
siblings := s.sel.getDependenciesOn(dep.Name)
|
2016-04-01 23:40:27 +03:00
|
|
|
siblings = siblings[:len(siblings)-1]
|
|
|
|
s.sel.deps[dep.Name] = siblings
|
2016-03-16 03:27:19 +03:00
|
|
|
|
|
|
|
// if no siblings, remove from unselected queue
|
|
|
|
if len(siblings) == 0 {
|
2016-04-01 23:40:27 +03:00
|
|
|
if s.l.Level >= logrus.DebugLevel {
|
|
|
|
s.l.WithFields(logrus.Fields{
|
|
|
|
"name": dep.Name,
|
|
|
|
"pname": pa.Name,
|
|
|
|
"pver": pa.Version.Info,
|
|
|
|
}).Debug("Removing project from unselected queue; last parent atom was unselected")
|
|
|
|
}
|
2016-03-30 20:51:23 +03:00
|
|
|
s.unsel.remove(dep.Name)
|
2016-03-16 03:27:19 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-15 06:36:42 +03:00
|
|
|
}
|