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

Commit d56d41b

Browse files
committed
Generalize FinalizeManifestAndLock
- Removes gopathScanner's FinalizeManifestAndLock() and its usage. - Adds using RemoveTransitiveDependencies() to remove unused constraints. - Changes rootAnalyzer's FinalizeManifestAndLock to log feedback for new constraints and locked projects only.
1 parent b674913 commit d56d41b

File tree

3 files changed

+30
-53
lines changed

3 files changed

+30
-53
lines changed

cmd/dep/gopath_scanner.go

-21
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,6 @@ func (g *gopathScanner) overlay(rootM *dep.Manifest, rootL *dep.Lock) {
134134
}
135135
}
136136

137-
func (g *gopathScanner) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock) {
138-
// Iterate through the new projects in solved lock and add them to manifest
139-
// if direct deps and log feedback for all the new projects.
140-
for _, x := range l.Projects() {
141-
pr := x.Ident().ProjectRoot
142-
newProject := true
143-
// Check if it's a new project, not in the old lock
144-
for _, y := range g.origL.Projects() {
145-
if pr == y.Ident().ProjectRoot {
146-
newProject = false
147-
}
148-
}
149-
if newProject {
150-
// If it's in notondisk, add to manifest, these are direct dependencies.
151-
if _, ok := g.pd.notondisk[pr]; ok {
152-
m.Constraints[pr] = getProjectPropertiesFromVersion(x.Version())
153-
}
154-
}
155-
}
156-
}
157-
158137
func trimPathPrefix(p1, p2 string) string {
159138
if fs.HasFilepathPrefix(p1, p2) {
160139
return p1[len(p2):]

cmd/dep/init.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
149149
}
150150

151151
rootAnalyzer.skipTools = true // Don't import external config during solve for now
152+
copyLock := *p.Lock // Copy lock before solving. Use this to separate new lock projects from solved lock
152153

153154
params := gps.SolveParameters{
154155
RootDir: root,
@@ -174,10 +175,8 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
174175
}
175176
p.Lock = dep.LockFromSolution(soln)
176177

177-
rootAnalyzer.FinalizeRootManifestAndLock(p.Manifest, p.Lock)
178-
if cmd.gopath {
179-
gs.FinalizeRootManifestAndLock(p.Manifest, p.Lock)
180-
}
178+
rootAnalyzer.RemoveTransitiveDependencies(p.Manifest)
179+
rootAnalyzer.FinalizeRootManifestAndLock(p.Manifest, p.Lock, copyLock)
181180

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

cmd/dep/root_analyzer.go

+27-28
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (a *rootAnalyzer) importManifestAndLock(dir string, pr gps.ProjectRoot, sup
8282
if err != nil {
8383
return nil, nil, err
8484
}
85-
a.removeTransitiveDependencies(m)
85+
a.RemoveTransitiveDependencies(m)
8686
return m, l, err
8787
}
8888
}
@@ -91,7 +91,7 @@ func (a *rootAnalyzer) importManifestAndLock(dir string, pr gps.ProjectRoot, sup
9191
return emptyManifest, nil, nil
9292
}
9393

94-
func (a *rootAnalyzer) removeTransitiveDependencies(m *dep.Manifest) {
94+
func (a *rootAnalyzer) RemoveTransitiveDependencies(m *dep.Manifest) {
9595
for pr := range m.Constraints {
9696
if _, isDirect := a.directDeps[string(pr)]; !isDirect {
9797
delete(m.Constraints, pr)
@@ -126,39 +126,38 @@ func (a *rootAnalyzer) DeriveManifestAndLock(dir string, pr gps.ProjectRoot) (gp
126126
return gps.SimpleManifest{}, nil, nil
127127
}
128128

129-
func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock) {
130-
// Remove dependencies from the manifest that aren't used
131-
for pr := range m.Constraints {
132-
var used bool
133-
for _, y := range l.Projects() {
134-
if pr == y.Ident().ProjectRoot {
135-
used = true
136-
break
137-
}
138-
}
139-
if !used {
140-
delete(m.Constraints, pr)
141-
}
142-
}
143-
// Pick the direct dependencies from the solution lock and add to manifest.
144-
// This is done to fill up the manifest constraints with the dependencies
145-
// solved over the network.
129+
func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock, ol dep.Lock) {
130+
// Iterate through the new projects in solved lock and add them to manifest
131+
// if they are direct deps and log feedback for all the new projects.
146132
for _, y := range l.Projects() {
147133
var f *fb.ConstraintFeedback
148134
pr := y.Ident().ProjectRoot
135+
// New constraints: in new lock and dir dep but not in manifest
149136
if _, ok := a.directDeps[string(pr)]; ok {
150-
pp := getProjectPropertiesFromVersion(y.Version())
151-
if pp.Constraint != nil {
152-
m.Constraints[pr] = pp
153-
pc := gps.ProjectConstraint{Ident: y.Ident(), Constraint: pp.Constraint}
154-
f = fb.NewConstraintFeedback(pc, fb.DepTypeDirect)
155-
} else {
156-
f = fb.NewLockedProjectFeedback(y, fb.DepTypeDirect)
137+
if _, ok := m.Constraints[pr]; !ok {
138+
pp := getProjectPropertiesFromVersion(y.Version())
139+
if pp.Constraint != nil {
140+
m.Constraints[pr] = pp
141+
pc := gps.ProjectConstraint{Ident: y.Ident(), Constraint: pp.Constraint}
142+
f = fb.NewConstraintFeedback(pc, fb.DepTypeDirect)
143+
} else {
144+
f = fb.NewLockedProjectFeedback(y, fb.DepTypeDirect)
145+
}
146+
f.LogFeedback(a.ctx.Err)
157147
}
158148
} else {
159-
f = fb.NewLockedProjectFeedback(y, fb.DepTypeTransitive)
149+
// New locked projects: in new lock but not in old lock
150+
newProject := true
151+
for _, opl := range ol.Projects() {
152+
if pr == opl.Ident().ProjectRoot {
153+
newProject = false
154+
}
155+
}
156+
if newProject {
157+
f = fb.NewLockedProjectFeedback(y, fb.DepTypeTransitive)
158+
f.LogFeedback(a.ctx.Err)
159+
}
160160
}
161-
f.LogFeedback(a.ctx.Err)
162161
}
163162
}
164163

0 commit comments

Comments
 (0)