From b6749133490caf5b580cba1398650a57b08730e0 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 7 Jun 2017 18:31:55 +0530 Subject: [PATCH 1/3] Add -gopath flag to init --- cmd/dep/init.go | 26 ++++++++++++++----- cmd/dep/root_analyzer.go | 21 +++++++++++++++ .../harness_tests/init/case1/testcase.json | 2 +- .../harness_tests/init/case2/testcase.json | 2 +- .../harness_tests/init/case3/testcase.json | 2 +- .../harness_tests/init/case4/final/Gopkg.lock | 21 +++++++++++++++ .../harness_tests/init/case4/final/Gopkg.toml | 8 ++++++ .../init/case4/initial/foo/bar.go | 13 ++++++++++ .../harness_tests/init/case4/initial/main.go | 19 ++++++++++++++ .../harness_tests/init/case4/testcase.json | 12 +++++++++ .../init/glide/case1/testcase.json | 2 +- .../init/glide/case2/testcase.json | 2 +- 12 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.lock create mode 100644 cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.toml create mode 100644 cmd/dep/testdata/harness_tests/init/case4/initial/foo/bar.go create mode 100644 cmd/dep/testdata/harness_tests/init/case4/initial/main.go create mode 100644 cmd/dep/testdata/harness_tests/init/case4/testcase.json diff --git a/cmd/dep/init.go b/cmd/dep/init.go index 823759906e..e3368d97c1 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,10 +139,13 @@ 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 { + err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock) + if err != nil { + return err + } } rootAnalyzer.skipTools = true // Don't import external config during solve for now @@ -165,7 +175,9 @@ 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) + if cmd.gopath { + gs.FinalizeRootManifestAndLock(p.Manifest, p.Lock) + } // 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..0fe5506db9 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" ) @@ -139,6 +140,26 @@ func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock) delete(m.Constraints, pr) } } + // Pick the direct dependencies from the solution lock and add to manifest. + // This is done to fill up the manifest constraints with the dependencies + // solved over the network. + for _, y := range l.Projects() { + var f *fb.ConstraintFeedback + pr := y.Ident().ProjectRoot + if _, ok := a.directDeps[string(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) + } else { + f = fb.NewLockedProjectFeedback(y, fb.DepTypeDirect) + } + } else { + f = fb.NewLockedProjectFeedback(y, fb.DepTypeTransitive) + } + f.LogFeedback(a.ctx.Err) + } } func (a *rootAnalyzer) Info() gps.ProjectAnalyzerInfo { 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": { From 3eb07c3527b3ea12b1c710204cd93322cd94a931 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sat, 17 Jun 2017 19:24:55 +0530 Subject: [PATCH 2/3] Generalize FinalizeManifestAndLock - Removes gopathScanner's FinalizeManifestAndLock() and its usage. - Adds using removeTransitiveDependencies() to remove unused constraints. - Changes rootAnalyzer's FinalizeManifestAndLock to log feedback for new constraints and locked projects only. --- cmd/dep/gopath_scanner.go | 21 ---------------- cmd/dep/init.go | 8 +++--- cmd/dep/root_analyzer.go | 53 ++++++++++++++++++++------------------- 3 files changed, 30 insertions(+), 52 deletions(-) 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 e3368d97c1..f3efc8f7a0 100644 --- a/cmd/dep/init.go +++ b/cmd/dep/init.go @@ -140,8 +140,8 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { return err } - gs := newGopathScanner(ctx, directDeps, sm) if cmd.gopath { + gs := newGopathScanner(ctx, directDeps, sm) err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock) if err != nil { return err @@ -149,6 +149,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { } 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, @@ -174,10 +175,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { } p.Lock = dep.LockFromSolution(soln) - rootAnalyzer.FinalizeRootManifestAndLock(p.Manifest, p.Lock) - if cmd.gopath { - 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 0fe5506db9..fc8b54f0a3 100644 --- a/cmd/dep/root_analyzer.go +++ b/cmd/dep/root_analyzer.go @@ -126,39 +126,40 @@ 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 - } - } - if !used { - delete(m.Constraints, pr) - } - } - // Pick the direct dependencies from the solution lock and add to manifest. - // This is done to fill up the manifest constraints with the dependencies - // solved over the network. +func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock, ol dep.Lock) { + a.removeTransitiveDependencies(m) + + // 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 { - 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) - } else { - f = fb.NewLockedProjectFeedback(y, fb.DepTypeDirect) + 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) + } else { + f = fb.NewLockedProjectFeedback(y, fb.DepTypeDirect) + } + f.LogFeedback(a.ctx.Err) } } else { - f = fb.NewLockedProjectFeedback(y, fb.DepTypeTransitive) + // 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) + } } - f.LogFeedback(a.ctx.Err) } } From 3c34e8fccaac6bdd815b83fa8a96d0be24c653b5 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sat, 17 Jun 2017 23:40:56 +0530 Subject: [PATCH 3/3] Print locked project feedback for all new dir deps - Removes `removeTransitiveDependencies()` from `FinalizeRootManifestAndLock`. - Moves locked project feedback for new direct deps out of conditional clause. --- cmd/dep/root_analyzer.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/dep/root_analyzer.go b/cmd/dep/root_analyzer.go index fc8b54f0a3..0658dcb055 100644 --- a/cmd/dep/root_analyzer.go +++ b/cmd/dep/root_analyzer.go @@ -127,8 +127,6 @@ func (a *rootAnalyzer) DeriveManifestAndLock(dir string, pr gps.ProjectRoot) (gp } func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock, ol dep.Lock) { - a.removeTransitiveDependencies(m) - // 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() { @@ -142,9 +140,9 @@ func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock, m.Constraints[pr] = pp pc := gps.ProjectConstraint{Ident: y.Ident(), Constraint: pp.Constraint} f = fb.NewConstraintFeedback(pc, fb.DepTypeDirect) - } else { - f = fb.NewLockedProjectFeedback(y, fb.DepTypeDirect) + f.LogFeedback(a.ctx.Err) } + f = fb.NewLockedProjectFeedback(y, fb.DepTypeDirect) f.LogFeedback(a.ctx.Err) } } else {