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

Commit a961d76

Browse files
committed
internal/gps: ensure packages are deducible before attempting to solve
Add gps.ValidateParams to ensure all packages in SolverParams are deducible. Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent 24c37f5 commit a961d76

File tree

4 files changed

+122
-3
lines changed

4 files changed

+122
-3
lines changed

cmd/dep/ensure.go

+12
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
142142
}
143143
}
144144

145+
err = gps.ValidateParams(params, sm)
146+
if err != nil {
147+
if deduceErrs, ok := err.(gps.DeductionErrs); ok {
148+
ctx.Err.Println("The following errors occurred while deducing packages:")
149+
for ip, dErr := range deduceErrs {
150+
ctx.Err.Printf(" * \"%s\": %s", ip, dErr)
151+
}
152+
ctx.Err.Println()
153+
}
154+
return errors.Wrap(err, "validateParams")
155+
}
156+
145157
solver, err := gps.Prepare(params, sm)
146158
if err != nil {
147159
return errors.Wrap(err, "ensure Prepare")

internal/gps/hash_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,8 @@ func TestHashInputsOverrides(t *testing.T) {
528528

529529
s, err := Prepare(params, newdepspecSM(basefix.ds, nil))
530530
if err != nil {
531-
t.Fatalf("(fix: %q) Unexpected error while prepping solver: %s", fix.name, err)
531+
t.Errorf("(fix: %q) Unexpected error while prepping solver: %s", fix.name, err)
532+
continue
532533
}
533534

534535
h := sha256.New()

internal/gps/solver.go

+46-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"log"
1111
"sort"
1212
"strings"
13+
"sync"
1314

1415
"github.com/armon/go-radix"
1516
"github.com/golang/dep/internal/gps/paths"
@@ -381,6 +382,50 @@ func (s *solver) Version() int {
381382
return 1
382383
}
383384

385+
// DeductionErrs maps package import path to errors occurring during deduction.
386+
type DeductionErrs map[string]error
387+
388+
func (e DeductionErrs) Error() string {
389+
return "could not deduce external imports' project roots"
390+
}
391+
392+
// ValidateParams validates the solver parameters to ensure solving can be completed.
393+
func ValidateParams(params SolveParameters, sm SourceManager) error {
394+
// Ensure that all packages are deducible without issues.
395+
var deducePkgsGroup sync.WaitGroup
396+
deductionErrs := make(DeductionErrs)
397+
var errsMut sync.Mutex
398+
399+
rd, err := params.toRootdata()
400+
if err != nil {
401+
return err
402+
}
403+
404+
deducePkg := func(ip string, sm SourceManager) {
405+
fmt.Println(ip)
406+
_, err := sm.DeduceProjectRoot(ip)
407+
if err != nil {
408+
errsMut.Lock()
409+
deductionErrs[ip] = err
410+
errsMut.Unlock()
411+
}
412+
deducePkgsGroup.Done()
413+
}
414+
415+
for _, ip := range rd.externalImportList(paths.IsStandardImportPath) {
416+
deducePkgsGroup.Add(1)
417+
go deducePkg(ip, sm)
418+
}
419+
420+
deducePkgsGroup.Wait()
421+
422+
if len(deductionErrs) > 0 {
423+
return deductionErrs
424+
}
425+
426+
return nil
427+
}
428+
384429
// Solve attempts to find a dependency solution for the given project, as
385430
// represented by the SolveParameters with which this Solver was created.
386431
//
@@ -391,8 +436,7 @@ func (s *solver) Solve() (Solution, error) {
391436
s.vUnify.mtr = s.mtr
392437

393438
// Prime the queues with the root project
394-
err := s.selectRoot()
395-
if err != nil {
439+
if err := s.selectRoot(); err != nil {
396440
return nil, err
397441
}
398442

internal/gps/solver_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gps
6+
7+
import (
8+
"testing"
9+
10+
"github.com/golang/dep/internal/gps/pkgtree"
11+
"github.com/golang/dep/internal/test"
12+
)
13+
14+
func TestValidateParams(t *testing.T) {
15+
h := test.NewHelper(t)
16+
defer h.Cleanup()
17+
18+
cacheDir := "gps-cache"
19+
h.TempDir(cacheDir)
20+
sm, err := NewSourceManager(h.Path(cacheDir))
21+
h.Must(err)
22+
defer sm.Release()
23+
24+
h.TempDir("src")
25+
26+
testcases := []struct {
27+
imports []string
28+
err bool
29+
}{
30+
{[]string{"google.com/non-existing/package"}, true},
31+
{[]string{"google.com/non-existing/package/subpkg"}, true},
32+
{[]string{"github.com/sdboyer/testrepo"}, false},
33+
{[]string{"github.com/sdboyer/testrepo/subpkg"}, false},
34+
}
35+
36+
params := SolveParameters{
37+
ProjectAnalyzer: naiveAnalyzer{},
38+
RootDir: h.Path("src"),
39+
RootPackageTree: pkgtree.PackageTree{
40+
ImportRoot: "github.com/sdboyer/dep",
41+
},
42+
}
43+
44+
for _, tc := range testcases {
45+
params.RootPackageTree.Packages = map[string]pkgtree.PackageOrErr{
46+
"github.com/sdboyer/dep": {
47+
P: pkgtree.Package{
48+
Name: "github.com/sdboyer/dep",
49+
ImportPath: "github.com/sdboyer/dep",
50+
Imports: tc.imports,
51+
},
52+
},
53+
}
54+
55+
err = ValidateParams(params, sm)
56+
if tc.err && err == nil {
57+
t.Fatalf("expected an error when deducing package fails, got none")
58+
} else if !tc.err && err != nil {
59+
t.Fatalf("deducing packges should have succeeded, got err: %#v", err)
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)