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

Commit 7db1353

Browse files
committed
dep, internal/gps: update SafeWriter.Write and gps.WriteDepTree
Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent 8344181 commit 7db1353

File tree

6 files changed

+82
-48
lines changed

6 files changed

+82
-48
lines changed

cmd/dep/ensure.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
252252
return nil
253253
}
254254

255-
return errors.WithMessage(sw.Write(p.AbsRoot, sm, true), "grouped write of manifest, lock and vendor")
255+
err = sw.Write(p.AbsRoot, sm, true, p.Manifest.PruneOptions, nil)
256+
return errors.WithMessage(err, "grouped write of manifest, lock and vendor")
256257
}
257258

258259
solution, err := solver.Solve()
@@ -269,7 +270,8 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
269270
return sw.PrintPreparedActions(ctx.Out)
270271
}
271272

272-
return errors.Wrap(sw.Write(p.AbsRoot, sm, false), "grouped write of manifest, lock and vendor")
273+
err = sw.Write(p.AbsRoot, sm, false, p.Manifest.PruneOptions, nil)
274+
return errors.Wrap(err, "grouped write of manifest, lock and vendor")
273275
}
274276

275277
func (cmd *ensureCommand) runVendorOnly(ctx *dep.Ctx, args []string, p *dep.Project, sm gps.SourceManager, params gps.SolveParameters) error {
@@ -292,7 +294,8 @@ func (cmd *ensureCommand) runVendorOnly(ctx *dep.Ctx, args []string, p *dep.Proj
292294
return nil
293295
}
294296

295-
return errors.WithMessage(sw.Write(p.AbsRoot, sm, true), "grouped write of manifest, lock and vendor")
297+
err = sw.Write(p.AbsRoot, sm, true, p.Manifest.PruneOptions, nil)
298+
return errors.WithMessage(err, "grouped write of manifest, lock and vendor")
296299
}
297300

298301
func (cmd *ensureCommand) runUpdate(ctx *dep.Ctx, args []string, p *dep.Project, sm gps.SourceManager, params gps.SolveParameters) error {
@@ -379,7 +382,8 @@ func (cmd *ensureCommand) runUpdate(ctx *dep.Ctx, args []string, p *dep.Project,
379382
return sw.PrintPreparedActions(ctx.Out)
380383
}
381384

382-
return errors.Wrap(sw.Write(p.AbsRoot, sm, false), "grouped write of manifest, lock and vendor")
385+
err = sw.Write(p.AbsRoot, sm, false, p.Manifest.PruneOptions, nil)
386+
return errors.Wrap(err, "grouped write of manifest, lock and vendor")
383387
}
384388

385389
func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm gps.SourceManager, params gps.SolveParameters) error {
@@ -609,7 +613,8 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
609613
return sw.PrintPreparedActions(ctx.Out)
610614
}
611615

612-
if err := errors.Wrap(sw.Write(p.AbsRoot, sm, true), "grouped write of manifest, lock and vendor"); err != nil {
616+
err = sw.Write(p.AbsRoot, sm, true, p.Manifest.PruneOptions, nil)
617+
if err := errors.Wrap(err, "grouped write of manifest, lock and vendor"); err != nil {
613618
return err
614619
}
615620

cmd/dep/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
201201
return err
202202
}
203203

