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

Commit b858322

Browse files
committed
Modify rootAnalyzer & gopathScanner for network mode
Make rootAnalyzer and gopathScanner -gopath flag aware and skip usual operations when in network mode.
1 parent 443490c commit b858322

File tree

3 files changed

+67
-66
lines changed

3 files changed

+67
-66
lines changed

cmd/dep/gopath_scanner.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ type gopathScanner struct {
2525
ctx *dep.Ctx
2626
directDeps map[string]bool
2727
sm gps.SourceManager
28+
gopath bool
2829

2930
pd projectData
3031
origM *dep.Manifest
3132
origL *dep.Lock
3233
}
3334

34-
func newGopathScanner(ctx *dep.Ctx, directDeps map[string]bool, sm gps.SourceManager) *gopathScanner {
35+
func newGopathScanner(ctx *dep.Ctx, directDeps map[string]bool, sm gps.SourceManager, gopath bool) *gopathScanner {
3536
return &gopathScanner{
3637
ctx: ctx,
3738
directDeps: directDeps,
3839
sm: sm,
40+
gopath: gopath,
3941
}
4042
}
4143

@@ -44,6 +46,11 @@ func newGopathScanner(ctx *dep.Ctx, directDeps map[string]bool, sm gps.SourceMan
4446
// constraints. Respect any initial constraints defined in the root manifest and
4547
// lock.
4648
func (g *gopathScanner) InitializeRootManifestAndLock(rootM *dep.Manifest, rootL *dep.Lock) error {
49+
// No operation in network mode
50+
if !g.gopath {
51+
return nil
52+
}
53+
4754
var err error
4855

4956
g.ctx.Err.Println("Searching GOPATH for projects...")
@@ -130,6 +137,11 @@ func (g *gopathScanner) overlay(rootM *dep.Manifest, rootL *dep.Lock) {
130137
}
131138

132139
func (g *gopathScanner) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock) {
140+
// No operation in network mode
141+
if !g.gopath {
142+
return
143+
}
144+
133145
// Iterate through the new projects in solved lock and add them to manifest
134146
// if direct deps and log feedback for all the new projects.
135147
for _, x := range l.Projects() {

cmd/dep/init.go

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/golang/dep"
14-
fb "github.com/golang/dep/internal/feedback"
1514
"github.com/golang/dep/internal/fs"
1615
"github.com/golang/dep/internal/gps"
1716
"github.com/golang/dep/internal/gps/paths"
@@ -121,52 +120,27 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
121120
sm.UseDefaultSignalHandling()
122121
defer sm.Release()
123122

124-
// Create empty manifest and lock
125-
m := &dep.Manifest{
126-
Constraints: make(gps.ProjectConstraints),
127-
Ovr: make(gps.ProjectConstraints),
123+
// Initialize with imported data, then fill in the gaps using the GOPATH
124+
rootAnalyzer := newRootAnalyzer(cmd.skipTools, cmd.gopath, ctx, directDeps, sm)
125+
m, l, err := rootAnalyzer.InitializeRootManifestAndLock(root, gps.ProjectRoot(cpr))
126+
if err != nil {
127+
return err
128128
}
129-
l := &dep.Lock{}
130-
131-
// Initialize rootAnalyzer and gopathScanner
132-
rootAnalyzer := newRootAnalyzer(cmd.skipTools, ctx, directDeps, sm)
133-
gs := newGopathScanner(ctx, directDeps, sm)
134-
135-
params := gps.SolveParameters{}
136-
137-
if cmd.gopath {
138-
// Initialize with imported data, then fill in the gaps using the GOPATH
139-
im, il, err := rootAnalyzer.InitializeRootManifestAndLock(root, gps.ProjectRoot(cpr))
140-
if err != nil {
141-
return err
142-
}
143-
144-
if im != nil {
145-
m = im
146-
}
147-
if il != nil {
148-
l = il
149-
}
150-
151-
err = gs.InitializeRootManifestAndLock(m, l)
152-
if err != nil {
153-
return err
154-
}
155129

156-
rootAnalyzer.skipTools = true // Don't import external config during solve for now
130+
gs := newGopathScanner(ctx, directDeps, sm, cmd.gopath)
131+
err = gs.InitializeRootManifestAndLock(m, l)
132+
if err != nil {
133+
return err
157134
}
158135

159-
params = gps.SolveParameters{
136+
rootAnalyzer.skipTools = true // Don't import external config during solve for now
137+
138+
params := gps.SolveParameters{
160139
RootDir: root,
161140
RootPackageTree: pkgT,
162141
Manifest: m,
163142
Lock: l,
164-
}
165-
166-
if cmd.gopath {
167-
params.ProjectAnalyzer = rootAnalyzer
168-
} else {
169-
params.ProjectAnalyzer = dep.Analyzer{}
143+
ProjectAnalyzer: rootAnalyzer,
170144
}
171145

172146
if ctx.Verbose {
@@ -185,21 +159,8 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
185159
}
186160
l = dep.LockFromSolution(soln)
187161

188-
if cmd.gopath {
189-
rootAnalyzer.FinalizeRootManifestAndLock(m, l)
190-
gs.FinalizeRootManifestAndLock(m, l)
191-
} else {
192-
// Pick the direct dependencies from the above solution and add to manifest
193-
for _, x := range l.Projects() {
194-
pr := x.Ident().ProjectRoot
195-
if _, ok := directDeps[string(pr)]; ok {
196-
m.Constraints[pr] = getProjectPropertiesFromVersion(x.Version())
197-
feedback(x.Version(), pr, fb.DepTypeDirect, ctx.Err)
198-
} else {
199-
feedback(x.Version(), pr, fb.DepTypeTransitive, ctx.Err)
200-
}
201-
}
202-
}
162+
rootAnalyzer.FinalizeRootManifestAndLock(m, l)
163+
gs.FinalizeRootManifestAndLock(m, l)
203164

204165
// Run gps.Prepare with appropriate constraint solutions from solve run
205166
// to generate the final lock memo.

cmd/dep/root_analyzer.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,41 @@ type importer interface {
3030
// then external tools.
3131
type rootAnalyzer struct {
3232
skipTools bool
33+
gopath bool
3334
ctx *dep.Ctx
3435
sm gps.SourceManager
3536
directDeps map[string]bool
3637
}
3738

38-
func newRootAnalyzer(skipTools bool, ctx *dep.Ctx, directDeps map[string]bool, sm gps.SourceManager) *rootAnalyzer {
39+
func newRootAnalyzer(skipTools bool, gopath bool, ctx *dep.Ctx, directDeps map[string]bool, sm gps.SourceManager) *rootAnalyzer {
3940
return &rootAnalyzer{
4041
skipTools: skipTools,
42+
gopath: gopath,
4143
ctx: ctx,
4244
sm: sm,
4345
directDeps: directDeps,
4446
}
4547
}
4648

4749
func (a *rootAnalyzer) InitializeRootManifestAndLock(dir string, pr gps.ProjectRoot) (rootM *dep.Manifest, rootL *dep.Lock, err error) {
48-
if !a.skipTools {
50+
// Use importer tools only in gopath mode
51+
if a.gopath && !a.skipTools {
4952
rootM, rootL, err = a.importManifestAndLock(dir, pr, false)
5053
if err != nil {
5154
return
5255
}
5356
}
5457

58+
if rootM == nil {
59+
rootM = &dep.Manifest{
60+
Constraints: make(gps.ProjectConstraints),
61+
Ovr: make(gps.ProjectConstraints),
62+
}
63+
}
64+
if rootL == nil {
65+
rootL = &dep.Lock{}
66+
}
67+
5568
return
5669
}
5770

@@ -114,17 +127,32 @@ func (a *rootAnalyzer) DeriveManifestAndLock(dir string, pr gps.ProjectRoot) (gp
114127
}
115128

116129
func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock) {
117-
// Remove dependencies from the manifest that aren't used
118-
for pr := range m.Constraints {
119-
var used bool
120-
for _, y := range l.Projects() {
121-
if pr == y.Ident().ProjectRoot {
122-
used = true
123-
break
130+
if a.gopath {
131+
// Remove dependencies from the manifest that aren't used
132+
for pr := range m.Constraints {
133+
var used bool
134+
for _, y := range l.Projects() {
135+
if pr == y.Ident().ProjectRoot {
136+
used = true
137+
break
138+
}
139+
}
140+
if !used {
141+
delete(m.Constraints, pr)
124142
}
125143
}
126-
if !used {
127-
delete(m.Constraints, pr)
144+
} else {
145+
// Pick the direct dependencies from the solution lock and add to manifest.
146+
// This is done in network mode to fill up the manifest constraints with
147+
// the dependencies solved over the network.
148+
for _, y := range l.Projects() {
149+
pr := y.Ident().ProjectRoot
150+
if _, ok := a.directDeps[string(pr)]; ok {
151+
m.Constraints[pr] = getProjectPropertiesFromVersion(y.Version())
152+
feedback(y.Version(), pr, fb.DepTypeDirect, a.ctx.Err)
153+
} else {
154+
feedback(y.Version(), pr, fb.DepTypeTransitive, a.ctx.Err)
155+
}
128156
}
129157
}
130158
}

0 commit comments

Comments
 (0)