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

Commit 6fc8e05

Browse files
committed
dep: Convert to using GetDirectDependencyNames()
Specifically, we need to switch map[string]bool for map[gps.ProjectRoot]bool. Trivial refactor, but it's preferable to do this anyway as this is a situation where gps.ProjectRoot should be used to denote that strings in this map are assumed to be project roots. Case in point, one of the importers was making the assumption that it had packages, not root paths, but it wasn't obvious without the type hinting.
1 parent 97b8be8 commit 6fc8e05

File tree

5 files changed

+25
-39
lines changed

5 files changed

+25
-39
lines changed

cmd/dep/gopath_scanner.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ import (
2424
// It uses its results to fill-in any missing details left by the rootAnalyzer.
2525
type gopathScanner struct {
2626
ctx *dep.Ctx
27-
directDeps map[string]bool
27+
directDeps map[gps.ProjectRoot]bool
2828
sm gps.SourceManager
2929

3030
pd projectData
3131
origM *dep.Manifest
3232
origL *dep.Lock
3333
}
3434

35-
func newGopathScanner(ctx *dep.Ctx, directDeps map[string]bool, sm gps.SourceManager) *gopathScanner {
35+
func newGopathScanner(ctx *dep.Ctx, directDeps map[gps.ProjectRoot]bool, sm gps.SourceManager) *gopathScanner {
3636
return &gopathScanner{
3737
ctx: ctx,
3838
directDeps: directDeps,
@@ -113,7 +113,7 @@ func (g *gopathScanner) overlay(rootM *dep.Manifest, rootL *dep.Lock) {
113113
rootL.P = append(rootL.P, lp)
114114
lockedProjects[pkg] = true
115115

116-
if _, isDirect := g.directDeps[string(pkg)]; !isDirect {
116+
if _, isDirect := g.directDeps[pkg]; !isDirect {
117117
f := fb.NewLockedProjectFeedback(lp, fb.DepTypeTransitive)
118118
f.LogFeedback(g.ctx.Err)
119119
}
@@ -220,7 +220,10 @@ func (g *gopathScanner) scanGopathForDependencies() (projectData, error) {
220220
return projectData{}, nil
221221
}
222222

223-
for ip := range g.directDeps {
223+
for ippr := range g.directDeps {
224+
// TODO(sdboyer) these are not import paths by this point, they've
225+
// already been worked down to project roots.
226+
ip := string(ippr)
224227
pr, err := g.sm.DeduceProjectRoot(ip)
225228
if err != nil {
226229
return projectData{}, errors.Wrap(err, "sm.DeduceProjectRoot")

cmd/dep/init.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ import (
1515

1616
"github.com/golang/dep"
1717
"github.com/golang/dep/gps"
18-
"github.com/golang/dep/gps/paths"
19-
"github.com/golang/dep/gps/pkgtree"
2018
"github.com/golang/dep/internal/fs"
2119
"github.com/pkg/errors"
2220
)
2321

24-
const initShortHelp = `Initialize a new project with manifest and lock files`
22+
const initShortHelp = `Set up a new Go project, or migrate an existing one`
2523
const initLongHelp = `
2624
Initialize the project at filepath root by parsing its dependencies, writing
2725
manifest and lock files, and vendoring the dependencies. If root isn't
@@ -101,12 +99,15 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
10199
if ctx.Verbose {
102100
ctx.Out.Println("Getting direct dependencies...")
103101
}
104-
pkgT, directDeps, err := getDirectDependencies(sm, p)
102+
103+
// If this errors, the next call will too; don't bother handling it twice.
104+
ptree, _ := p.ParseRootPackageTree()
105+
directDeps, err := p.GetDirectDependencyNames(sm)
105106
if err != nil {
106107
return errors.Wrap(err, "init failed: unable to determine direct dependencies")
107108
}
108109
if ctx.Verbose {
109-
ctx.Out.Printf("Checked %d directories for packages.\nFound %d direct dependencies.\n", len(pkgT.Packages), len(directDeps))
110+
ctx.Out.Printf("Checked %d directories for packages.\nFound %d direct dependencies.\n", len(ptree.Packages), len(directDeps))
110111
}
111112

112113
// Initialize with imported data, then fill in the gaps using the GOPATH
@@ -129,7 +130,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
129130

130131
params := gps.SolveParameters{
131132
RootDir: root,
132-
RootPackageTree: pkgT,
133+
RootPackageTree: ptree,
133134
Manifest: p.Manifest,
134135
Lock: p.Lock,
135136
ProjectAnalyzer: rootAnalyzer,
@@ -238,22 +239,3 @@ func (cmd *initCommand) establishProjectAt(root string, ctx *dep.Ctx) (*dep.Proj
238239

239240
return p, nil
240241
}
241-
242-
func getDirectDependencies(sm gps.SourceManager, p *dep.Project) (pkgtree.PackageTree, map[string]bool, error) {
243-
pkgT, err := p.ParseRootPackageTree()
244-
if err != nil {
245-
return pkgtree.PackageTree{}, nil, err
246-
}
247-
248-
directDeps := map[string]bool{}
249-
rm, _ := pkgT.ToReachMap(true, true, false, nil)
250-
for _, ip := range rm.FlattenFn(paths.IsStandardImportPath) {
251-
pr, err := sm.DeduceProjectRoot(ip)
252-
if err != nil {
253-
return pkgtree.PackageTree{}, nil, err
254-
}
255-
directDeps[string(pr)] = true
256-
}
257-
258-
return pkgT, directDeps, nil
259-
}

cmd/dep/root_analyzer.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ type rootAnalyzer struct {
2626
skipTools bool
2727
ctx *dep.Ctx
2828
sm gps.SourceManager
29-
directDeps map[string]bool
29+
directDeps map[gps.ProjectRoot]bool
3030
}
3131

32-
func newRootAnalyzer(skipTools bool, ctx *dep.Ctx, directDeps map[string]bool, sm gps.SourceManager) *rootAnalyzer {
32+
func newRootAnalyzer(skipTools bool, ctx *dep.Ctx, directDeps map[gps.ProjectRoot]bool, sm gps.SourceManager) *rootAnalyzer {
3333
return &rootAnalyzer{
3434
skipTools: skipTools,
3535
ctx: ctx,
@@ -73,7 +73,7 @@ func (a *rootAnalyzer) cacheDeps(pr gps.ProjectRoot) error {
7373
return nil
7474
}
7575

76-
deps := make(chan string)
76+
deps := make(chan gps.ProjectRoot)
7777

7878
for i := 0; i < concurrency; i++ {
7979
g.Go(func() error {
@@ -132,7 +132,7 @@ func (a *rootAnalyzer) importManifestAndLock(dir string, pr gps.ProjectRoot, sup
132132

133133
func (a *rootAnalyzer) removeTransitiveDependencies(m *dep.Manifest) {
134134
for pr := range m.Constraints {
135-
if _, isDirect := a.directDeps[string(pr)]; !isDirect {
135+
if _, isDirect := a.directDeps[pr]; !isDirect {
136136
delete(m.Constraints, pr)
137137
}
138138
}
@@ -172,7 +172,7 @@ func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock,
172172
var f *fb.ConstraintFeedback
173173
pr := y.Ident().ProjectRoot
174174
// New constraints: in new lock and dir dep but not in manifest
175-
if _, ok := a.directDeps[string(pr)]; ok {
175+
if _, ok := a.directDeps[pr]; ok {
176176
if _, ok := m.Constraints[pr]; !ok {
177177
pp := getProjectPropertiesFromVersion(y.Version())
178178
if pp.Constraint != nil {

cmd/dep/status.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,9 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con
764764
var mutex sync.Mutex
765765
constraintCollection := make(constraintsCollection)
766766

767-
// Get direct deps of the root project.
768-
_, directDeps, err := getDirectDependencies(sm, p)
767+
// Collect the complete set of direct project dependencies, incorporating
768+
// requireds and ignores appropriately.
769+
directDeps, err := p.GetDirectDependencyNames(sm)
769770
if err != nil {
770771
// Return empty collection, not nil, if we fail here.
771772
return constraintCollection, []error{errors.Wrap(err, "failed to get direct dependencies")}
@@ -805,7 +806,7 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con
805806
// project and constraint values.
806807
for pr, pp := range pc {
807808
// Check if the project constraint is imported in the root project
808-
if _, ok := directDeps[string(pr)]; !ok {
809+
if _, ok := directDeps[pr]; !ok {
809810
continue
810811
}
811812

project.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ func (p *Project) ParseRootPackageTree() (pkgtree.PackageTree, error) {
167167
// project's package tree.
168168
//
169169
// The returned map of Project Roots contains only boolean true values; this
170-
// makes a "false" value always indicate an absent key, which makes if checks
171-
// against the map more ergonomic.
170+
// makes a "false" value always indicate an absent key, which makes conditional
171+
// checks against the map more ergonomic.
172172
//
173173
// This function will correctly utilize ignores and requireds from an existing
174174
// manifest, if one is present, but will also do the right thing without a

0 commit comments

Comments
 (0)