Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
internal/gps: add solver.validateParams() method
Browse files Browse the repository at this point in the history
Signed-off-by: Ibrahim AshShohail <ibra.sho@gmail.com>
  • Loading branch information
ibrasho committed Jun 1, 2017
1 parent da33cb1 commit c7a2389
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
16 changes: 7 additions & 9 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,19 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {

solver, err := gps.Prepare(params, sm)
if err != nil {
if deduceErrs, ok := err.(gps.DeductionFailureErrs); ok {
ctx.Loggers.Err.Println("The following errors occurred while deducing packages:")
for ip, error := range deduceErrs {
ctx.Loggers.Err.Printf(" * \"%s\": %s", ip, error)
}
ctx.Loggers.Err.Println()

return errors.New("could not deduce packages to ensure constraints")
}
return errors.Wrap(err, "ensure Prepare")
}

solution, err := solver.Solve()
if err != nil {
handleAllTheFailuresOfTheWorld(err)
if deduceErrs, ok := err.(gps.DeductionFailureErrs); ok {
ctx.Loggers.Err.Println("The following errors occurred while deducing packages:")
for ip, dErr := range deduceErrs {
ctx.Loggers.Err.Printf(" * \"%s\": %s", ip, dErr)
}
ctx.Loggers.Err.Println()
}
return errors.Wrap(err, "ensure Solve()")
}

Expand Down
2 changes: 1 addition & 1 deletion internal/gps/solve_failures.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (e badOptsFailure) Error() string {
type DeductionFailureErrs map[string]error

func (e DeductionFailureErrs) Error() string {
return fmt.Sprintf("%v", e)
return "could not deduce packages to ensure constraints are solvable"
}

type sourceMismatchFailure struct {
Expand Down
61 changes: 37 additions & 24 deletions internal/gps/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,28 +290,6 @@ func Prepare(params SolveParameters, sm SourceManager) (Solver, error) {
rd: rd,
}

var deducePkgsGroup sync.WaitGroup
deductionErrs := make(DeductionFailureErrs)

checkPkg := func(ip string, sm SourceManager) {
_, err := sm.DeduceProjectRoot(ip)
if err != nil {
deductionErrs[ip] = err
}
deducePkgsGroup.Done()
}

for _, ip := range rd.externalImportList(params.stdLibFn) {
deducePkgsGroup.Add(1)
go checkPkg(ip, sm)
}

deducePkgsGroup.Wait()

if len(deductionErrs) > 0 {
return nil, deductionErrs
}

// Set up the bridge and ensure the root dir is in good, working order
// before doing anything else.
if params.mkBridgeFn == nil {
Expand Down Expand Up @@ -413,9 +391,13 @@ func (s *solver) Solve() (Solution, error) {
s.mtr = newMetrics()
s.vUnify.mtr = s.mtr

// Validate the solver parameters
if err := s.validateParams(); err != nil {
return nil, err
}

// Prime the queues with the root project
err := s.selectRoot()
if err != nil {
if err := s.selectRoot(); err != nil {
return nil, err
}

Expand Down Expand Up @@ -556,6 +538,37 @@ func (s *solver) solve() (map[atom]map[string]struct{}, error) {
return projs, nil
}

// validateParams validates the solver parameters to ensure solving can be completed.
func (s *solver) validateParams() error {
// Ensure that all packages are deducible without issues.
var deducePkgsGroup sync.WaitGroup
deductionErrs := make(DeductionFailureErrs)
var errsMut sync.Mutex

deducePkg := func(ip string, s *solver) {
_, err := s.b.DeduceProjectRoot(ip)
if err != nil {
errsMut.Lock()
deductionErrs[ip] = err
errsMut.Unlock()
}
deducePkgsGroup.Done()
}

for _, ip := range s.rd.externalImportList(s.stdLibFn) {
deducePkgsGroup.Add(1)
go deducePkg(ip, s)
}

deducePkgsGroup.Wait()

if len(deductionErrs) > 0 {
return deductionErrs
}

return nil
}

// selectRoot is a specialized selectAtom, used solely to initially
// populate the queues at the beginning of a solve run.
func (s *solver) selectRoot() error {
Expand Down

0 comments on commit c7a2389

Please sign in to comment.