@@ -247,7 +247,7 @@ fn get_targets(strategy: &CargoFmtStrategy) -> Result<BTreeSet<Target>, io::Erro
247247}
248248
249249fn get_targets_root_only ( targets : & mut BTreeSet < Target > ) -> Result < ( ) , io:: Error > {
250- let metadata = get_cargo_metadata ( None ) ?;
250+ let metadata = get_cargo_metadata ( None , false ) ?;
251251 let current_dir = env:: current_dir ( ) ?. canonicalize ( ) ?;
252252 let current_dir_manifest = current_dir. join ( "Cargo.toml" ) ;
253253 let workspace_root_path = PathBuf :: from ( & metadata. workspace_root ) . canonicalize ( ) ?;
@@ -282,7 +282,8 @@ fn get_targets_recursive(
282282 mut targets : & mut BTreeSet < Target > ,
283283 visited : & mut BTreeSet < String > ,
284284) -> Result < ( ) , io:: Error > {
285- let metadata = get_cargo_metadata ( manifest_path) ?;
285+ let metadata = get_cargo_metadata ( manifest_path, false ) ?;
286+ let metadata_with_deps = get_cargo_metadata ( manifest_path, true ) ?;
286287
287288 for package in metadata. packages {
288289 add_targets ( & package. targets , & mut targets) ;
@@ -293,11 +294,19 @@ fn get_targets_recursive(
293294 continue ;
294295 }
295296
296- let mut manifest_path = PathBuf :: from ( & package. manifest_path ) ;
297-
298- manifest_path. pop ( ) ;
299- manifest_path. push ( & dependency. name ) ;
300- manifest_path. push ( "Cargo.toml" ) ;
297+ let dependency_package = metadata_with_deps
298+ . packages
299+ . iter ( )
300+ . find ( |p| p. name == dependency. name ) ;
301+ let manifest_path = if dependency_package. is_some ( ) {
302+ PathBuf :: from ( & dependency_package. unwrap ( ) . manifest_path )
303+ } else {
304+ let mut package_manifest_path = PathBuf :: from ( & package. manifest_path ) ;
305+ package_manifest_path. pop ( ) ;
306+ package_manifest_path. push ( & dependency. name ) ;
307+ package_manifest_path. push ( "Cargo.toml" ) ;
308+ package_manifest_path
309+ } ;
301310
302311 if manifest_path. exists ( ) {
303312 visited. insert ( dependency. name ) ;
@@ -313,7 +322,7 @@ fn get_targets_with_hitlist(
313322 hitlist : & [ String ] ,
314323 targets : & mut BTreeSet < Target > ,
315324) -> Result < ( ) , io:: Error > {
316- let metadata = get_cargo_metadata ( None ) ?;
325+ let metadata = get_cargo_metadata ( None , false ) ?;
317326
318327 let mut workspace_hitlist: BTreeSet < & String > = BTreeSet :: from_iter ( hitlist) ;
319328
@@ -399,9 +408,14 @@ fn run_rustfmt(
399408 . unwrap_or ( SUCCESS ) )
400409}
401410
402- fn get_cargo_metadata ( manifest_path : Option < & Path > ) -> Result < cargo_metadata:: Metadata , io:: Error > {
411+ fn get_cargo_metadata (
412+ manifest_path : Option < & Path > ,
413+ include_deps : bool ,
414+ ) -> Result < cargo_metadata:: Metadata , io:: Error > {
403415 let mut cmd = cargo_metadata:: MetadataCommand :: new ( ) ;
404- cmd. no_deps ( ) ;
416+ if !include_deps {
417+ cmd. no_deps ( ) ;
418+ }
405419 if let Some ( manifest_path) = manifest_path {
406420 cmd. manifest_path ( manifest_path) ;
407421 }
0 commit comments