@@ -272,8 +272,8 @@ impl CargoWorkspace {
272
272
let target = config
273
273
. target
274
274
. clone ( )
275
- . or_else ( || cargo_config_build_target ( cargo_toml, config) )
276
- . or_else ( || rustc_discover_host_triple ( cargo_toml, config) ) ;
275
+ . or_else ( || cargo_config_build_target ( cargo_toml, & config. extra_env ) )
276
+ . or_else ( || rustc_discover_host_triple ( cargo_toml, & config. extra_env ) ) ;
277
277
278
278
let mut meta = MetadataCommand :: new ( ) ;
279
279
meta. cargo_path ( toolchain:: cargo ( ) ) ;
@@ -304,12 +304,9 @@ impl CargoWorkspace {
304
304
// unclear whether cargo itself supports it.
305
305
progress ( "metadata" . to_string ( ) ) ;
306
306
307
- fn exec_with_env (
308
- command : & cargo_metadata:: MetadataCommand ,
309
- extra_env : & FxHashMap < String , String > ,
310
- ) -> Result < cargo_metadata:: Metadata , cargo_metadata:: Error > {
311
- let mut command = command. cargo_command ( ) ;
312
- command. envs ( extra_env) ;
307
+ ( || -> Result < cargo_metadata:: Metadata , cargo_metadata:: Error > {
308
+ let mut command = meta. cargo_command ( ) ;
309
+ command. envs ( & config. extra_env ) ;
313
310
let output = command. output ( ) ?;
314
311
if !output. status . success ( ) {
315
312
return Err ( cargo_metadata:: Error :: CargoMetadata {
@@ -321,12 +318,8 @@ impl CargoWorkspace {
321
318
. find ( |line| line. starts_with ( '{' ) )
322
319
. ok_or ( cargo_metadata:: Error :: NoJson ) ?;
323
320
cargo_metadata:: MetadataCommand :: parse ( stdout)
324
- }
325
-
326
- let meta = exec_with_env ( & meta, & config. extra_env )
327
- . with_context ( || format ! ( "Failed to run `{:?}`" , meta. cargo_command( ) ) ) ?;
328
-
329
- Ok ( meta)
321
+ } ) ( )
322
+ . with_context ( || format ! ( "Failed to run `{:?}`" , meta. cargo_command( ) ) )
330
323
}
331
324
332
325
pub fn new ( mut meta : cargo_metadata:: Metadata ) -> CargoWorkspace {
@@ -395,32 +388,14 @@ impl CargoWorkspace {
395
388
}
396
389
let resolve = meta. resolve . expect ( "metadata executed with deps" ) ;
397
390
for mut node in resolve. nodes {
398
- let source = match pkg_by_id. get ( & node. id ) {
399
- Some ( & src) => src,
400
- // FIXME: replace this and a similar branch below with `.unwrap`, once
401
- // https://github.com/rust-lang/cargo/issues/7841
402
- // is fixed and hits stable (around 1.43-is probably?).
403
- None => {
404
- tracing:: error!( "Node id do not match in cargo metadata, ignoring {}" , node. id) ;
405
- continue ;
406
- }
407
- } ;
391
+ let & source = pkg_by_id. get ( & node. id ) . unwrap ( ) ;
408
392
node. deps . sort_by ( |a, b| a. pkg . cmp ( & b. pkg ) ) ;
409
- for ( dep_node , kind ) in node
393
+ let dependencies = node
410
394
. deps
411
395
. iter ( )
412
- . flat_map ( |dep| DepKind :: iter ( & dep. dep_kinds ) . map ( move |kind| ( dep, kind) ) )
413
- {
414
- let pkg = match pkg_by_id. get ( & dep_node. pkg ) {
415
- Some ( & pkg) => pkg,
416
- None => {
417
- tracing:: error!(
418
- "Dep node id do not match in cargo metadata, ignoring {}" ,
419
- dep_node. pkg
420
- ) ;
421
- continue ;
422
- }
423
- } ;
396
+ . flat_map ( |dep| DepKind :: iter ( & dep. dep_kinds ) . map ( move |kind| ( dep, kind) ) ) ;
397
+ for ( dep_node, kind) in dependencies {
398
+ let & pkg = pkg_by_id. get ( & dep_node. pkg ) . unwrap ( ) ;
424
399
let dep = PackageDependency { name : dep_node. name . clone ( ) , pkg, kind } ;
425
400
packages[ source] . dependencies . push ( dep) ;
426
401
}
@@ -465,10 +440,7 @@ impl CargoWorkspace {
465
440
found = true
466
441
}
467
442
self [ pkg] . dependencies . iter ( ) . find_map ( |dep| {
468
- if & self [ dep. pkg ] . manifest == manifest_path {
469
- return Some ( self [ pkg] . manifest . clone ( ) ) ;
470
- }
471
- None
443
+ ( & self [ dep. pkg ] . manifest == manifest_path) . then ( || self [ pkg] . manifest . clone ( ) )
472
444
} )
473
445
} )
474
446
. collect :: < Vec < ManifestPath > > ( ) ;
@@ -494,9 +466,12 @@ impl CargoWorkspace {
494
466
}
495
467
}
496
468
497
- fn rustc_discover_host_triple ( cargo_toml : & ManifestPath , config : & CargoConfig ) -> Option < String > {
469
+ fn rustc_discover_host_triple (
470
+ cargo_toml : & ManifestPath ,
471
+ extra_env : & FxHashMap < String , String > ,
472
+ ) -> Option < String > {
498
473
let mut rustc = Command :: new ( toolchain:: rustc ( ) ) ;
499
- rustc. envs ( & config . extra_env ) ;
474
+ rustc. envs ( extra_env) ;
500
475
rustc. current_dir ( cargo_toml. parent ( ) ) . arg ( "-vV" ) ;
501
476
tracing:: debug!( "Discovering host platform by {:?}" , rustc) ;
502
477
match utf8_stdout ( rustc) {
@@ -518,9 +493,12 @@ fn rustc_discover_host_triple(cargo_toml: &ManifestPath, config: &CargoConfig) -
518
493
}
519
494
}
520
495
521
- fn cargo_config_build_target ( cargo_toml : & ManifestPath , config : & CargoConfig ) -> Option < String > {
496
+ fn cargo_config_build_target (
497
+ cargo_toml : & ManifestPath ,
498
+ extra_env : & FxHashMap < String , String > ,
499
+ ) -> Option < String > {
522
500
let mut cargo_config = Command :: new ( toolchain:: cargo ( ) ) ;
523
- cargo_config. envs ( & config . extra_env ) ;
501
+ cargo_config. envs ( extra_env) ;
524
502
cargo_config
525
503
. current_dir ( cargo_toml. parent ( ) )
526
504
. args ( & [ "-Z" , "unstable-options" , "config" , "get" , "build.target" ] )
0 commit comments