11use std:: { io:: Write as _, path:: PathBuf } ;
22
3- use anyhow:: { Context as _, Result } ;
3+ use anyhow:: { bail , Context as _, Result } ;
44use clap:: Args ;
55use wac_graph:: { CompositionGraph , EncodeOptions , NodeId , PackageId } ;
66use wac_types:: { Package , SubtypeChecker } ;
@@ -60,6 +60,15 @@ impl PlugCommand {
6060 ) ?;
6161 }
6262
63+ // Check we've actually done any plugging.
64+ if graph
65+ . get_instantiation_arguments ( socket_instantiation)
66+ . next ( )
67+ . is_none ( )
68+ {
69+ bail ! ( "no plugs were used to plug into the socket component" )
70+ }
71+
6372 // Export all exports from the socket component.
6473 for name in graph. types ( ) [ graph[ socket] . ty ( ) ]
6574 . exports
@@ -99,7 +108,6 @@ fn plug_into_socket(
99108) -> Result < ( ) , anyhow:: Error > {
100109 let plug = Package :: from_bytes ( name, None , plug, graph. types_mut ( ) ) ?;
101110 let plug = graph. register_package ( plug) ?;
102- let plug_instantiation = graph. instantiate ( plug) ;
103111
104112 let mut plugs = Vec :: new ( ) ;
105113 let mut cache = Default :: default ( ) ;
@@ -112,10 +120,13 @@ fn plug_into_socket(
112120 }
113121 }
114122
115- for plug in plugs {
116- log:: debug!( "using export `{plug}` for plug" ) ;
117- let export = graph. alias_instance_export ( plug_instantiation, & plug) ?;
118- graph. set_instantiation_argument ( socket_instantiation, & plug, export) ?;
123+ // Instantiate the plug component
124+ let mut plug_instantiation = None ;
125+ for plug_name in plugs {
126+ log:: debug!( "using export `{plug_name}` for plug" ) ;
127+ let plug_instantiation = * plug_instantiation. get_or_insert_with ( || graph. instantiate ( plug) ) ;
128+ let export = graph. alias_instance_export ( plug_instantiation, & plug_name) ?;
129+ graph. set_instantiation_argument ( socket_instantiation, & plug_name, export) ?;
119130 }
120131 Ok ( ( ) )
121132}
0 commit comments