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 e159914
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
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
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
Expand Up @@ -284,34 +284,17 @@ func Prepare(params SolveParameters, sm SourceManager) (Solver, error) {
params.stdLibFn = paths.IsStandardImportPath
}

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

s := &solver{
tl: params.TraceLogger,
stdLibFn: params.stdLibFn,
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 @@ -414,8 +397,7 @@ func (s *solver) Solve() (Solution, error) {
s.vUnify.mtr = s.mtr

// 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 validateParams(sm SourceManager, rd rootdata, stdLibFn func(string) bool) error {
// Ensure that all packages are deducible without issues.
var deducePkgsGroup sync.WaitGroup
deductionErrs := make(DeductionFailureErrs)
var errsMut sync.Mutex

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

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

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 e159914

Please sign in to comment.