@@ -215,14 +215,16 @@ func shouldInclude(target string, ignoreRegex []string) bool {
215
215
return true
216
216
}
217
217
218
- func exploreAllDependenciesToExclude (input * api.Package , allPackages map [string ]* api.Package , previouslyExplored map [string ]bool ) []* api.Package {
218
+ func exploreAllDependencies (input * api.Package , allPackages map [string ]* api.Package , previouslyExplored map [string ]bool ) []* api.Package {
219
219
alreadyExplored := make (map [string ]* api.Package , 0 )
220
220
pending := []* api.Package {input }
221
221
222
222
for len (pending ) > 0 {
223
223
current := pending [0 ]
224
224
pending = pending [1 :]
225
225
226
+ logrus .Debugf ("exploring %s" , current .Name )
227
+
226
228
if _ , explored := previouslyExplored [current .Name ]; explored {
227
229
logrus .Debugf ("previously explored %s" , current .Name )
228
230
continue
@@ -235,7 +237,11 @@ func exploreAllDependenciesToExclude(input *api.Package, allPackages map[string]
235
237
alreadyExplored [current .Name ] = current
236
238
237
239
for _ , entry := range current .Format .Requires .Entries {
238
- pending = append (pending , allPackages [entry .Name ])
240
+ t , ok := allPackages [entry .Name ]
241
+ if ! ok {
242
+ continue
243
+ }
244
+ pending = append (pending , t )
239
245
}
240
246
}
241
247
@@ -273,6 +279,11 @@ func filterIgnores(rpmsRequested []string, allAvailable []*api.Package, ignoreRe
273
279
toInstall := make ([]* api.Package , 0 )
274
280
ignored := make (map [string ]* api.Package , 0 )
275
281
282
+ requested := map [string ]bool {}
283
+ for _ , rpm := range rpmsRequested {
284
+ requested [rpm ] = true
285
+ }
286
+
276
287
explored := make (map [string ]bool , 0 )
277
288
for _ , rpm := range rpmsRequested {
278
289
target , ok := allAvailablePerName [rpm ]
@@ -292,8 +303,12 @@ func filterIgnores(rpmsRequested []string, allAvailable []*api.Package, ignoreRe
292
303
}
293
304
294
305
if ! shouldInclude (current .Name , ignoreRegex ) {
295
- toIgnore := exploreAllDependenciesToExclude (current , allAvailablePerName , explored )
306
+ toIgnore := exploreAllDependencies (current , allAvailablePerName , explored )
296
307
for _ , d := range toIgnore {
308
+ // don't exclude those things that were explicitly requested
309
+ if _ , isRequested := requested [d .Name ]; isRequested {
310
+ continue
311
+ }
297
312
logrus .Debugf ("excluding %s" , d .Name )
298
313
ignored [d .Name ] = d
299
314
explored [d .Name ] = true
@@ -322,6 +337,47 @@ func filterIgnores(rpmsRequested []string, allAvailable []*api.Package, ignoreRe
322
337
return toInstall , ignoredPackages
323
338
}
324
339
340
+ func garbageCollect (rpmsRequested []string , toInstall []* api.Package , ignored []* api.Package ) ([]* api.Package , []* api.Package ) {
341
+ requested := map [string ]bool {}
342
+ for _ , rpm := range rpmsRequested {
343
+ requested [rpm ] = true
344
+ }
345
+
346
+ directDependencies := make (map [string ]bool )
347
+ allPackages := make (map [string ]* api.Package , 0 )
348
+ for _ , rpm := range toInstall {
349
+ allPackages [rpm .Name ] = rpm
350
+ }
351
+
352
+ emptyMap := make (map [string ]bool , 0 )
353
+
354
+ for _ , rpm := range toInstall {
355
+ if _ , isDirectDependency := requested [rpm .Name ]; ! isDirectDependency {
356
+ continue
357
+ }
358
+ directDependencies [rpm .Name ] = true
359
+ deps := exploreAllDependencies (rpm , allPackages , emptyMap )
360
+ for _ , dep := range deps {
361
+ if _ , available := allPackages [dep .Name ]; ! available {
362
+ // this one was filtered out by the ignore regex
363
+ continue
364
+ }
365
+ directDependencies [dep .Name ] = true
366
+ }
367
+ }
368
+
369
+ toKeep := make ([]* api.Package , 0 )
370
+ for _ , rpm := range toInstall {
371
+ if _ , keep := directDependencies [rpm .Name ]; keep {
372
+ toKeep = append (toKeep , rpm )
373
+ } else {
374
+ ignored = append (ignored , rpm )
375
+ }
376
+ }
377
+
378
+ return toKeep , ignored
379
+ }
380
+
325
381
func (opts * BzlmodOpts ) RunE (cmd * cobra.Command , rpms []string ) error {
326
382
logrus .Info ("Loading repo files" )
327
383
repos , err := repo .LoadRepoFiles (bzlmodopts .repoFiles )
@@ -366,7 +422,10 @@ func (opts *BzlmodOpts) RunE(cmd *cobra.Command, rpms []string) error {
366
422
logrus .Debugf ("install: %v" , install )
367
423
368
424
actualInstall , forceIgnored := filterIgnores (rpms , install , resolvehelperopts .forceIgnoreRegex )
425
+ logrus .Debugf ("before GC actual install: %d" , len (actualInstall ))
426
+ logrus .Debugf ("before GC actual ignored: %d" , len (forceIgnored ))
369
427
428
+ actualInstall , forceIgnored = garbageCollect (rpms , actualInstall , forceIgnored )
370
429
logrus .Debugf ("actualInstall: %v" , actualInstall )
371
430
logrus .Debugf ("forceIgnored: %v" , forceIgnored )
372
431
0 commit comments