-
Notifications
You must be signed in to change notification settings - Fork 1k
Warn on ineffectual constraints #1534
Changes from 5 commits
5583b68
97b8be8
6fc8e05
6a5fcbd
689880a
2eeefbd
b89aa7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2018 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 main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/golang/dep/gps" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// 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) error { | ||
switch errors.Cause(err) { | ||
case context.Canceled, context.DeadlineExceeded, gps.ErrSourceManagerIsReleased: | ||
return nil | ||
} | ||
|
||
return errors.Wrap(err, "Solving failure") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,11 @@ import ( | |
|
||
"github.com/golang/dep" | ||
"github.com/golang/dep/gps" | ||
"github.com/golang/dep/gps/paths" | ||
"github.com/golang/dep/gps/pkgtree" | ||
"github.com/golang/dep/internal/fs" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const initShortHelp = `Initialize a new project with manifest and lock files` | ||
const initShortHelp = `Set up a new Go project, or migrate an existing one` | ||
const initLongHelp = ` | ||
Initialize the project at filepath root by parsing its dependencies, writing | ||
manifest and lock files, and vendoring the dependencies. If root isn't | ||
|
@@ -89,43 +87,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { | |
} | ||
} | ||
|
||
var err error | ||
p := new(dep.Project) | ||
if err = p.SetRoot(root); err != nil { | ||
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, "init failed: unable to detect the containing GOPATH") | ||
} | ||
|
||
mf := filepath.Join(root, dep.ManifestName) | ||
lf := filepath.Join(root, dep.LockName) | ||
vpath := filepath.Join(root, "vendor") | ||
|
||
mok, err := fs.IsRegular(mf) | ||
if err != nil { | ||
return errors.Wrapf(err, "init failed: unable to check for an existing manifest at %s", mf) | ||
} | ||
if mok { | ||
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 errors.Wrapf(err, "init failed: unable to check for an existing lock at %s", lf) | ||
} | ||
if lok { | ||
return errors.Errorf("invalid aborted: lock already exists at %s", lf) | ||
} | ||
|
||
ip, err := ctx.ImportForAbs(root) | ||
if err != nil { | ||
return errors.Wrapf(err, "init failed: unable to determine the import path for the root project %s", root) | ||
} | ||
p.ImportRoot = gps.ProjectRoot(ip) | ||
p, err := cmd.establishProjectAt(root, ctx) | ||
|
||
sm, err := ctx.SourceManager() | ||
if err != nil { | ||
|
@@ -137,12 +99,15 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { | |
if ctx.Verbose { | ||
ctx.Out.Println("Getting direct dependencies...") | ||
} | ||
pkgT, directDeps, err := getDirectDependencies(sm, p) | ||
|
||
// If this errors, the next call will too; don't bother handling it twice. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems brittle. Since
Edit: If this was the only reason for caching the result internally, then that could be simplified out too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like that nil pointer is from the cache field too: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i waffled over this a bit. it was between that signature, and what i did here - because we absolutely can't create a circumstance where casual use of the API results in hitting the disk twice for data that, at least in dep's model, hasn't changed. however, i didn't particularly like having the extra return value - it felt like exposing a bit too much detail of the underlying behavior. the so yeah, i'll switch it around. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually looking at the code again, i've flipped back. not caching it internally also makes the there are a lot of different possible paths through this code, and orders in which different use cases might call it. we can't reasonably take out the caching. but i suppose that also adding the additional return value doesn't hurt - at minimum, it's one less instance of error handling (which was your original concern). |
||
ptree, _ := p.ParseRootPackageTree() | ||
directDeps, err := p.GetDirectDependencyNames(sm) | ||
if err != nil { | ||
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)) | ||
ctx.Out.Printf("Checked %d directories for packages.\nFound %d direct dependencies.\n", len(ptree.Packages), len(directDeps)) | ||
} | ||
|
||
// Initialize with imported data, then fill in the gaps using the GOPATH | ||
|
@@ -165,7 +130,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { | |
|
||
params := gps.SolveParameters{ | ||
RootDir: root, | ||
RootPackageTree: pkgT, | ||
RootPackageTree: ptree, | ||
Manifest: p.Manifest, | ||
Lock: p.Lock, | ||
ProjectAnalyzer: rootAnalyzer, | ||
|
@@ -203,7 +168,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { | |
p.Lock.SolveMeta.InputsDigest = s.HashInputs() | ||
|
||
// Pass timestamp (yyyyMMddHHmmss format) as suffix to backup name. | ||
vendorbak, err := dep.BackupVendor(vpath, time.Now().Format("20060102150405")) | ||
vendorbak, err := dep.BackupVendor(filepath.Join(root, "vendor"), time.Now().Format("20060102150405")) | ||
if err != nil { | ||
return errors.Wrap(err, "init failed: first backup vendor/, delete it, and then retry the previous command: failed to backup existing vendor directory") | ||
} | ||
|
@@ -227,32 +192,50 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { | |
return nil | ||
} | ||
|
||
func getDirectDependencies(sm gps.SourceManager, p *dep.Project) (pkgtree.PackageTree, map[string]bool, error) { | ||
pkgT, err := p.ParseRootPackageTree() | ||
// establishProjectAt attempts to set up the provided path as the root for the | ||
// project to be created. | ||
// | ||
// It checks for being within a GOPATH, that there is no pre-existing manifest | ||
// and lock, and that we can successfully infer the root import path from | ||
// GOPATH. | ||
// | ||
// If successful, it returns a dep.Project, ready for further use. | ||
func (cmd *initCommand) establishProjectAt(root string, ctx *dep.Ctx) (*dep.Project, error) { | ||
var err error | ||
p := new(dep.Project) | ||
if err = p.SetRoot(root); err != nil { | ||
return nil, errors.Wrapf(err, "init failed: unable to set the root project to %s", root) | ||
} | ||
|
||
ctx.GOPATH, err = ctx.DetectProjectGOPATH(p) | ||
if err != nil { | ||
return pkgtree.PackageTree{}, nil, err | ||
return nil, errors.Wrapf(err, "init failed: unable to detect the containing GOPATH") | ||
} | ||
|
||
directDeps := map[string]bool{} | ||
rm, _ := pkgT.ToReachMap(true, true, false, nil) | ||
for _, ip := range rm.FlattenFn(paths.IsStandardImportPath) { | ||
pr, err := sm.DeduceProjectRoot(ip) | ||
if err != nil { | ||
return pkgtree.PackageTree{}, nil, err | ||
} | ||
directDeps[string(pr)] = true | ||
mf := filepath.Join(root, dep.ManifestName) | ||
lf := filepath.Join(root, dep.LockName) | ||
|
||
mok, err := fs.IsRegular(mf) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "init failed: unable to check for an existing manifest at %s", mf) | ||
} | ||
if mok { | ||
return nil, errors.Errorf("init aborted: manifest already exists at %s", mf) | ||
} | ||
|
||
return pkgT, directDeps, nil | ||
} | ||
lok, err := fs.IsRegular(lf) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "init failed: unable to check for an existing lock at %s", lf) | ||
} | ||
if lok { | ||
return nil, errors.Errorf("invalid aborted: lock already exists at %s", lf) | ||
} | ||
|
||
// 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) error { | ||
switch errors.Cause(err) { | ||
case context.Canceled, context.DeadlineExceeded, gps.ErrSourceManagerIsReleased: | ||
return nil | ||
ip, err := ctx.ImportForAbs(root) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "init failed: unable to determine the import path for the root project %s", root) | ||
} | ||
p.ImportRoot = gps.ProjectRoot(ip) | ||
|
||
return errors.Wrap(err, "Solving failure") | ||
return p, nil | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@carolynvs i noticed this while working on this. do you have any immediate thoughts about bugs we might have as a result of this pseudotype mismatch?