@@ -272,8 +272,8 @@ impl CargoWorkspace {
272272 let target = config
273273 . target
274274 . 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 ) ) ;
277277
278278 let mut meta = MetadataCommand :: new ( ) ;
279279 meta. cargo_path ( toolchain:: cargo ( ) ) ;
@@ -304,12 +304,9 @@ impl CargoWorkspace {
304304 // unclear whether cargo itself supports it.
305305 progress ( "metadata" . to_string ( ) ) ;
306306
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 ) ;
313310 let output = command. output ( ) ?;
314311 if !output. status . success ( ) {
315312 return Err ( cargo_metadata:: Error :: CargoMetadata {
@@ -321,12 +318,8 @@ impl CargoWorkspace {
321318 . find ( |line| line. starts_with ( '{' ) )
322319 . ok_or ( cargo_metadata:: Error :: NoJson ) ?;
323320 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( ) ) )
330323 }
331324
332325 pub fn new ( mut meta : cargo_metadata:: Metadata ) -> CargoWorkspace {
@@ -395,32 +388,14 @@ impl CargoWorkspace {
395388 }
396389 let resolve = meta. resolve . expect ( "metadata executed with deps" ) ;
397390 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 ( ) ;
408392 node. deps . sort_by ( |a, b| a. pkg . cmp ( & b. pkg ) ) ;
409- for ( dep_node , kind ) in node
393+ let dependencies = node
410394 . deps
411395 . 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 ( ) ;
424399 let dep = PackageDependency { name : dep_node. name . clone ( ) , pkg, kind } ;
425400 packages[ source] . dependencies . push ( dep) ;
426401 }
@@ -465,10 +440,7 @@ impl CargoWorkspace {
465440 found = true
466441 }
467442 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 ( ) )
472444 } )
473445 } )
474446 . collect :: < Vec < ManifestPath > > ( ) ;
@@ -494,9 +466,12 @@ impl CargoWorkspace {
494466 }
495467}
496468
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 > {
498473 let mut rustc = Command :: new ( toolchain:: rustc ( ) ) ;
499- rustc. envs ( & config . extra_env ) ;
474+ rustc. envs ( extra_env) ;
500475 rustc. current_dir ( cargo_toml. parent ( ) ) . arg ( "-vV" ) ;
501476 tracing:: debug!( "Discovering host platform by {:?}" , rustc) ;
502477 match utf8_stdout ( rustc) {
@@ -518,9 +493,12 @@ fn rustc_discover_host_triple(cargo_toml: &ManifestPath, config: &CargoConfig) -
518493 }
519494}
520495
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 > {
522500 let mut cargo_config = Command :: new ( toolchain:: cargo ( ) ) ;
523- cargo_config. envs ( & config . extra_env ) ;
501+ cargo_config. envs ( extra_env) ;
524502 cargo_config
525503 . current_dir ( cargo_toml. parent ( ) )
526504 . args ( & [ "-Z" , "unstable-options" , "config" , "get" , "build.target" ] )
0 commit comments