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

Commit 6a5fcbd

Browse files
committed
dep: Add ineffectual constraints finder and warn
Finally fixes #302.
1 parent 6fc8e05 commit 6a5fcbd

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

cmd/dep/ensure.go

+14
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
197197
ctx.Out.Println(err)
198198
}
199199
}
200+
if ineffs := p.FindIneffectualConstraints(sm); len(ineffs) > 0 {
201+
ctx.Err.Printf("Warning: the following project(s) have [[constraint]] stanzas in %s:\n\n", dep.ManifestName)
202+
for _, ineff := range ineffs {
203+
ctx.Err.Println(" ✗ ", ineff)
204+
}
205+
// TODO(sdboyer) lazy wording, it does not mention ignores at all
206+
ctx.Err.Printf("\nHowever, these projects are not direct dependencies of the current project:\n")
207+
ctx.Err.Printf("they are not imported in any .go files, nor are they in the 'required' list in\n")
208+
ctx.Err.Printf("%s. Dep only applies [[constraint]] rules to direct dependencies, so\n", dep.ManifestName)
209+
ctx.Err.Printf("these rules will have no effect.\n\n")
210+
ctx.Err.Printf("Either or import/require packages from these projects to make them into direct\n")
211+
ctx.Err.Printf("dependencies, or convert the [[constraint]] to an [[override]] to enforce rules\n")
212+
ctx.Err.Printf("on these projects if they are transitive dependencies,\n\n")
213+
}
200214

201215
if cmd.add {
202216
return cmd.runAdd(ctx, args, p, sm, params)

project.go

+30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"os"
1010
"path/filepath"
11+
"sort"
1112

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

222+
// FindIneffectualConstraints looks for constraint rules expressed in the
223+
// manifest that will have no effect during solving, as they are specified for
224+
// projects that are not direct dependencies of the Project.
225+
//
226+
// "Direct dependency" here is as implemented by GetDirectDependencyNames() -
227+
// after all "ignored" and "required" rules have been considered.
228+
func (p *Project) FindIneffectualConstraints(sm gps.SourceManager) []gps.ProjectRoot {
229+
if p.Manifest == nil {
230+
return nil
231+
}
232+
233+
dd, err := p.GetDirectDependencyNames(sm)
234+
if err != nil {
235+
return nil
236+
}
237+
238+
var ineff []gps.ProjectRoot
239+
for pr := range p.Manifest.DependencyConstraints() {
240+
if !dd[pr] {
241+
ineff = append(ineff, pr)
242+
}
243+
}
244+
245+
sort.Slice(ineff, func(i, j int) bool {
246+
return ineff[i] < ineff[j]
247+
})
248+
return ineff
249+
}
250+
221251
// BackupVendor looks for existing vendor directory and if it's not empty,
222252
// creates a backup of it to a new directory with the provided suffix.
223253
func BackupVendor(vpath, suffix string) (string, error) {

0 commit comments

Comments
 (0)