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

Commit

Permalink
Merge pull request #1230 from sdboyer/context-plumbing
Browse files Browse the repository at this point in the history
Plumb context through solver
  • Loading branch information
sdboyer authored Oct 27, 2017
2 parents aedd4f1 + 2ade7e7 commit a836fed
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 76 deletions.
16 changes: 7 additions & 9 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"bytes"
"context"
"flag"
"fmt"
"go/build"
Expand Down Expand Up @@ -277,10 +278,9 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
return errors.New("Gopkg.lock was not up to date")
}

solution, err := solver.Solve()
solution, err := solver.Solve(context.TODO())
if err != nil {
handleAllTheFailuresOfTheWorld(err)
return errors.Wrap(err, "ensure Solve()")
return handleAllTheFailuresOfTheWorld(err)
}

vendorBehavior := dep.VendorOnChanged
Expand Down Expand Up @@ -369,13 +369,12 @@ func (cmd *ensureCommand) runUpdate(ctx *dep.Ctx, args []string, p *dep.Project,
if err != nil {
return errors.Wrap(err, "fastpath solver prepare")
}
solution, err := solver.Solve()
solution, err := solver.Solve(context.TODO())
if err != nil {
// TODO(sdboyer) special handling for warning cases as described in spec
// - e.g., named projects did not upgrade even though newer versions
// were available.
handleAllTheFailuresOfTheWorld(err)
return errors.Wrap(err, "ensure Solve()")
return handleAllTheFailuresOfTheWorld(err)
}

sw, err := dep.NewSafeWriter(nil, p.Lock, dep.LockFromSolution(solution), dep.VendorOnChanged)
Expand Down Expand Up @@ -631,11 +630,10 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
if err != nil {
return errors.Wrap(err, "fastpath solver prepare")
}
solution, err := solver.Solve()
solution, err := solver.Solve(context.TODO())
if err != nil {
// TODO(sdboyer) detect if the failure was specifically about some of the -add arguments
handleAllTheFailuresOfTheWorld(err)
return errors.Wrap(err, "ensure Solve()")
return handleAllTheFailuresOfTheWorld(err)
}

// Prep post-actions and feedback from adds.
Expand Down
14 changes: 10 additions & 4 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
"context"
"flag"
"io/ioutil"
"log"
Expand Down Expand Up @@ -183,10 +184,9 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
return errors.Wrap(err, "prepare solver")
}

soln, err := s.Solve()
soln, err := s.Solve(context.TODO())
if err != nil {
handleAllTheFailuresOfTheWorld(err)
return err
return handleAllTheFailuresOfTheWorld(err)
}
p.Lock = dep.LockFromSolution(soln)

