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

Commit e159914

Browse files
committed
internal/gps: add solver.validateParams() method
Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent da33cb1 commit e159914

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

cmd/dep/ensure.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,19 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
144144

145145
solver, err := gps.Prepare(params, sm)
146146
if err != nil {
147-
if deduceErrs, ok := err.(gps.DeductionFailureErrs); ok {
148-
ctx.Loggers.Err.Println("The following errors occurred while deducing packages:")
149-
for ip, error := range deduceErrs {
150-
ctx.Loggers.Err.Printf(" * \"%s\": %s", ip, error)
151-
}
152-
ctx.Loggers.Err.Println()
153-
154-
return errors.New("could not deduce packages to ensure constraints")
155-
}
156147
return errors.Wrap(err, "ensure Prepare")
157148
}
158149

159150
solution, err := solver.Solve()
160151
if err != nil {
161152
handleAllTheFailuresOfTheWorld(err)
153+
if deduceErrs, ok := err.(gps.DeductionFailureErrs); ok {
154+
ctx.Loggers.Err.Println("The following errors occurred while deducing packages:")
155+
for ip, dErr := range deduceErrs {
156+
ctx.Loggers.Err.Printf(" * \"%s\": %s", ip, dErr)
157+
}
158+
ctx.Loggers.Err.Println()
159+
}
162160
return errors.Wrap(err, "ensure Solve()")
163161
}
164162

internal/gps/solve_failures.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (e badOptsFailure) Error() string {
243243
type DeductionFailureErrs map[string]error
244244

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

249249
type sourceMismatchFailure struct {

internal/gps/solver.go

+37-24
Original file line numberDiff line numberDiff line change
@@ -284,34 +284,17 @@ func Prepare(params SolveParameters, sm SourceManager) (Solver, error) {
284284
params.stdLibFn = paths.IsStandardImportPath
285285
}
286286

287+
// Validate the solver parameters
288+
if err := validateParams(sm, rd, params.stdLibFn); err != nil {
289+
return nil, err
290+
}
291+
287292
s := &solver{
288293
tl: params.TraceLogger,
289294
stdLibFn: params.stdLibFn,
290295
rd: rd,
291296
}
292297

293-
var deducePkgsGroup sync.WaitGroup
294-
deductionErrs := make(DeductionFailureErrs)
295-
296-
checkPkg := func(ip string, sm SourceManager) {
297-
_, err := sm.DeduceProjectRoot(ip)
298-
if err != nil {
299-
deductionErrs[ip] = err
300-
}
301-
deducePkgsGroup.Done()
302-
}
303-
304-
for _, ip := range rd.externalImportList(params.stdLibFn) {
305-
deducePkgsGroup.Add(1)
306-
go checkPkg(ip, sm)
307-
}
308-
309-
deducePkgsGroup.Wait()
310-
311-
if len(deductionErrs) > 0 {
312-
return nil, deductionErrs
313-
}
314-
315298
// Set up the bridge and ensure the root dir is in good, working order
316299
// before doing anything else.
317300
if params.mkBridgeFn == nil {
@@ -414,8 +397,7 @@ func (s *solver) Solve() (Solution, error) {
414397
s.vUnify.mtr = s.mtr
415398

416399
// Prime the queues with the root project
417-
err := s.selectRoot()
418-
if err != nil {
400+
if err := s.selectRoot(); err != nil {
419401
return nil, err
420402
}
421403

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

541+
// validateParams validates the solver parameters to ensure solving can be completed.
542+
func validateParams(sm SourceManager, rd rootdata, stdLibFn func(string) bool) error {
543+
// Ensure that all packages are deducible without issues.
544+
var deducePkgsGroup sync.WaitGroup
545+
deductionErrs := make(DeductionFailureErrs)
546+
var errsMut sync.Mutex
547+
548+
deducePkg := func(ip string, sm SourceManager) {
549+
_, err := sm.DeduceProjectRoot(ip)
550+
if err != nil {
551+
errsMut.Lock()
552+
deductionErrs[ip] = err
553+
errsMut.Unlock()
554+
}
555+
deducePkgsGroup.Done()
556+
}
557+
558+
for _, ip := range rd.externalImportList(stdLibFn) {
559+
deducePkgsGroup.Add(1)
560+
go deducePkg(ip, sm)
561+
}
562+
563+
deducePkgsGroup.Wait()
564+
565+
if len(deductionErrs) > 0 {
566+
return deductionErrs
567+
}
568+
569+
return nil
570+
}
571+
559572
// selectRoot is a specialized selectAtom, used solely to initially
560573
// populate the queues at the beginning of a solve run.
561574
func (s *solver) selectRoot() error {

0 commit comments

Comments
 (0)