Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
dep: Add ineffectual constraints finder and warn
Browse files Browse the repository at this point in the history
Finally fixes #302.
  • Loading branch information
sdboyer committed Jan 16, 2018
1 parent 6fc8e05 commit 6a5fcbd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
ctx.Out.Println(err)
}
}
if ineffs := p.FindIneffectualConstraints(sm); len(ineffs) > 0 {
ctx.Err.Printf("Warning: the following project(s) have [[constraint]] stanzas in %s:\n\n", dep.ManifestName)
for _, ineff := range ineffs {
ctx.Err.Println(" ✗ ", ineff)
}
// TODO(sdboyer) lazy wording, it does not mention ignores at all
ctx.Err.Printf("\nHowever, these projects are not direct dependencies of the current project:\n")
ctx.Err.Printf("they are not imported in any .go files, nor are they in the 'required' list in\n")
ctx.Err.Printf("%s. Dep only applies [[constraint]] rules to direct dependencies, so\n", dep.ManifestName)
ctx.Err.Printf("these rules will have no effect.\n\n")
ctx.Err.Printf("Either or import/require packages from these projects to make them into direct\n")
ctx.Err.Printf("dependencies, or convert the [[constraint]] to an [[override]] to enforce rules\n")
ctx.Err.Printf("on these projects if they are transitive dependencies,\n\n")
}

if cmd.add {
return cmd.runAdd(ctx, args, p, sm, params)
Expand Down
30 changes: 30 additions & 0 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"

"github.com/golang/dep/gps"
"github.com/golang/dep/gps/paths"
Expand Down Expand Up @@ -218,6 +219,35 @@ func (p *Project) GetDirectDependencyNames(sm gps.SourceManager) (map[gps.Projec
return directDeps, nil
}

// FindIneffectualConstraints looks for constraint rules expressed in the
// manifest that will have no effect during solving, as they are specified for
// projects that are not direct dependencies of the Project.
//
// "Direct dependency" here is as implemented by GetDirectDependencyNames() -
// after all "ignored" and "required" rules have been considered.
func (p *Project) FindIneffectualConstraints(sm gps.SourceManager) []gps.ProjectRoot {
if p.Manifest == nil {
return nil
}

dd, err := p.GetDirectDependencyNames(sm)
if err != nil {
return nil
}

var ineff []gps.ProjectRoot
for pr := range p.Manifest.DependencyConstraints() {
if !dd[pr] {
ineff = append(ineff, pr)
}
}

sort.Slice(ineff, func(i, j int) bool {
return ineff[i] < ineff[j]
})
return ineff
}

// BackupVendor looks for existing vendor directory and if it's not empty,
// creates a backup of it to a new directory with the provided suffix.
func BackupVendor(vpath, suffix string) (string, error) {
Expand Down

0 comments on commit 6a5fcbd

Please sign in to comment.