@@ -247,7 +247,7 @@ fn get_targets(strategy: &CargoFmtStrategy) -> Result<BTreeSet<Target>, io::Erro
247
247
}
248
248
249
249
fn 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 ) ?;
251
251
let current_dir = env:: current_dir ( ) ?. canonicalize ( ) ?;
252
252
let current_dir_manifest = current_dir. join ( "Cargo.toml" ) ;
253
253
let workspace_root_path = PathBuf :: from ( & metadata. workspace_root ) . canonicalize ( ) ?;
@@ -282,7 +282,8 @@ fn get_targets_recursive(
282
282
mut targets : & mut BTreeSet < Target > ,
283
283
visited : & mut BTreeSet < String > ,
284
284
) -> 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 ) ?;
286
287
287
288
for package in metadata. packages {
288
289
add_targets ( & package. targets , & mut targets) ;
@@ -293,11 +294,19 @@ fn get_targets_recursive(
293
294
continue ;
294
295
}
295
296
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
+ } ;
301
310
302
311
if manifest_path. exists ( ) {
303
312
visited. insert ( dependency. name ) ;
@@ -313,7 +322,7 @@ fn get_targets_with_hitlist(
313
322
hitlist : & [ String ] ,
314
323
targets : & mut BTreeSet < Target > ,
315
324
) -> Result < ( ) , io:: Error > {
316
- let metadata = get_cargo_metadata ( None ) ?;
325
+ let metadata = get_cargo_metadata ( None , false ) ?;
317
326
318
327
let mut workspace_hitlist: BTreeSet < & String > = BTreeSet :: from_iter ( hitlist) ;
319
328
@@ -399,9 +408,14 @@ fn run_rustfmt(
399
408
. unwrap_or ( SUCCESS ) )
400
409
}
401
410
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 > {
403
415
let mut cmd = cargo_metadata:: MetadataCommand :: new ( ) ;
404
- cmd. no_deps ( ) ;
416
+ if !include_deps {
417
+ cmd. no_deps ( ) ;
418
+ }
405
419
if let Some ( manifest_path) = manifest_path {
406
420
cmd. manifest_path ( manifest_path) ;
407
421
}
0 commit comments