Expand Down Expand Up @@ -247,5 +247,11 @@ func getDirectDependencies(sm gps.SourceManager, p *dep.Project) (pkgtree.Packag

// TODO solve failures can be really creative - we need to be similarly creative
// in handling them and informing the user appropriately
func handleAllTheFailuresOfTheWorld(err error) {
func handleAllTheFailuresOfTheWorld(err error) error {
switch errors.Cause(err) {
case context.Canceled, context.DeadlineExceeded, gps.ErrSourceManagerIsReleased:
return nil
}

return errors.Wrap(err, "Solving failure")
}
6 changes: 6 additions & 0 deletions internal/gps/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ type bridge struct {

// Whether to sort version lists for downgrade.
down bool

// The cancellation context provided to the solver. Threading it through the
// various solver methods is needlessly verbose so long as we maintain the
// lifetime guarantees that a solver can only be run once.
// TODO(sdboyer) uncomment this and thread it through SourceManager methods
//ctx context.Context
}

// mkBridge creates a bridge
Expand Down
32 changes: 16 additions & 16 deletions internal/gps/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,57 +818,57 @@ func TestErrAfterRelease(t *testing.T) {
_, err := sm.SourceExists(id)
if err == nil {
t.Errorf("SourceExists did not error after calling Release()")
} else if terr, ok := err.(smIsReleased); !ok {
t.Errorf("SourceExists errored after Release(), but with unexpected error: %T %s", terr, terr.Error())
} else if err != ErrSourceManagerIsReleased {
t.Errorf("SourceExists errored after Release(), but with unexpected error: %T %s", err, err.Error())
}

err = sm.SyncSourceFor(id)
if err == nil {
t.Errorf("SyncSourceFor did not error after calling Release()")
} else if terr, ok := err.(smIsReleased); !ok {
t.Errorf("SyncSourceFor errored after Release(), but with unexpected error: %T %s", terr, terr.Error())
} else if err != ErrSourceManagerIsReleased {
t.Errorf("SyncSourceFor errored after Release(), but with unexpected error: %T %s", err, err.Error())
}

_, err = sm.ListVersions(id)
if err == nil {
t.Errorf("ListVersions did not error after calling Release()")
} else if terr, ok := err.(smIsReleased); !ok {
t.Errorf("ListVersions errored after Release(), but with unexpected error: %T %s", terr, terr.Error())
} else if err != ErrSourceManagerIsReleased {
t.Errorf("ListVersions errored after Release(), but with unexpected error: %T %s", err, err.Error())
}

_, err = sm.RevisionPresentIn(id, "")
if err == nil {
t.Errorf("RevisionPresentIn did not error after calling Release()")
} else if terr, ok := err.(smIsReleased); !ok {
t.Errorf("RevisionPresentIn errored after Release(), but with unexpected error: %T %s", terr, terr.Error())
} else if err != ErrSourceManagerIsReleased {
t.Errorf("RevisionPresentIn errored after Release(), but with unexpected error: %T %s", err, err.Error())
}

_, err = sm.ListPackages(id, nil)
if err == nil {
t.Errorf("ListPackages did not error after calling Release()")
} else if terr, ok := err.(smIsReleased); !ok {
t.Errorf("ListPackages errored after Release(), but with unexpected error: %T %s", terr, terr.Error())
} else if err != ErrSourceManagerIsReleased {
t.Errorf("ListPackages errored after Release(), but with unexpected error: %T %s", err, err.Error())
}

_, _, err = sm.GetManifestAndLock(id, nil, naiveAnalyzer{})
if err == nil {
t.Errorf("GetManifestAndLock did not error after calling Release()")
} else if terr, ok := err.(smIsReleased); !ok {
t.Errorf("GetManifestAndLock errored after Release(), but with unexpected error: %T %s", terr, terr.Error())
} else if err != ErrSourceManagerIsReleased {
t.Errorf("GetManifestAndLock errored after Release(), but with unexpected error: %T %s", err, err.Error())
}

err = sm.ExportProject(context.Background(), id, nil, "")
if err == nil {
t.Errorf("ExportProject did not error after calling Release()")
} else if terr, ok := err.(smIsReleased); !ok {
t.Errorf("ExportProject errored after Release(), but with unexpected error: %T %s", terr, terr.Error())
} else if err != ErrSourceManagerIsReleased {
t.Errorf("ExportProject errored after Release(), but with unexpected error: %T %s", err, err.Error())
}

_, err = sm.DeduceProjectRoot("")
if err == nil {
t.Errorf("DeduceProjectRoot did not error after calling Release()")
} else if terr, ok := err.(smIsReleased); !ok {
t.Errorf("DeduceProjectRoot errored after Release(), but with unexpected error: %T %s", terr, terr.Error())
} else if err != ErrSourceManagerIsReleased {
t.Errorf("DeduceProjectRoot errored after Release(), but with unexpected error: %T %s", err, err.Error())
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/gps/solve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package gps

import (
"bytes"
"context"
"fmt"
"log"
"reflect"
Expand Down Expand Up @@ -35,7 +36,7 @@ func fixSolve(params SolveParameters, sm SourceManager, t *testing.T) (Solution,
return nil, err
}

return s.Solve()
return s.Solve(context.Background())
}

// Test all the basic table fixtures.
Expand Down
Loading

0 comments on commit a836fed

Please sign in to comment.