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

Commit 1d73a4a

Browse files
authored
Merge pull request #1271 from sdboyer/pkgtree-hidden-trimming
Trim hidden packages from (root) PackageTrees
2 parents 31dfa52 + ecd7b87 commit 1d73a4a

File tree

21 files changed

+870
-353
lines changed

21 files changed

+870
-353
lines changed

cmd/dep/ensure.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
178178
return cmd.runVendorOnly(ctx, args, p, sm, params)
179179
}
180180

181-
params.RootPackageTree, err = pkgtree.ListPackages(p.ResolvedAbsRoot, string(p.ImportRoot))
181+
params.RootPackageTree, err = p.ParseRootPackageTree()
182182
if err != nil {
183-
return errors.Wrap(err, "ensure ListPackage for project")
183+
return err
184184
}
185185

186-
if fatal, err := checkErrors(params.RootPackageTree.Packages); err != nil {
186+
if fatal, err := checkErrors(params.RootPackageTree.Packages, p.Manifest.IgnoredPackages()); err != nil {
187187
if fatal {
188188
return err
189189
} else if ctx.Verbose {
@@ -761,13 +761,17 @@ func getProjectConstraint(arg string, sm gps.SourceManager) (gps.ProjectConstrai
761761
return gps.ProjectConstraint{Ident: pi, Constraint: c}, arg, nil
762762
}
763763

764-
func checkErrors(m map[string]pkgtree.PackageOrErr) (fatal bool, err error) {
764+
func checkErrors(m map[string]pkgtree.PackageOrErr, ignore *pkgtree.IgnoredRuleset) (fatal bool, err error) {
765765
var (
766766
noGoErrors int
767767
pkgtreeErrors = make(pkgtreeErrs, 0, len(m))
768768
)
769769

770-
for _, poe := range m {
770+
for ip, poe := range m {
771+
if ignore.IsIgnored(ip) {
772+
continue
773+
}
774+
771775
if poe.Err != nil {
772776
switch poe.Err.(type) {
773777
case *build.NoGoError:

cmd/dep/ensure_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestCheckErrors(t *testing.T) {
135135

136136
for _, tc := range tt {
137137
t.Run(tc.name, func(t *testing.T) {
138-
fatal, err := checkErrors(tc.pkgOrErrMap)
138+
fatal, err := checkErrors(tc.pkgOrErrMap, nil)
139139
if tc.fatal != fatal {
140140
t.Fatalf("expected fatal flag to be %T, got %T", tc.fatal, fatal)
141141
}

cmd/dep/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
227227
}
228228

229229
func getDirectDependencies(sm gps.SourceManager, p *dep.Project) (pkgtree.PackageTree, map[string]bool, error) {
230-
pkgT, err := pkgtree.ListPackages(p.ResolvedAbsRoot, string(p.ImportRoot))
230+
pkgT, err := p.ParseRootPackageTree()
231231
if err != nil {
232-
return pkgtree.PackageTree{}, nil, errors.Wrap(err, "gps.ListPackages")
232+
return pkgtree.PackageTree{}, nil, err
233233
}
234234

235235
directDeps := map[string]bool{}

cmd/dep/status.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ type dotOutput struct {
171171
func (out *dotOutput) BasicHeader() {
172172
out.g = new(graphviz).New()
173173

174-
ptree, _ := pkgtree.ListPackages(out.p.ResolvedAbsRoot, string(out.p.ImportRoot))
174+
ptree, _ := out.p.ParseRootPackageTree()
175+
// TODO(sdboyer) should be true, true, false, out.p.Manifest.IgnoredPackages()
175176
prm, _ := ptree.ToReachMap(true, false, false, nil)
176177

177178
out.g.createNode(string(out.p.ImportRoot), "", prm.FlattenFn(paths.IsStandardImportPath))
@@ -358,9 +359,9 @@ type MissingStatus struct {
358359
func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceManager) (hasMissingPkgs bool, errCount int, err error) {
359360
// While the network churns on ListVersions() requests, statically analyze
360361
// code from the current project.
361-
ptree, err := pkgtree.ListPackages(p.ResolvedAbsRoot, string(p.ImportRoot))
362+
ptree, err := p.ParseRootPackageTree()
362363
if err != nil {
363-
return false, 0, errors.Wrapf(err, "analysis of local packages failed")
364+
return false, 0, err
364365
}
365366

366367
// Set up a solver in order to check the InputHash.
@@ -437,7 +438,7 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana
437438
errListPkgCh <- err
438439
}
439440

440-
prm, _ := ptr.ToReachMap(true, false, false, nil)
441+
prm, _ := ptr.ToReachMap(true, true, false, p.Manifest.IgnoredPackages())
441442
bs.Children = prm.FlattenFn(paths.IsStandardImportPath)
442443
}
443444

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package onlyfromtests
6+
7+
import (
8+
"sort"
9+
10+
_ "varied/_secondorder"
11+
12+
"github.com/golang/dep/internal/gps"
13+
)
14+
15+
var (
16+
M = sort.Strings
17+
_ = gps.Solve
18+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package onlyfromtests
6+
7+
import (
8+
"os"
9+
"sort"
10+
)
11+
12+
var (
13+
_ = sort.Strings
14+
_ = os.PathSeparator
15+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package frommain
6+
7+
import (
8+
"sort"
9+
10+
"github.com/golang/dep/internal/gps"
11+
)
12+
13+
var (
14+
M = sort.Strings
15+
_ = gps.Solve
16+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package never
6+
7+
import (
8+
"sort"
9+
10+
"github.com/golang/dep/internal/gps"
11+
)
12+
13+
var (
14+
M = sort.Strings
15+
_ = gps.Solve
16+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package secondorder
6+
7+
import "hash"
8+
9+
var (
10+
H = hash.Hash
11+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package always
6+
7+
import _ "varied/.onlyfromtests"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package dotslash
6+
7+
import (
8+
"../github.com/golang/dep/internal/gps"
9+
)
10+
11+
var (
12+
A = gps.Solver
13+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
_ "varied/_frommain"
9+
"varied/simple"
10+
)
11+
12+
var (
13+
_ = simple.S
14+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"net/http"
9+
)
10+
11+
var (
12+
_ = http.Client
13+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package simple
6+
7+
import "varied/simple/testdata"
8+
9+
var (
10+
_ = testdata.H
11+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package simple
6+
7+
import (
8+
"go/parser"
9+
10+
"github.com/golang/dep/internal/gps"
11+
)
12+
13+
var (
14+
_ = parser.ParseFile
15+
S = gps.Prepare
16+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package testdata
6+
7+
import _ "varied/dotdotslash"
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package pkgtree
6+
7+
import (
8+
"sort"
9+
"strings"
10+
11+
"github.com/armon/go-radix"
12+
)
13+
14+
// IgnoredRuleset comprises a set of rules for ignoring import paths. It can
15+
// manage both literal and prefix-wildcard matches.
16+
type IgnoredRuleset struct {
17+
t *radix.Tree
18+
}
19+
20+
// NewIgnoredRuleset processes a set of strings into an IgnoredRuleset. Strings
21+
// that end in "*" are treated as wildcards, where any import path with a
22+
// matching prefix will be ignored. IgnoredRulesets are immutable once created.
23+
//
24+
// Duplicate and redundant (i.e. a literal path that has a prefix of a wildcard
25+
// path) declarations are discarded. Consequently, it is possible that the
26+
// returned IgnoredRuleset may have a smaller Len() than the input slice.
27+
func NewIgnoredRuleset(ig []string) *IgnoredRuleset {
28+
if len(ig) == 0 {
29+
return &IgnoredRuleset{}
30+
}
31+
32+
ir := &IgnoredRuleset{
33+
t: radix.New(),
34+
}
35+
36+
// Sort the list of all the ignores in order to ensure that wildcard
37+
// precedence is recorded correctly in the trie.
38+
sort.Strings(ig)
39+
for _, i := range ig {
40+
// Skip global ignore and empty string.
41+
if i == "*" || i == "" {
42+
continue
43+
}
44+
45+
_, wildi, has := ir.t.LongestPrefix(i)
46+
// We may not always have a value here, but if we do, then it's a bool.
47+
wild, _ := wildi.(bool)
48+
// Check if it's a wildcard ignore.
49+
if strings.HasSuffix(i, "*") {
50+
// Check if it is ineffectual.
51+
if has && wild {
52+
// Skip ineffectual wildcard ignore.
53+
continue
54+
}
55+
// Create the ignore prefix and insert in the radix tree.
56+
ir.t.Insert(i[:len(i)-1], true)
57+
} else if !has || !wild {
58+
ir.t.Insert(i, false)
59+
}
60+
}
61+
62+
if ir.t.Len() == 0 {
63+
ir.t = nil
64+
}
65+
66+
return ir
67+
}
68+
69+
// IsIgnored indicates whether the provided path should be ignored, according to
70+
// the ruleset.
71+
func (ir *IgnoredRuleset) IsIgnored(path string) bool {
72+
if path == "" || ir == nil || ir.t == nil {
73+
return false
74+
}
75+
76+
prefix, wildi, has := ir.t.LongestPrefix(path)
77+
return has && (wildi.(bool) || path == prefix)
78+
}
79+
80+
// Len indicates the number of rules in the ruleset.
81+
func (ir *IgnoredRuleset) Len() int {
82+
if ir == nil || ir.t == nil {
83+
return 0
84+
}
85+
86+
return ir.t.Len()
87+
}
88+
89+
// ToSlice converts the contents of the IgnoredRuleset to a string slice.
90+
//
91+
// This operation is symmetrically dual to NewIgnoredRuleset.
92+
func (ir *IgnoredRuleset) ToSlice() []string {
93+
irlen := ir.Len()
94+
if irlen == 0 {
95+
return nil
96+
}
97+
98+
items := make([]string, 0, irlen)
99+
ir.t.Walk(func(s string, v interface{}) bool {
100+
if s != "" {
101+
if v.(bool) {
102+
items = append(items, s+"*")
103+
} else {
104+
items = append(items, s)
105+
}
106+
}
107+
return false
108+
})
109+
110+
return items
111+
}

0 commit comments

Comments
 (0)