@@ -122,6 +122,7 @@ type ensureCommand struct {
122
122
add bool
123
123
noVendor bool
124
124
vendorOnly bool
125
+ dryRun bool
125
126
overrides stringSlice
126
127
}
127
128
@@ -173,7 +174,6 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
173
174
return err
174
175
}
175
176
176
- var fail error
177
177
if cmd .add {
178
178
return cmd .runAdd (ctx , args , p , sm , params )
179
179
} else if cmd .update {
@@ -191,14 +191,13 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
191
191
return errors .New ("dep ensure only takes spec arguments with -add or -update - did you want one of those?" )
192
192
}
193
193
194
- sw := & dep.SafeWriter {}
195
194
if cmd .vendorOnly {
196
195
if p .Lock == nil {
197
196
return errors .Errorf ("no %s exists from which to populate vendor/ directory" , dep .LockName )
198
197
}
199
198
// Pass the same lock as old and new so that the writer will observe no
200
199
// difference and choose not to write it out.
201
- err := sw . Prepare (nil , p .Lock , p .Lock , dep .VendorAlways )
200
+ sw , err := dep . NewSafeWriter (nil , p .Lock , p .Lock , dep .VendorAlways )
202
201
if err != nil {
203
202
return err
204
203
}
@@ -229,7 +228,7 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
229
228
// that "verification" is supposed to look like (#121); in the meantime,
230
229
// we unconditionally write out vendor/ so that `dep ensure`'s behavior
231
230
// is maximally compatible with what it will eventually become.
232
- err := sw . Prepare (nil , p .Lock , p .Lock , dep .VendorAlways )
231
+ sw , err := dep . NewSafeWriter (nil , p .Lock , p .Lock , dep .VendorAlways )
233
232
if err != nil {
234
233
return err
235
234
}
@@ -248,17 +247,20 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
248
247
return errors .Wrap (err , "ensure Solve()" )
249
248
}
250
249
251
- sw .Prepare (nil , p .Lock , dep .LockFromInterface (solution ), dep .VendorOnChanged )
250
+ sw , err := dep .NewSafeWriter (nil , p .Lock , dep .LockFromInterface (solution ), dep .VendorOnChanged )
251
+ if err != nil {
252
+ return err
253
+ }
252
254
if cmd .dryRun {
253
- return sw .PrintPreparedActions ()
255
+ return sw .PrintPreparedActions (ctx . Loggers . Out )
254
256
}
255
257
256
258
return nil
257
259
}
258
260
259
261
func (cmd * ensureCommand ) runUpdate (ctx * dep.Ctx , args []string , p * dep.Project , sm gps.SourceManager , params gps.SolveParameters ) error {
260
262
if p .Lock == nil {
261
- return errors .New ("%s does not exist. nothing to do, as -update works by updating the values in %s." , dep .LockName , dep .LockName )
263
+ return errors .Errorf ("%s does not exist. nothing to do, as -update works by updating the values in %s." , dep .LockName , dep .LockName )
262
264
}
263
265
264
266
// We'll need to discard this prepared solver as later work changes params,
@@ -283,7 +285,6 @@ func (cmd *ensureCommand) runUpdate(ctx *dep.Ctx, args []string, p *dep.Project,
283
285
// versions, regardless of the lock file.
284
286
if len (args ) == 0 {
285
287
params .ChangeAll = true
286
- return
287
288
}
288
289
289
290
// Allow any of specified project versions to change, regardless of the lock
@@ -302,8 +303,8 @@ func (cmd *ensureCommand) runUpdate(ctx *dep.Ctx, args []string, p *dep.Project,
302
303
return errors .Errorf ("%s is not present in %s, cannot -update it" , pc .Ident .ProjectRoot , dep .LockName )
303
304
}
304
305
305
- if p .Ident .Source != "" {
306
- return errors .Errorf ("cannot specify alternate sources on -update (%s)" , p .Ident )
306
+ if pc .Ident .Source != "" {
307
+ return errors .Errorf ("cannot specify alternate sources on -update (%s)" , pc .Ident )
307
308
}
308
309
309
310
if ! gps .IsAny (pc .Constraint ) {
@@ -326,8 +327,10 @@ func (cmd *ensureCommand) runUpdate(ctx *dep.Ctx, args []string, p *dep.Project,
326
327
return errors .Wrap (err , "ensure Solve()" )
327
328
}
328
329
329
- var sw dep.SafeWriter
330
- sw .Prepare (nil , p .Lock , dep .LockFromInterface (solution ), dep .VendorOnChanged )
330
+ sw , err := dep .NewSafeWriter (nil , p .Lock , dep .LockFromInterface (solution ), dep .VendorOnChanged )
331
+ if err != nil {
332
+ return err
333
+ }
331
334
// TODO(sdboyer) special handling for warning cases as described in spec -
332
335
// e.g., named projects did not upgrade even though newer versions were
333
336
// available.
@@ -343,6 +346,14 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
343
346
return errors .New ("must specify at least one project or package to add" )
344
347
}
345
348
349
+ // We'll need to discard this prepared solver as later work changes params,
350
+ // but solver preparation is cheap and worth doing up front in order to
351
+ // perform the fastpath check of hash comparison.
352
+ solver , err := gps .Prepare (params , sm )
353
+ if err != nil {
354
+ return errors .Wrap (err , "fastpath solver prepare" )
355
+ }
356
+
346
357
// Compare the hashes. If they're not equal, bail out and ask the user to
347
358
// run a straight `dep ensure` before updating. This is handholding the
348
359
// user a bit, but the extra effort required is minimal, and it ensures the
@@ -357,25 +368,29 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
357
368
// Having some problematic internal packages isn't cause for termination,
358
369
// but the user needs to be warned.
359
370
for fail := range errmap {
360
- internal . Logf ("Warning: %s" , fail )
371
+ ctx . Loggers . Err . Printf ("Warning: %s" , fail )
361
372
}
362
373
374
+ // Compile unique sets of 1) all external packages imported or required, and
375
+ // 2) the project roots under which they fall.
363
376
exmap := make (map [string ]bool )
364
377
exrmap := make (map [gps.ProjectRoot ]bool )
365
- for _ , ex := range append (rm .Flatten (false ), p .Manifest .RequiredPackages ()... ) {
378
+
379
+ for _ , ex := range append (rm .Flatten (false ), p .Manifest .Required ... ) {
366
380
exmap [ex ] = true
367
381
root , err := sm .DeduceProjectRoot (ex )
368
382
if err != nil {
369
- // This should be essentially impossible to hit, as it entails that
370
- // we couldn't deduce the root for an import, but that some previous
371
- // solve run WAS able to deduce the root.
372
- return errors .Wrap (err , "could not deduce project root" )
383
+ // This should be very uncommon to hit, as it entails that we
384
+ // couldn't deduce the root for an import, but that some previous
385
+ // solve run WAS able to deduce the root. It's most likely to occur
386
+ // if the user has e.g. not connected to their organization's VPN,
387
+ // and thus cannot access an internal go-get metadata service.
388
+ return errors .Wrapf (err , "could not deduce project root for %s" , ex )
373
389
}
374
390
exrmap [root ] = true
375
391
}
376
392
377
393
var reqlist []string
378
- //pclist := make(map[gps.ProjectRoot]gps.ProjectConstraint)
379
394
380
395
for _ , arg := range args {
381
396
// TODO(sdboyer) return all errors, not just the first one we encounter
@@ -394,10 +409,10 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
394
409
395
410
err = sm .SyncSourceFor (pc .Ident )
396
411
if err != nil {
397
- return errors .Wrap (err , "failed to fetch source for %s" , pc .Ident .ProjectRoot )
412
+ return errors .Wrapf (err , "failed to fetch source for %s" , pc .Ident .ProjectRoot )
398
413
}
399
414
400
- someConstraint = pc .Constraint != nil || pc .Ident .Source != ""
415
+ someConstraint : = pc .Constraint != nil || pc .Ident .Source != ""
401
416
if inManifest {
402
417
if someConstraint {
403
418
return errors .Errorf ("%s already contains constraints for %s, cannot specify a version constraint or alternate source" , arg , dep .ManifestName )
@@ -457,13 +472,12 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
457
472
return errors .Wrap (err , "ensure Solve()" )
458
473
}
459
474
460
- var sw dep.SafeWriter
461
- sw .Prepare (nil , p .Lock , dep .LockFromInterface (solution ), dep .VendorOnChanged )
475
+ sw , err := dep .NewSafeWriter (nil , p .Lock , dep .LockFromInterface (solution ), dep .VendorOnChanged )
462
476
// TODO(sdboyer) special handling for warning cases as described in spec -
463
477
// e.g., named projects did not upgrade even though newer versions were
464
478
// available.
465
479
if cmd .dryRun {
466
- return sw .PrintPreparedActions ()
480
+ return sw .PrintPreparedActions (ctx . Loggers . Out )
467
481
}
468
482
469
483
return errors .Wrap (sw .Write (p .AbsRoot , sm , true ), "grouped write of manifest, lock and vendor" )
0 commit comments