@@ -190,8 +190,8 @@ func limiterForEdit(ctx context.Context, rs *Requirements, tryUpgrade, mustSelec
190
190
191
191
// raiseLimitsForUpgrades increases the module versions in maxVersions to the
192
192
// versions that would be needed to allow each of the modules in tryUpgrade
193
- // (individually) and all of the modules in mustSelect (simultaneously) to be
194
- // added as roots.
193
+ // (individually or in any combination ) and all of the modules in mustSelect
194
+ // (simultaneously) to be added as roots.
195
195
//
196
196
// Versions not present in maxVersion are unrestricted, and it is assumed that
197
197
// they will not be promoted to root requirements (and thus will not contribute
@@ -213,18 +213,42 @@ func raiseLimitsForUpgrades(ctx context.Context, maxVersion map[string]string, d
213
213
}
214
214
}
215
215
216
- var eagerUpgrades []module.Version
216
+ var (
217
+ eagerUpgrades []module.Version
218
+ isLazyRootPath map [string ]bool
219
+ )
217
220
if depth == eager {
218
221
eagerUpgrades = tryUpgrade
219
222
} else {
223
+ isLazyRootPath = make (map [string ]bool , len (maxVersion ))
224
+ for p := range maxVersion {
225
+ isLazyRootPath [p ] = true
226
+ }
220
227
for _ , m := range tryUpgrade {
228
+ isLazyRootPath [m .Path ] = true
229
+ }
230
+ for _ , m := range mustSelect {
231
+ isLazyRootPath [m .Path ] = true
232
+ }
233
+
234
+ allowedRoot := map [module.Version ]bool {}
235
+
236
+ var allowRoot func (m module.Version ) error
237
+ allowRoot = func (m module.Version ) error {
238
+ if allowedRoot [m ] {
239
+ return nil
240
+ }
241
+ allowedRoot [m ] = true
242
+
221
243
if m .Path == Target .Path {
222
244
// Target is already considered to be higher than any possible m, so we
223
245
// won't be upgrading to it anyway and there is no point scanning its
224
246
// dependencies.
225
- continue
247
+ return nil
226
248
}
227
249
250
+ allow (m )
251
+
228
252
summary , err := goModSummary (m )
229
253
if err != nil {
230
254
return err
@@ -234,13 +258,27 @@ func raiseLimitsForUpgrades(ctx context.Context, maxVersion map[string]string, d
234
258
// graph, rather than loading the (potentially-overlapping) subgraph for
235
259
// each upgrade individually.
236
260
eagerUpgrades = append (eagerUpgrades , m )
237
- continue
261
+ return nil
238
262
}
239
-
240
- allow (m )
241
263
for _ , r := range summary .require {
242
- allow (r )
264
+ if isLazyRootPath [r .Path ] {
265
+ // r could become a root as the result of an upgrade or downgrade,
266
+ // in which case its dependencies will not be pruned out.
267
+ // We need to allow those dependencies to be upgraded too.
268
+ if err := allowRoot (r ); err != nil {
269
+ return err
270
+ }
271
+ } else {
272
+ // r will not become a root, so its dependencies don't matter.
273
+ // Allow only r itself.
274
+ allow (r )
275
+ }
243
276
}
277
+ return nil
278
+ }
279
+
280
+ for _ , m := range tryUpgrade {
281
+ allowRoot (m )
244
282
}
245
283
}
246
284
@@ -269,16 +307,41 @@ func raiseLimitsForUpgrades(ctx context.Context, maxVersion map[string]string, d
269
307
}
270
308
}
271
309
272
- if len (mustSelect ) > 0 {
273
- mustGraph , err := readModGraph (ctx , depth , mustSelect )
310
+ // Explicitly allow any (transitive) upgrades implied by mustSelect.
311
+ nextRoots := append ([]module.Version (nil ), mustSelect ... )
312
+ for nextRoots != nil {
313
+ module .Sort (nextRoots )
314
+ rs := newRequirements (depth , nextRoots , nil )
315
+ nextRoots = nil
316
+
317
+ rs , mustGraph , err := expandGraph (ctx , rs )
274
318
if err != nil {
275
319
return err
276
320
}
277
321
278
322
for _ , r := range mustGraph .BuildList () {
279
- // Some module in mustSelect requires r, so we must allow at least r.Version
280
- // unless it conflicts with an entry in mustSelect.
323
+ // Some module in mustSelect requires r, so we must allow at least
324
+ // r.Version (unless it conflicts with another entry in mustSelect, in
325
+ // which case we will error out either way).
281
326
allow (r )
327
+
328
+ if isLazyRootPath [r .Path ] {
329
+ if v , ok := rs .rootSelected (r .Path ); ok && r .Version == v {
330
+ // r is already a root, so its requirements are already included in
331
+ // the build list.
332
+ continue
333
+ }
334
+
335
+ // The dependencies in mustSelect may upgrade (or downgrade) an existing
336
+ // root to match r, which will remain as a root. However, since r is not
337
+ // a root of rs, its dependencies have been pruned out of this build
338
+ // list. We need to add it back explicitly so that we allow any
339
+ // transitive upgrades that r will pull in.
340
+ if nextRoots == nil {
341
+ nextRoots = rs .rootModules // already capped
342
+ }
343
+ nextRoots = append (nextRoots , r )
344
+ }
282
345
}
283
346
}
284
347
0 commit comments