@@ -73,6 +73,7 @@ pub enum ProjectWorkspace {
7373 cfg_overrides : CfgOverrides ,
7474 toolchain : Option < Version > ,
7575 target_layout : Result < String , String > ,
76+ cargo_config_extra_env : FxHashMap < String , String > ,
7677 } ,
7778 /// Project workspace was manually specified using a `rust-project.json` file.
7879 Json {
@@ -115,7 +116,8 @@ impl fmt::Debug for ProjectWorkspace {
115116 rustc_cfg,
116117 cfg_overrides,
117118 toolchain,
118- target_layout : data_layout,
119+ target_layout,
120+ cargo_config_extra_env,
119121 } => f
120122 . debug_struct ( "Cargo" )
121123 . field ( "root" , & cargo. workspace_root ( ) . file_name ( ) )
@@ -128,7 +130,8 @@ impl fmt::Debug for ProjectWorkspace {
128130 . field ( "n_rustc_cfg" , & rustc_cfg. len ( ) )
129131 . field ( "n_cfg_overrides" , & cfg_overrides. len ( ) )
130132 . field ( "toolchain" , & toolchain)
131- . field ( "data_layout" , & data_layout)
133+ . field ( "data_layout" , & target_layout)
134+ . field ( "cargo_config_extra_env" , & cargo_config_extra_env)
132135 . finish ( ) ,
133136 ProjectWorkspace :: Json {
134137 project,
@@ -320,6 +323,8 @@ impl ProjectWorkspace {
320323 } ) ?;
321324 let cargo = CargoWorkspace :: new ( meta) ;
322325
326+ let cargo_config_extra_env =
327+ cargo_config_env ( cargo_toml, & config. extra_env , sysroot_ref) ;
323328 ProjectWorkspace :: Cargo {
324329 cargo,
325330 build_scripts : WorkspaceBuildScripts :: default ( ) ,
@@ -329,6 +334,7 @@ impl ProjectWorkspace {
329334 cfg_overrides,
330335 toolchain,
331336 target_layout : data_layout. map_err ( |it| it. to_string ( ) ) ,
337+ cargo_config_extra_env,
332338 }
333339 }
334340 } ;
@@ -589,6 +595,7 @@ impl ProjectWorkspace {
589595 build_scripts,
590596 toolchain : _,
591597 target_layout : _,
598+ cargo_config_extra_env : _,
592599 } => {
593600 cargo
594601 . packages ( )
@@ -700,6 +707,7 @@ impl ProjectWorkspace {
700707 build_scripts,
701708 toolchain,
702709 target_layout,
710+ cargo_config_extra_env : _,
703711 } => cargo_to_crate_graph (
704712 load,
705713 rustc. as_ref ( ) . map ( |a| a. as_ref ( ) ) . ok ( ) ,
@@ -742,6 +750,7 @@ impl ProjectWorkspace {
742750 rustc_cfg,
743751 cfg_overrides,
744752 toolchain,
753+ cargo_config_extra_env,
745754 build_scripts : _,
746755 target_layout : _,
747756 } ,
@@ -752,6 +761,7 @@ impl ProjectWorkspace {
752761 rustc_cfg : o_rustc_cfg,
753762 cfg_overrides : o_cfg_overrides,
754763 toolchain : o_toolchain,
764+ cargo_config_extra_env : o_cargo_config_extra_env,
755765 build_scripts : _,
756766 target_layout : _,
757767 } ,
@@ -762,6 +772,7 @@ impl ProjectWorkspace {
762772 && cfg_overrides == o_cfg_overrides
763773 && toolchain == o_toolchain
764774 && sysroot == o_sysroot
775+ && cargo_config_extra_env == o_cargo_config_extra_env
765776 }
766777 (
767778 Self :: Json { project, sysroot, rustc_cfg, toolchain, target_layout : _ } ,
@@ -1598,3 +1609,31 @@ fn create_cfg_options(rustc_cfg: Vec<CfgFlag>) -> CfgOptions {
15981609 cfg_options. insert_atom ( "debug_assertions" . into ( ) ) ;
15991610 cfg_options
16001611}
1612+
1613+ fn cargo_config_env (
1614+ cargo_toml : & ManifestPath ,
1615+ extra_env : & FxHashMap < String , String > ,
1616+ sysroot : Option < & Sysroot > ,
1617+ ) -> FxHashMap < String , String > {
1618+ let Ok ( program) = Sysroot :: discover_tool ( sysroot, toolchain:: Tool :: Cargo ) else {
1619+ return Default :: default ( ) ;
1620+ } ;
1621+ let mut cargo_config = Command :: new ( program) ;
1622+ cargo_config. envs ( extra_env) ;
1623+ cargo_config
1624+ . current_dir ( cargo_toml. parent ( ) )
1625+ . args ( [ "-Z" , "unstable-options" , "config" , "get" , "env" ] )
1626+ . env ( "RUSTC_BOOTSTRAP" , "1" ) ;
1627+ // if successful we receive `env.key.value = "value" per entry
1628+ tracing:: debug!( "Discovering cargo config env by {:?}" , cargo_config) ;
1629+ utf8_stdout ( cargo_config) . map ( parse_output_cargo_config_env) . unwrap_or_default ( )
1630+ }
1631+
1632+ fn parse_output_cargo_config_env ( stdout : String ) -> FxHashMap < String , String > {
1633+ stdout
1634+ . lines ( )
1635+ . filter_map ( |l| l. strip_prefix ( "env." ) )
1636+ . filter_map ( |l| l. split_once ( ".value = " ) )
1637+ . map ( |( key, value) | ( key. to_owned ( ) , value. trim_matches ( '"' ) . to_owned ( ) ) )
1638+ . collect ( )
1639+ }
0 commit comments