@@ -215,14 +215,16 @@ func shouldInclude(target string, ignoreRegex []string) bool {
215215 return true
216216}
217217
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 {
219219 alreadyExplored := make (map [string ]* api.Package , 0 )
220220 pending := []* api.Package {input }
221221
222222 for len (pending ) > 0 {
223223 current := pending [0 ]
224224 pending = pending [1 :]
225225
226+ logrus .Debugf ("exploring %s" , current .Name )
227+
226228 if _ , explored := previouslyExplored [current .Name ]; explored {
227229 logrus .Debugf ("previously explored %s" , current .Name )
228230 continue
@@ -235,7 +237,11 @@ func exploreAllDependenciesToExclude(input *api.Package, allPackages map[string]
235237 alreadyExplored [current .Name ] = current
236238
237239 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 )
239245 }
240246 }
241247
@@ -273,6 +279,11 @@ func filterIgnores(rpmsRequested []string, allAvailable []*api.Package, ignoreRe
273279 toInstall := make ([]* api.Package , 0 )
274280 ignored := make (map [string ]* api.Package , 0 )
275281
282+ requested := map [string ]bool {}
283+ for _ , rpm := range rpmsRequested {
284+ requested [rpm ] = true
285+ }
286+
276287 explored := make (map [string ]bool , 0 )
277288 for _ , rpm := range rpmsRequested {
278289 target , ok := allAvailablePerName [rpm ]
@@ -292,8 +303,12 @@ func filterIgnores(rpmsRequested []string, allAvailable []*api.Package, ignoreRe
292303 }
293304
294305 if ! shouldInclude (current .Name , ignoreRegex ) {
295- toIgnore := exploreAllDependenciesToExclude (current , allAvailablePerName , explored )
306+ toIgnore := exploreAllDependencies (current , allAvailablePerName , explored )
296307 for _ , d := range toIgnore {
308+ // don't exclude those things that were explicitly requested
309+ if _ , isRequested := requested [d .Name ]; isRequested {
310+ continue
311+ }
297312 logrus .Debugf ("excluding %s" , d .Name )
298313 ignored [d .Name ] = d
299314 explored [d .Name ] = true
@@ -322,6 +337,47 @@ func filterIgnores(rpmsRequested []string, allAvailable []*api.Package, ignoreRe
322337 return toInstall , ignoredPackages
323338}
324339
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+
325381func (opts * BzlmodOpts ) RunE (cmd * cobra.Command , rpms []string ) error {
326382 logrus .Info ("Loading repo files" )
327383 repos , err := repo .LoadRepoFiles (bzlmodopts .repoFiles )
@@ -366,7 +422,10 @@ func (opts *BzlmodOpts) RunE(cmd *cobra.Command, rpms []string) error {
366422 logrus .Debugf ("install: %v" , install )
367423
368424 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 ))
369427
428+ actualInstall , forceIgnored = garbageCollect (rpms , actualInstall , forceIgnored )
370429 logrus .Debugf ("actualInstall: %v" , actualInstall )
371430 logrus .Debugf ("forceIgnored: %v" , forceIgnored )
372431
0 commit comments