204-
if err := sw.Write(root, sm, !cmd.noExamples); err != nil {
204+
if err := sw.Write(root, sm, !cmd.noExamples, p.Manifest.PruneOptions, nil); err != nil {
205205
return errors.Wrap(err, "safe write of manifest and lock")
206206
}
207207

internal/gps/result.go

+48-20
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
package gps
66

77
import (
8-
"fmt"
8+
"log"
99
"os"
1010
"path/filepath"
11+
"sync"
12+
13+
"github.com/pkg/errors"
1114
)
1215

1316
// A Solution is returned by a solver run. It is mostly just a Lock, with some
@@ -42,37 +45,62 @@ type solution struct {
4245
solv Solver
4346
}
4447

45-
// WriteDepTree takes a basedir and a Lock, and exports all the projects
46-
// listed in the lock to the appropriate target location within the basedir.
48+
// WriteDepTree takes a baseDir and a Lock, and exports all the projects
49+
// listed in the lock to the appropriate target location within baseDir.
4750
//
48-
// If the goal is to populate a vendor directory, basedir should be the absolute
51+
// If the goal is to populate a vendor directory, baseDir should be the absolute
4952
// path to that vendor directory, not its parent (a project root, typically).
5053
//
51-
// It requires a SourceManager to do the work, and takes a flag indicating
52-
// whether or not to strip vendor directories contained in the exported
53-
// dependencies.
54-
func WriteDepTree(basedir string, l Lock, sm SourceManager, sv bool) error {
54+
// It requires a SourceManager to do the work, and takes a PruneOptions
55+
// indicating the pruning options required for the exported dependencies.
56+
func WriteDepTree(baseDir string, l Lock, sm SourceManager, prune PruneOptions, logger *log.Logger) error {
57+
if baseDir == "" {
58+
return errors.New("must provide a non-empty baseDir")
59+
}
5560
if l == nil {
56-
return fmt.Errorf("must provide non-nil Lock to WriteDepTree")
61+
return errors.New("must provide a non-nil Lock to WriteDepTree")
62+
}
63+
if sm == nil {
64+
return errors.New("must provide a non-nil SourceManager to WriteDepTree")
5765
}
5866

59-
err := os.MkdirAll(basedir, 0777)
60-
if err != nil {
67+
if err := os.MkdirAll(baseDir, 0777); err != nil {
6168
return err
6269
}
6370

64-
// TODO(sdboyer) parallelize
71+
var wg sync.WaitGroup
72+
errCh := make(chan error, len(l.Projects()))
73+
6574
for _, p := range l.Projects() {
66-
to := filepath.FromSlash(filepath.Join(basedir, string(p.Ident().ProjectRoot)))
75+
wg.Add(1)
76+
go func(p LockedProject) {
77+
projectPath := filepath.Join(baseDir, string(p.Ident().ProjectRoot))
78+
to := filepath.FromSlash(projectPath)
79+
80+
if err := sm.ExportProject(p.Ident(), p.Version(), to); err != nil {
81+
removeAll(projectPath)
82+
errCh <- errors.Wrapf(err, "failed to export %s: %s", p.Ident().ProjectRoot)
83+
}
84+
85+
wg.Done()
86+
}(p)
87+
}
6788

68-
err = sm.ExportProject(p.Ident(), p.Version(), to)
69-
if err != nil {
70-
removeAll(basedir)
71-
return fmt.Errorf("error while exporting %s: %s", p.Ident().ProjectRoot, err)
72-
}
73-
if sv {
74-
filepath.Walk(to, stripVendor)
89+
wg.Wait()
90+
91+
if len(errCh) > 0 {
92+
logger.Println("Failed to write dep tree. The following errors occurred:")
93+
for err := range errCh {
94+
logger.Println(" * ", err)
7595
}
96+
removeAll(baseDir)
97+
return <-errCh
98+
}
99+
100+
if err := Prune(baseDir, prune, l, logger); err != nil {
101+
logger.Println("Failed to prune dep tree.")
102+
removeAll(baseDir)
103+
return err
76104
}
77105

78106
return nil

internal/gps/result_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,11 @@ func testWriteDepTree(t *testing.T) {
9090
}
9191

9292
// nil lock/result should err immediately
93-
err = WriteDepTree(tmp, nil, sm, true)
94-
if err == nil {
93+
if err := WriteDepTree(tmp, nil, sm, PruneNestedVendorDirs, nil); err == nil {
9594
t.Errorf("Should error if nil lock is passed to WriteDepTree")
9695
}
9796

98-
err = WriteDepTree(tmp, r, sm, true)
99-
if err != nil {
97+
if err := WriteDepTree(tmp, r, sm, PruneNestedVendorDirs, nil); err != nil {
10098
t.Errorf("Unexpected error while creating vendor tree: %s", err)
10199
}
102100

@@ -143,7 +141,7 @@ func BenchmarkCreateVendorTree(b *testing.B) {
143141
// ease manual inspection
144142
os.RemoveAll(exp)
145143
b.StartTimer()
146-
err = WriteDepTree(exp, r, sm, true)
144+
err = WriteDepTree(exp, r, sm, PruneNestedVendorDirs, nil)
147145
b.StopTimer()
148146
if err != nil {
149147
b.Errorf("unexpected error after %v iterations: %s", i, err)

txn_writer.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,15 @@ func (sw SafeWriter) validate(root string, sm gps.SourceManager) error {
253253
return nil
254254
}
255255

256-
// Write saves some combination of config yaml, lock, and a vendor tree.
256+
// Write saves some combination of a manifest, a lock, and a vendor tree.
257257
// root is the absolute path of root dir in which to write.
258258
// sm is only required if vendor is being written.
259259
//
260260
// It first writes to a temp dir, then moves them in place if and only if all the write
261261
// operations succeeded. It also does its best to roll back if any moves fail.
262262
// This mostly guarantees that dep cannot exit with a partial write that would
263263
// leave an undefined state on disk.
264-
func (sw *SafeWriter) Write(root string, sm gps.SourceManager, examples bool) error {
264+
func (sw *SafeWriter) Write(root string, sm gps.SourceManager, examples bool, prune gps.PruneOptions, logger *log.Logger) error {
265265
err := sw.validate(root, sm)
266266
if err != nil {
267267
return err
@@ -313,7 +313,10 @@ func (sw *SafeWriter) Write(root string, sm gps.SourceManager, examples bool) er
313313
}
314314

315315
if sw.writeVendor {
316-
err = gps.WriteDepTree(filepath.Join(td, "vendor"), sw.lock, sm, true)
316+
// Ensure that gps.PruneNestedVendorDirs is toggled on.
317+
prune |= gps.PruneNestedVendorDirs
318+
319+
err = gps.WriteDepTree(filepath.Join(td, "vendor"), sw.lock, sm, prune, logger)
317320
if err != nil {
318321
return errors.Wrap(err, "error while writing out vendor tree")
319322
}

txn_writer_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestSafeWriter_BadInput_MissingRoot(t *testing.T) {
2626
defer pc.Release()
2727

2828
sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged)
29-
err := sw.Write("", pc.SourceManager, true)
29+
err := sw.Write("", pc.SourceManager, true, 0, nil)
3030

3131
if err == nil {
3232
t.Fatal("should have errored without a root path, but did not")
@@ -44,7 +44,7 @@ func TestSafeWriter_BadInput_MissingSourceManager(t *testing.T) {
4444
pc.Load()
4545

4646
sw, _ := NewSafeWriter(nil, nil, pc.Project.Lock, VendorAlways)
47-
err := sw.Write(pc.Project.AbsRoot, nil, true)
47+
err := sw.Write(pc.Project.AbsRoot, nil, true, 0, nil)
4848

4949
if err == nil {
5050
t.Fatal("should have errored without a source manager when forceVendor is true, but did not")
@@ -92,7 +92,7 @@ func TestSafeWriter_BadInput_NonexistentRoot(t *testing.T) {
9292
sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged)
9393

9494
missingroot := filepath.Join(pc.Project.AbsRoot, "nonexistent")
95-
err := sw.Write(missingroot, pc.SourceManager, true)
95+
err := sw.Write(missingroot, pc.SourceManager, true, 0, nil)
9696

9797
if err == nil {
9898
t.Fatal("should have errored with nonexistent dir for root path, but did not")
@@ -110,7 +110,7 @@ func TestSafeWriter_BadInput_RootIsFile(t *testing.T) {
110110
sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged)
111111

112112
fileroot := pc.CopyFile("fileroot", "txn_writer/badinput_fileroot")
113-
err := sw.Write(fileroot, pc.SourceManager, true)
113+
err := sw.Write(fileroot, pc.SourceManager, true, 0, nil)
114114

115115
if err == nil {
116116
t.Fatal("should have errored when root path is a file, but did not")
@@ -145,7 +145,7 @@ func TestSafeWriter_Manifest(t *testing.T) {
145145
}
146146

147147
// Write changes
148-
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true)
148+
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, 0, nil)
149149
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
150150

151151
// Verify file system changes
@@ -190,7 +190,7 @@ func TestSafeWriter_ManifestAndUnmodifiedLock(t *testing.T) {
190190
}
191191

192192
// Write changes
193-
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true)
193+
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, 0, nil)
194194
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
195195

196196
// Verify file system changes
@@ -235,7 +235,7 @@ func TestSafeWriter_ManifestAndUnmodifiedLockWithForceVendor(t *testing.T) {
235235
}
236236

237237
// Write changes
238-
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true)
238+
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, 0, nil)
239239
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
240240

241241
// Verify file system changes
@@ -285,7 +285,7 @@ func TestSafeWriter_ModifiedLock(t *testing.T) {
285285
}
286286

287287
// Write changes
288-
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true)
288+
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, 0, nil)
289289
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
290290

291291
// Verify file system changes
@@ -335,7 +335,7 @@ func TestSafeWriter_ModifiedLockSkipVendor(t *testing.T) {
335335
}
336336

337337
// Write changes
338-
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true)
338+
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, 0, nil)
339339
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
340340

341341
// Verify file system changes
@@ -363,7 +363,7 @@ func TestSafeWriter_ForceVendorWhenVendorAlreadyExists(t *testing.T) {
363363
pc.Load()
364364

365365
sw, _ := NewSafeWriter(nil, pc.Project.Lock, pc.Project.Lock, VendorAlways)
366-
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true)
366+
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, 0, nil)
367367
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
368368

369369
// Verify prepared actions
@@ -381,7 +381,7 @@ func TestSafeWriter_ForceVendorWhenVendorAlreadyExists(t *testing.T) {
381381
t.Fatal("Expected the payload to contain the vendor directory ")
382382
}
383383

384-
err = sw.Write(pc.Project.AbsRoot, pc.SourceManager, true)
384+
err = sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, 0, nil)
385385
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
386386

387387
// Verify file system changes
@@ -431,7 +431,7 @@ func TestSafeWriter_NewLock(t *testing.T) {
431431
}
432432

433433
// Write changes
434-
err = sw.Write(pc.Project.AbsRoot, pc.SourceManager, true)
434+
err = sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, 0, nil)
435435
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
436436

437437
// Verify file system changes
@@ -478,7 +478,7 @@ func TestSafeWriter_NewLockSkipVendor(t *testing.T) {
478478
}
479479

480480
// Write changes
481-
err = sw.Write(pc.Project.AbsRoot, pc.SourceManager, true)
481+
err = sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, 0, nil)
482482
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
483483

484484
// Verify file system changes
@@ -571,7 +571,7 @@ func TestSafeWriter_VendorDotGitPreservedWithForceVendor(t *testing.T) {
571571
t.Fatal("Expected the payload to contain the vendor directory")
572572
}
573573

574-
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true)
574+
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, 0, nil)
575575
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
576576

577577
// Verify file system changes

0 commit comments

Comments
 (0)