diff --git a/cmd/dep/gopath_scanner.go b/cmd/dep/gopath_scanner.go index f6ca1d157e..d672a4de04 100644 --- a/cmd/dep/gopath_scanner.go +++ b/cmd/dep/gopath_scanner.go @@ -134,27 +134,6 @@ func (g *gopathScanner) overlay(rootM *dep.Manifest, rootL *dep.Lock) { } } -func (g *gopathScanner) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock) { - // Iterate through the new projects in solved lock and add them to manifest - // if direct deps and log feedback for all the new projects. - for _, x := range l.Projects() { - pr := x.Ident().ProjectRoot - newProject := true - // Check if it's a new project, not in the old lock - for _, y := range g.origL.Projects() { - if pr == y.Ident().ProjectRoot { - newProject = false - } - } - if newProject { - // If it's in notondisk, add to manifest, these are direct dependencies. - if _, ok := g.pd.notondisk[pr]; ok { - m.Constraints[pr] = getProjectPropertiesFromVersion(x.Version()) - } - } - } -} - func trimPathPrefix(p1, p2 string) string { if fs.HasFilepathPrefix(p1, p2) { return p1[len(p2):] diff --git a/cmd/dep/init.go b/cmd/dep/init.go index 823759906e..f3efc8f7a0 100644 --- a/cmd/dep/init.go +++ b/cmd/dep/init.go @@ -30,14 +30,19 @@ disable this behavior. The following external tools are supported: glide. Any dependencies that are not constrained by external configuration use the GOPATH analysis below. -The version of each dependency will reflect the current state of the GOPATH. If -a dependency doesn't exist in the GOPATH, a version will be selected from the -versions available from the upstream source per the following algorithm: +By default, the dependencies are resolved over the network. A version will be +selected from the versions available from the upstream source per the following +algorithm: - Tags conforming to semver (sorted by semver rules) - Default branch(es) (sorted lexicographically) - Non-semver tags (sorted lexicographically) +An alternate mode can be activated by passing -gopath. In this mode, the version +of each dependency will reflect the current state of the GOPATH. If a dependency +doesn't exist in the GOPATH, a version will be selected based on the above +network version selection algorithm. + A Gopkg.toml file will be written with inferred version constraints for all direct dependencies. Gopkg.lock will be written with precise versions, and vendor/ will be populated with the precise versions written to Gopkg.lock. @@ -52,11 +57,13 @@ func (cmd *initCommand) Hidden() bool { return false } func (cmd *initCommand) Register(fs *flag.FlagSet) { fs.BoolVar(&cmd.noExamples, "no-examples", false, "don't include example in Gopkg.toml") fs.BoolVar(&cmd.skipTools, "skip-tools", false, "skip importing configuration from other dependency managers") + fs.BoolVar(&cmd.gopath, "gopath", false, "search in GOPATH for dependencies") } type initCommand struct { noExamples bool skipTools bool + gopath bool } func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { @@ -132,13 +139,17 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { if err != nil { return err } - gs := newGopathScanner(ctx, directDeps, sm) - err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock) - if err != nil { - return err + + if cmd.gopath { + gs := newGopathScanner(ctx, directDeps, sm) + err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock) + if err != nil { + return err + } } rootAnalyzer.skipTools = true // Don't import external config during solve for now + copyLock := *p.Lock // Copy lock before solving. Use this to separate new lock projects from solved lock params := gps.SolveParameters{ RootDir: root, @@ -164,8 +175,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { } p.Lock = dep.LockFromSolution(soln) - rootAnalyzer.FinalizeRootManifestAndLock(p.Manifest, p.Lock) - gs.FinalizeRootManifestAndLock(p.Manifest, p.Lock) + rootAnalyzer.FinalizeRootManifestAndLock(p.Manifest, p.Lock, copyLock) // Run gps.Prepare with appropriate constraint solutions from solve run // to generate the final lock memo. diff --git a/cmd/dep/root_analyzer.go b/cmd/dep/root_analyzer.go index f93af66f24..0658dcb055 100644 --- a/cmd/dep/root_analyzer.go +++ b/cmd/dep/root_analyzer.go @@ -9,6 +9,7 @@ import ( "log" "github.com/golang/dep" + fb "github.com/golang/dep/internal/feedback" "github.com/golang/dep/internal/gps" "github.com/pkg/errors" ) @@ -125,18 +126,37 @@ func (a *rootAnalyzer) DeriveManifestAndLock(dir string, pr gps.ProjectRoot) (gp return gps.SimpleManifest{}, nil, nil } -func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock) { - // Remove dependencies from the manifest that aren't used - for pr := range m.Constraints { - var used bool - for _, y := range l.Projects() { - if pr == y.Ident().ProjectRoot { - used = true - break +func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock, ol dep.Lock) { + // Iterate through the new projects in solved lock and add them to manifest + // if they are direct deps and log feedback for all the new projects. + for _, y := range l.Projects() { + var f *fb.ConstraintFeedback + pr := y.Ident().ProjectRoot + // New constraints: in new lock and dir dep but not in manifest + if _, ok := a.directDeps[string(pr)]; ok { + if _, ok := m.Constraints[pr]; !ok { + pp := getProjectPropertiesFromVersion(y.Version()) + if pp.Constraint != nil { + m.Constraints[pr] = pp + pc := gps.ProjectConstraint{Ident: y.Ident(), Constraint: pp.Constraint} + f = fb.NewConstraintFeedback(pc, fb.DepTypeDirect) + f.LogFeedback(a.ctx.Err) + } + f = fb.NewLockedProjectFeedback(y, fb.DepTypeDirect) + f.LogFeedback(a.ctx.Err) + } + } else { + // New locked projects: in new lock but not in old lock + newProject := true + for _, opl := range ol.Projects() { + if pr == opl.Ident().ProjectRoot { + newProject = false + } + } + if newProject { + f = fb.NewLockedProjectFeedback(y, fb.DepTypeTransitive) + f.LogFeedback(a.ctx.Err) } - } - if !used { - delete(m.Constraints, pr) } } } diff --git a/cmd/dep/testdata/harness_tests/init/case1/testcase.json b/cmd/dep/testdata/harness_tests/init/case1/testcase.json index d2403484b7..ad5663adf4 100644 --- a/cmd/dep/testdata/harness_tests/init/case1/testcase.json +++ b/cmd/dep/testdata/harness_tests/init/case1/testcase.json @@ -1,6 +1,6 @@ { "commands": [ - ["init", "-no-examples", "-skip-tools"] + ["init", "-no-examples", "-skip-tools", "-gopath"] ], "error-expected": "", "gopath-initial": { diff --git a/cmd/dep/testdata/harness_tests/init/case2/testcase.json b/cmd/dep/testdata/harness_tests/init/case2/testcase.json index 9167534f7b..df64687209 100644 --- a/cmd/dep/testdata/harness_tests/init/case2/testcase.json +++ b/cmd/dep/testdata/harness_tests/init/case2/testcase.json @@ -1,6 +1,6 @@ { "commands": [ - ["init", "-no-examples", "-skip-tools"] + ["init", "-no-examples", "-skip-tools", "-gopath"] ], "error-expected": "", "gopath-initial": { diff --git a/cmd/dep/testdata/harness_tests/init/case3/testcase.json b/cmd/dep/testdata/harness_tests/init/case3/testcase.json index d50e7d91cb..04cfb832e7 100644 --- a/cmd/dep/testdata/harness_tests/init/case3/testcase.json +++ b/cmd/dep/testdata/harness_tests/init/case3/testcase.json @@ -1,6 +1,6 @@ { "commands": [ - ["init", "-no-examples", "-skip-tools"] + ["init", "-no-examples", "-skip-tools", "-gopath"] ], "error-expected": "", "gopath-initial": { diff --git a/cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.lock b/cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.lock new file mode 100644 index 0000000000..e076e162c8 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.lock @@ -0,0 +1,21 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/sdboyer/deptest" + packages = ["."] + revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" + version = "v1.0.0" + +[[projects]] + name = "github.com/sdboyer/deptestdos" + packages = ["."] + revision = "5c607206be5decd28e6263ffffdcee067266015e" + version = "v2.0.0" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "a6ba2237d28d125b55fc6c86e94e33363f1dfd880d471118d36d7587398c30b4" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.toml b/cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.toml new file mode 100644 index 0000000000..d57fc4dad0 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.toml @@ -0,0 +1,8 @@ + +[[constraint]] + name = "github.com/sdboyer/deptest" + version = "1.0.0" + +[[constraint]] + name = "github.com/sdboyer/deptestdos" + version = "2.0.0" diff --git a/cmd/dep/testdata/harness_tests/init/case4/initial/foo/bar.go b/cmd/dep/testdata/harness_tests/init/case4/initial/foo/bar.go new file mode 100644 index 0000000000..c1ed69fc65 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/case4/initial/foo/bar.go @@ -0,0 +1,13 @@ +// 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 foo + +import "github.com/sdboyer/deptest" + +func Foo() deptest.Foo { + var y deptest.Foo + + return y +} diff --git a/cmd/dep/testdata/harness_tests/init/case4/initial/main.go b/cmd/dep/testdata/harness_tests/init/case4/initial/main.go new file mode 100644 index 0000000000..83a4dfcd57 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/case4/initial/main.go @@ -0,0 +1,19 @@ +// 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 main + +import ( + "fmt" + + "github.com/golang/notexist/foo" + "github.com/sdboyer/deptestdos" +) + +func main() { + var x deptestdos.Bar + y := foo.FooFunc() + + fmt.Println(x, y) +} diff --git a/cmd/dep/testdata/harness_tests/init/case4/testcase.json b/cmd/dep/testdata/harness_tests/init/case4/testcase.json new file mode 100644 index 0000000000..13cba35aeb --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/case4/testcase.json @@ -0,0 +1,12 @@ +{ + "commands": [ + ["init", "-no-examples"] + ], + "gopath-initial": { + "github.com/sdboyer/deptestdos": "a0196baa11ea047dd65037287451d36b861b00ea" + }, + "vendor-final": [ + "github.com/sdboyer/deptest", + "github.com/sdboyer/deptestdos" + ] +} diff --git a/cmd/dep/testdata/harness_tests/init/glide/case1/testcase.json b/cmd/dep/testdata/harness_tests/init/glide/case1/testcase.json index 85a05ed658..e119765489 100644 --- a/cmd/dep/testdata/harness_tests/init/glide/case1/testcase.json +++ b/cmd/dep/testdata/harness_tests/init/glide/case1/testcase.json @@ -1,6 +1,6 @@ { "commands": [ - ["init", "-no-examples"] + ["init", "-no-examples", "-gopath"] ], "error-expected": "", "gopath-initial": { diff --git a/cmd/dep/testdata/harness_tests/init/glide/case2/testcase.json b/cmd/dep/testdata/harness_tests/init/glide/case2/testcase.json index 5c387ed5f7..39c7d874e7 100644 --- a/cmd/dep/testdata/harness_tests/init/glide/case2/testcase.json +++ b/cmd/dep/testdata/harness_tests/init/glide/case2/testcase.json @@ -1,6 +1,6 @@ { "commands": [ - ["init", "-no-examples", "-skip-tools"] + ["init", "-no-examples", "-skip-tools", "-gopath"] ], "error-expected": "", "gopath-initial": {