Skip to content

Commit

Permalink
cmd/dep: wrap errors returned from init
Browse files Browse the repository at this point in the history
In some places, we assume that the error returned
from another call is already wrapped with a clear error message.
These extra wraps make it clear that init failed.
  • Loading branch information
carolynvs committed Nov 14, 2017
1 parent 4ecb1d1 commit a3ed011
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
39 changes: 20 additions & 19 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
root = filepath.Join(ctx.WorkingDir, args[0])
}
if err := os.MkdirAll(root, os.FileMode(0777)); err != nil {
return errors.Wrapf(err, "unable to create directory %s", root)
return errors.Wrapf(err, "init failed: unable to create a directory at %s", root)
}
}

var err error
p := new(dep.Project)
if err = p.SetRoot(root); err != nil {
return errors.Wrap(err, "NewProject")
return errors.Wrapf(err, "init failed: unable to set the root project to %s", root)
}

ctx.GOPATH, err = ctx.DetectProjectGOPATH(p)
if err != nil {
return errors.Wrapf(err, "ctx.DetectProjectGOPATH")
return errors.Wrapf(err, "init failed: unable to detect the containing GOPATH")
}

mf := filepath.Join(root, dep.ManifestName)
Expand All @@ -106,30 +106,30 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {

mok, err := fs.IsRegular(mf)
if err != nil {
return err
return errors.Wrapf(err, "init failed: unable to check for an existing manifest at %s", mf)
}
if mok {
return errors.Errorf("manifest already exists: %s", mf)
return errors.Errorf("init aborted: manifest already exists at %s", mf)
}
// Manifest file does not exist.

lok, err := fs.IsRegular(lf)
if err != nil {
return err
return errors.Wrapf(err, "init failed: unable to check for an existing lock at %s", lf)
}
if lok {
return errors.Errorf("invalid state: manifest %q does not exist, but lock %q does", mf, lf)
return errors.Errorf("invalid aborted: lock already exists at %s", lf)
}

ip, err := ctx.ImportForAbs(root)
if err != nil {
return errors.Wrap(err, "root project import")
return errors.Wrapf(err, "init failed: unable to determine the import path for the root project %s", root)
}
p.ImportRoot = gps.ProjectRoot(ip)

sm, err := ctx.SourceManager()
if err != nil {
return errors.Wrap(err, "getSourceManager")
return errors.Wrap(err, "init failed: unable to create a source manager")
}
sm.UseDefaultSignalHandling()
defer sm.Release()
Expand All @@ -139,7 +139,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
}
pkgT, directDeps, err := getDirectDependencies(sm, p)
if err != nil {
return err
return errors.Wrap(err, "init failed: unable to determine direct dependencies")
}
if ctx.Verbose {
ctx.Out.Printf("Checked %d directories for packages.\nFound %d direct dependencies.\n", len(pkgT.Packages), len(directDeps))
Expand All @@ -149,14 +149,14 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
rootAnalyzer := newRootAnalyzer(cmd.skipTools, ctx, directDeps, sm)
p.Manifest, p.Lock, err = rootAnalyzer.InitializeRootManifestAndLock(root, p.ImportRoot)
if err != nil {
return err
return errors.Wrap(err, "init failed: unable to prepare an initial manifest and lock for the solver")
}

if cmd.gopath {
gs := newGopathScanner(ctx, directDeps, sm)
err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock)
if err != nil {
return err
return errors.Wrap(err, "init failed: unable to scan the GOPATH for dependencies")
}
}

Expand All @@ -176,17 +176,18 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
}

if err := ctx.ValidateParams(sm, params); err != nil {
return err
return errors.Wrapf(err, "init failed: validation of solve parameters failed")
}

s, err := gps.Prepare(params, sm)
if err != nil {
return errors.Wrap(err, "prepare solver")
return errors.Wrap(err, "init failed: unable to prepare the solver")
}

soln, err := s.Solve(context.TODO())
if err != nil {
return handleAllTheFailuresOfTheWorld(err)
err = handleAllTheFailuresOfTheWorld(err)
return errors.Wrap(err, "init failed: unable to solve the dependency graph")
}
p.Lock = dep.LockFromSolution(soln)

Expand All @@ -196,31 +197,31 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
// to generate the final lock memo.
s, err = gps.Prepare(params, sm)
if err != nil {
return errors.Wrap(err, "prepare solver")
return errors.Wrap(err, "init failed: unable to recalculate the lock digest")
}

p.Lock.SolveMeta.InputsDigest = s.HashInputs()

// Pass timestamp (yyyyMMddHHmmss format) as suffix to backup name.
vendorbak, err := dep.BackupVendor(vpath, time.Now().Format("20060102150405"))
if err != nil {
return err
return errors.Wrap(err, "init failed: first backup vendor/, delete it, and then retry the previous command: failed to backup existing vendor directory")
}
if vendorbak != "" {
ctx.Err.Printf("Old vendor backed up to %v", vendorbak)
}

sw, err := dep.NewSafeWriter(p.Manifest, nil, p.Lock, dep.VendorAlways)
if err != nil {
return err
return errors.Wrap(err, "init failed: unable to create a SafeWriter")
}

logger := ctx.Err
if !ctx.Verbose {
logger = log.New(ioutil.Discard, "", 0)
}
if err := sw.Write(root, sm, !cmd.noExamples, logger); err != nil {
return errors.Wrap(err, "safe write of manifest and lock")
return errors.Wrap(err, "init failed: unable to write the manifest, lock and vendor directory to disk")
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"commands": [
["init"]
],
"error-expected": "manifest already exists:",
"error-expected": "init aborted: manifest already exists",
"vendor-final": []
}
2 changes: 1 addition & 1 deletion gps/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func PruneProject(baseDir string, lp LockedProject, options PruneOptions, logger

if (options & PruneNestedVendorDirs) != 0 {
if err := pruneNestedVendorDirs(projectDir); err != nil {
return err
return errors.Wrapf(err, "failed to prune nested vendor directories")
}
}

Expand Down

0 comments on commit a3ed011

Please sign in to comment.