@@ -481,6 +481,11 @@ func discoverComponents(ctx context.Context, workspace *Workspace, args Argument
481481 defer wg .Done ()
482482
483483 for c := range cchan {
484+ // filter variant-excluded components and all their packages
485+ if filterExcludedComponents (variant , c ) {
486+ continue
487+ }
488+
484489 comps = append (comps , c )
485490 }
486491 }()
@@ -494,6 +499,33 @@ func discoverComponents(ctx context.Context, workspace *Workspace, args Argument
494499 return comps , nil
495500}
496501
502+ // filterExcludedComponents returns true if the component is excluded by the variant.
503+ // This function also removes all dependencies to excluded components.
504+ func filterExcludedComponents (variant * PackageVariant , c * Component ) (ignoreComponent bool ) {
505+ if variant == nil {
506+ return false
507+ }
508+ if variant .ExcludeComponent (c .Name ) {
509+ log .WithField ("component" , c .Name ).Debug ("selected variant excludes this component" )
510+ return true
511+ }
512+
513+ for _ , p := range c .Packages {
514+ for i , dep := range p .Dependencies {
515+ segs := strings .Split (dep , ":" )
516+ if len (segs ) != 2 {
517+ continue
518+ }
519+
520+ if variant .ExcludeComponent (segs [0 ]) {
521+ p .Dependencies [i ] = p .Dependencies [len (p .Dependencies )- 1 ]
522+ p .Dependencies = p .Dependencies [:len (p .Dependencies )- 1 ]
523+ }
524+ }
525+ }
526+ return false
527+ }
528+
497529// loadComponent loads a component from a BUILD.yaml file
498530func loadComponent (ctx context.Context , workspace * Workspace , path string , args Arguments , variant * PackageVariant ) (c Component , err error ) {
499531 defer trace .StartRegion (context .Background (), "loadComponent" ).End ()
0 commit comments