@@ -46,6 +46,18 @@ pub struct Workspace<'cfg> {
4646 // set above.
4747 members : Vec < PathBuf > ,
4848
49+ // The subset of `members` that are used by the
50+ // `build`, `check`, `test`, and `bench` subcommands
51+ // when no package is selected with `--package` / `-p` and `--all`
52+ // is not used.
53+ //
54+ // This is set by the `default-members` config
55+ // in the `[workspace]` section.
56+ // When unset, this is the same as `members` for virtual workspaces
57+ // (`--all` is implied)
58+ // or only the root package for non-virtual workspaces.
59+ default_members : Vec < PathBuf > ,
60+
4961 // True, if this is a temporary workspace created for the purposes of
5062 // cargo install or cargo package.
5163 is_ephemeral : bool ,
@@ -90,6 +102,7 @@ pub enum WorkspaceConfig {
90102pub struct WorkspaceRootConfig {
91103 root_dir : PathBuf ,
92104 members : Option < Vec < String > > ,
105+ default_members : Option < Vec < String > > ,
93106 exclude : Vec < String > ,
94107}
95108
@@ -121,6 +134,7 @@ impl<'cfg> Workspace<'cfg> {
121134 root_manifest : None ,
122135 target_dir : target_dir,
123136 members : Vec :: new ( ) ,
137+ default_members : Vec :: new ( ) ,
124138 is_ephemeral : false ,
125139 require_optional_deps : true ,
126140 } ;
@@ -157,6 +171,7 @@ impl<'cfg> Workspace<'cfg> {
157171 root_manifest : None ,
158172 target_dir : None ,
159173 members : Vec :: new ( ) ,
174+ default_members : Vec :: new ( ) ,
160175 is_ephemeral : true ,
161176 require_optional_deps : require_optional_deps,
162177 } ;
@@ -170,6 +185,7 @@ impl<'cfg> Workspace<'cfg> {
170185 ws. config . target_dir ( ) ?
171186 } ;
172187 ws. members . push ( ws. current_manifest . clone ( ) ) ;
188+ ws. default_members . push ( ws. current_manifest . clone ( ) ) ;
173189 }
174190 Ok ( ws)
175191 }
@@ -267,6 +283,14 @@ impl<'cfg> Workspace<'cfg> {
267283 }
268284 }
269285
286+ /// Returns an iterator over default packages in this workspace
287+ pub fn default_members < ' a > ( & ' a self ) -> Members < ' a , ' cfg > {
288+ Members {
289+ ws : self ,
290+ iter : self . default_members . iter ( ) ,
291+ }
292+ }
293+
270294 pub fn is_ephemeral ( & self ) -> bool {
271295 self . is_ephemeral
272296 }
@@ -345,23 +369,51 @@ impl<'cfg> Workspace<'cfg> {
345369 None => {
346370 debug ! ( "find_members - only me as a member" ) ;
347371 self . members . push ( self . current_manifest . clone ( ) ) ;
372+ self . default_members . push ( self . current_manifest . clone ( ) ) ;
348373 return Ok ( ( ) )
349374 }
350375 } ;
351376
352- let members_paths = {
377+ let members_paths;
378+ let default_members_paths;
379+ {
353380 let root_package = self . packages . load ( & root_manifest_path) ?;
354381 match * root_package. workspace_config ( ) {
355- WorkspaceConfig :: Root ( ref root_config) => root_config. members_paths ( ) ?,
382+ WorkspaceConfig :: Root ( ref root_config) => {
383+ members_paths = root_config. members_paths (
384+ root_config. members . as_ref ( ) . unwrap_or ( & vec ! [ ] )
385+ ) ?;
386+ default_members_paths = if let Some ( ref default) = root_config. default_members {
387+ Some ( root_config. members_paths ( default) ?)
388+ } else {
389+ None
390+ }
391+ }
356392 _ => bail ! ( "root of a workspace inferred but wasn't a root: {}" ,
357393 root_manifest_path. display( ) ) ,
358394 }
359- } ;
395+ }
360396
361397 for path in members_paths {
362398 self . find_path_deps ( & path. join ( "Cargo.toml" ) , & root_manifest_path, false ) ?;
363399 }
364400
401+ if let Some ( default) = default_members_paths {
402+ for path in default {
403+ let manifest_path = paths:: normalize_path ( & path. join ( "Cargo.toml" ) ) ;
404+ if !self . members . contains ( & manifest_path) {
405+ bail ! ( "package `{}` is listed in workspace’s default-members \
406+ but is not a member.",
407+ path. display( ) )
408+ }
409+ self . default_members . push ( manifest_path)
410+ }
411+ } else if self . is_virtual ( ) {
412+ self . default_members = self . members . clone ( )
413+ } else {
414+ self . default_members . push ( self . current_manifest . clone ( ) )
415+ }
416+
365417 self . find_path_deps ( & root_manifest_path, & root_manifest_path, false )
366418 }
367419
@@ -370,7 +422,7 @@ impl<'cfg> Workspace<'cfg> {
370422 root_manifest : & Path ,
371423 is_path_dep : bool ) -> CargoResult < ( ) > {
372424 let manifest_path = paths:: normalize_path ( manifest_path) ;
373- if self . members . iter ( ) . any ( |p| p == & manifest_path) {
425+ if self . members . contains ( & manifest_path) {
374426 return Ok ( ( ) )
375427 }
376428 if is_path_dep
@@ -632,11 +684,13 @@ impl WorkspaceRootConfig {
632684 pub fn new (
633685 root_dir : & Path ,
634686 members : & Option < Vec < String > > ,
687+ default_members : & Option < Vec < String > > ,
635688 exclude : & Option < Vec < String > > ,
636689 ) -> WorkspaceRootConfig {
637690 WorkspaceRootConfig {
638691 root_dir : root_dir. to_path_buf ( ) ,
639692 members : members. clone ( ) ,
693+ default_members : default_members. clone ( ) ,
640694 exclude : exclude. clone ( ) . unwrap_or_default ( ) ,
641695 }
642696 }
@@ -665,21 +719,19 @@ impl WorkspaceRootConfig {
665719 self . members . is_some ( )
666720 }
667721
668- fn members_paths ( & self ) -> CargoResult < Vec < PathBuf > > {
722+ fn members_paths ( & self , globs : & [ String ] ) -> CargoResult < Vec < PathBuf > > {
669723 let mut expanded_list = Vec :: new ( ) ;
670724
671- if let Some ( globs) = self . members . clone ( ) {
672- for glob in globs {
673- let pathbuf = self . root_dir . join ( glob) ;
674- let expanded_paths = Self :: expand_member_path ( & pathbuf) ?;
675-
676- // If glob does not find any valid paths, then put the original
677- // path in the expanded list to maintain backwards compatibility.
678- if expanded_paths. is_empty ( ) {
679- expanded_list. push ( pathbuf) ;
680- } else {
681- expanded_list. extend ( expanded_paths) ;
682- }
725+ for glob in globs {
726+ let pathbuf = self . root_dir . join ( glob) ;
727+ let expanded_paths = Self :: expand_member_path ( & pathbuf) ?;
728+
729+ // If glob does not find any valid paths, then put the original
730+ // path in the expanded list to maintain backwards compatibility.
731+ if expanded_paths. is_empty ( ) {
732+ expanded_list. push ( pathbuf) ;
733+ } else {
734+ expanded_list. extend ( expanded_paths) ;
683735 }
684736 }
685737
0 commit comments