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

Commit

Permalink
internal/gps: ensure packages are deducible before attempting to solve
Browse files Browse the repository at this point in the history
Add gps.ValidateParams to ensure all packages in SolverParams are
deducible.

Signed-off-by: Ibrahim AshShohail <ibra.sho@gmail.com>
  • Loading branch information
ibrasho committed Jul 16, 2017
1 parent 24c37f5 commit a961d76
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 3 deletions.
12 changes: 12 additions & 0 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
}
}

err = gps.ValidateParams(params, sm)
if err != nil {
if deduceErrs, ok := err.(gps.DeductionErrs); ok {
ctx.Err.Println("The following errors occurred while deducing packages:")
for ip, dErr := range deduceErrs {
ctx.Err.Printf(" * \"%s\": %s", ip, dErr)
}
ctx.Err.Println()
}
return errors.Wrap(err, "validateParams")
}

solver, err := gps.Prepare(params, sm)
if err != nil {
return errors.Wrap(err, "ensure Prepare")
Expand Down
3 changes: 2 additions & 1 deletion internal/gps/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,8 @@ func TestHashInputsOverrides(t *testing.T) {

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

h := sha256.New()
Expand Down
48 changes: 46 additions & 2 deletions internal/gps/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"sort"
"strings"
"sync"

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

// DeductionErrs maps package import path to errors occurring during deduction.
type DeductionErrs map[string]error

func (e DeductionErrs) Error() string {
return "could not deduce external imports' project roots"
}

// ValidateParams validates the solver parameters to ensure solving can be completed.
func ValidateParams(params SolveParameters, sm SourceManager) error {
// Ensure that all packages are deducible without issues.
var deducePkgsGroup sync.WaitGroup
deductionErrs := make(DeductionErrs)
var errsMut sync.Mutex

rd, err := params.toRootdata()
if err != nil {
return err
}

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

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

deducePkgsGroup.Wait()

if len(deductionErrs) > 0 {
return deductionErrs
}

return nil
}

// Solve attempts to find a dependency solution for the given project, as
// represented by the SolveParameters with which this Solver was created.
//
Expand All @@ -391,8 +436,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
62 changes: 62 additions & 0 deletions internal/gps/solver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gps

import (
"testing"

"github.com/golang/dep/internal/gps/pkgtree"
"github.com/golang/dep/internal/test"
)

func TestValidateParams(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()

cacheDir := "gps-cache"
h.TempDir(cacheDir)
sm, err := NewSourceManager(h.Path(cacheDir))
h.Must(err)
defer sm.Release()

h.TempDir("src")

testcases := []struct {
imports []string
err bool
}{
{[]string{"google.com/non-existing/package"}, true},
{[]string{"google.com/non-existing/package/subpkg"}, true},
{[]string{"github.com/sdboyer/testrepo"}, false},
{[]string{"github.com/sdboyer/testrepo/subpkg"}, false},
}

params := SolveParameters{
ProjectAnalyzer: naiveAnalyzer{},
RootDir: h.Path("src"),
RootPackageTree: pkgtree.PackageTree{
ImportRoot: "github.com/sdboyer/dep",
},
}

for _, tc := range testcases {
params.RootPackageTree.Packages = map[string]pkgtree.PackageOrErr{
"github.com/sdboyer/dep": {
P: pkgtree.Package{
Name: "github.com/sdboyer/dep",
ImportPath: "github.com/sdboyer/dep",
Imports: tc.imports,
},
},
}

err = ValidateParams(params, sm)
if tc.err && err == nil {
t.Fatalf("expected an error when deducing package fails, got none")
} else if !tc.err && err != nil {
t.Fatalf("deducing packges should have succeeded, got err: %#v", err)
}
}
}

0 comments on commit a961d76

Please sign in to comment.