From f9d76b4cff9de3bffa703a3780dc8fb2b4d36a7c Mon Sep 17 00:00:00 2001 From: Christian Tinnefeld Date: Sat, 19 May 2018 17:46:47 -0700 Subject: [PATCH 1/2] CollectConstraints from root project as well The collectConstraints() function in status.go incorporates effective constraints from the root projects manifest. Issue #1452. --- cmd/dep/status.go | 33 ++++++++++++++++-- cmd/dep/status_test.go | 77 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 106 insertions(+), 4 deletions(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index 42b6dd32..3904691f 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -984,9 +984,9 @@ type projectConstraint struct { // on a dependency and the projects that apply those constraints. type constraintsCollection map[string][]projectConstraint -// collectConstraints collects constraints declared by all the dependencies. -// It returns constraintsCollection and a slice of errors encountered while -// collecting the constraints, if any. +// collectConstraints collects constraints declared by all the dependencies and +// all effective constraints from the root project. It returns constraintsCollection and +// a slice of errors encountered while collecting the constraints, if any. func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (constraintsCollection, []error) { logger := ctx.Err if !ctx.Verbose { @@ -1068,6 +1068,33 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con } } + // Incorporate constraints set in the manifest of the root project. + if p.Manifest != nil { + + ineffCon := p.FindIneffectualConstraints(sm) + + // Iterate through regular project constraints, append if it is not + // an ineffectual constraint + for pr, pp := range p.Manifest.Constraints { + i := sort.Search(len(ineffCon), func(i int) bool { + return pr == ineffCon[i] + }) + if i < len(ineffCon) && ineffCon[i] == pr { + continue + } + + tempCC := append( + constraintCollection[string(pr)], + projectConstraint{pr, pp.Constraint}, + ) + + // Sort the inner projectConstraint slice by Project string. + // Required for consistent returned value. + sort.Sort(byProject(tempCC)) + constraintCollection[string(pr)] = tempCC + } + } + return constraintCollection, errs } diff --git a/cmd/dep/status_test.go b/cmd/dep/status_test.go index 509a6749..80289f60 100644 --- a/cmd/dep/status_test.go +++ b/cmd/dep/status_test.go @@ -351,6 +351,7 @@ func TestCollectConstraints(t *testing.T) { cases := []struct { name string lock dep.Lock + manifest dep.Manifest wantConstraints constraintsCollection wantErr bool }{ @@ -368,7 +369,7 @@ func TestCollectConstraints(t *testing.T) { wantConstraints: constraintsCollection{}, }, { - name: "with multiple constraints", + name: "with multiple constraints from dependencies", lock: dep.Lock{ P: []gps.LockedProject{ gps.NewLockedProject( @@ -401,6 +402,54 @@ func TestCollectConstraints(t *testing.T) { }, }, }, + { + name: "with multiple constraints from dependencies and root project", + lock: dep.Lock{ + P: []gps.LockedProject{ + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")}, + gps.NewVersion("v1.0.0"), + []string{"."}, + ), + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")}, + gps.NewVersion("v0.1.0"), + []string{"."}, + ), + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-2")}, + gps.NewBranch("master").Pair(gps.Revision("824a8d56a4c6b2f4718824a98cd6d70d3dbd4c3e")), + []string{"."}, + ), + }, + }, + manifest: dep.Manifest{ + Constraints: map[gps.ProjectRoot]gps.ProjectProperties{ + gps.ProjectRoot("github.com/sdboyer/deptest"): { + Constraint: gps.Revision("3f4c3bea144e112a69bbe5d8d01c1b09a544253f"), + }, + }, + Ovr: make(gps.ProjectConstraints), + PruneOptions: gps.CascadingPruneOptions{ + DefaultOptions: gps.PruneNestedVendorDirs, + PerProjectOptions: make(map[gps.ProjectRoot]gps.PruneOptionSet), + }, + }, + wantConstraints: constraintsCollection{ + "github.com/sdboyer/deptestdos": []projectConstraint{ + {"github.com/darkowlzz/deptest-project-2", ver2}, + }, + "github.com/sdboyer/dep-test": []projectConstraint{ + {"github.com/darkowlzz/deptest-project-2", ver1}, + }, + "github.com/sdboyer/deptest": []projectConstraint{ + {"github.com/darkowlzz/deptest-project-1", ver1}, + {"github.com/darkowlzz/deptest-project-2", ver08}, + {"github.com/sdboyer/deptest", + gps.Revision("3f4c3bea144e112a69bbe5d8d01c1b09a544253f")}, + }, + }, + }, { name: "skip projects with invalid versions", lock: dep.Lock{ @@ -444,6 +493,31 @@ func TestCollectConstraints(t *testing.T) { }, }, }, + { + name: "skip ineffective constraint from manifest", + lock: dep.Lock{ + P: []gps.LockedProject{ + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")}, + gps.NewVersion("v1.0.0"), + []string{"."}, + ), + }, + }, + manifest: dep.Manifest{ + Constraints: map[gps.ProjectRoot]gps.ProjectProperties{ + gps.ProjectRoot("github.com/darkowlzz/deptest-project-1"): { + Constraint: ver1, + }, + }, + Ovr: make(gps.ProjectConstraints), + PruneOptions: gps.CascadingPruneOptions{ + DefaultOptions: gps.PruneNestedVendorDirs, + PerProjectOptions: make(map[gps.ProjectRoot]gps.PruneOptionSet), + }, + }, + wantConstraints: constraintsCollection{}, + }, } h := test.NewHelper(t) @@ -474,6 +548,7 @@ func TestCollectConstraints(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { p.Lock = &c.lock + p.Manifest = &c.manifest gotConstraints, err := collectConstraints(ctx, p, sm) if len(err) > 0 && !c.wantErr { t.Fatalf("unexpected errors while collecting constraints: %v", err) From 88d9067857581dc09424a3262d0e5ee25b8c6048 Mon Sep 17 00:00:00 2001 From: Christian Tinnefeld Date: Sun, 3 Jun 2018 17:28:55 -0700 Subject: [PATCH 2/2] CollectConstraints from root project as well The collectConstraints() function in status.go incorporates constraints from the root projects manifest. Issue #1452. --- cmd/dep/status.go | 16 ++++++---------- cmd/dep/status_test.go | 3 +-- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index 3904691f..efe5a91b 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -985,7 +985,7 @@ type projectConstraint struct { type constraintsCollection map[string][]projectConstraint // collectConstraints collects constraints declared by all the dependencies and -// all effective constraints from the root project. It returns constraintsCollection and +// constraints from the root project. It returns constraintsCollection and // a slice of errors encountered while collecting the constraints, if any. func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (constraintsCollection, []error) { logger := ctx.Err @@ -1071,21 +1071,17 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con // Incorporate constraints set in the manifest of the root project. if p.Manifest != nil { - ineffCon := p.FindIneffectualConstraints(sm) - - // Iterate through regular project constraints, append if it is not - // an ineffectual constraint + // Iterate through constraints in the manifest, append if it is a + // direct dependency for pr, pp := range p.Manifest.Constraints { - i := sort.Search(len(ineffCon), func(i int) bool { - return pr == ineffCon[i] - }) - if i < len(ineffCon) && ineffCon[i] == pr { + if _, ok := directDeps[pr]; !ok { continue } + // Mark constraints coming from the manifest as "root" tempCC := append( constraintCollection[string(pr)], - projectConstraint{pr, pp.Constraint}, + projectConstraint{"root", pp.Constraint}, ) // Sort the inner projectConstraint slice by Project string. diff --git a/cmd/dep/status_test.go b/cmd/dep/status_test.go index 80289f60..50efeba4 100644 --- a/cmd/dep/status_test.go +++ b/cmd/dep/status_test.go @@ -445,8 +445,7 @@ func TestCollectConstraints(t *testing.T) { "github.com/sdboyer/deptest": []projectConstraint{ {"github.com/darkowlzz/deptest-project-1", ver1}, {"github.com/darkowlzz/deptest-project-2", ver08}, - {"github.com/sdboyer/deptest", - gps.Revision("3f4c3bea144e112a69bbe5d8d01c1b09a544253f")}, + {"root", gps.Revision("3f4c3bea144e112a69bbe5d8d01c1b09a544253f")}, }, }, },