22//! metadata` or `rust-project.json`) into representation stored in the salsa
33//! database -- `CrateGraph`.
44
5- use std:: { collections:: VecDeque , fmt, fs, process:: Command , str:: FromStr , sync} ;
5+ use std:: { collections:: VecDeque , fmt, fs, iter , process:: Command , str:: FromStr , sync} ;
66
77use anyhow:: { format_err, Context } ;
88use base_db:: {
@@ -730,6 +730,7 @@ fn project_json_to_crate_graph(
730730 )
731731 } ) ;
732732
733+ let r_a_cfg_flag = CfgFlag :: Atom ( "rust_analyzer" . to_owned ( ) ) ;
733734 let mut cfg_cache: FxHashMap < & str , Vec < CfgFlag > > = FxHashMap :: default ( ) ;
734735 let crates: FxHashMap < CrateId , CrateId > = project
735736 . crates ( )
@@ -765,7 +766,12 @@ fn project_json_to_crate_graph(
765766 * edition,
766767 display_name. clone ( ) ,
767768 version. clone ( ) ,
768- target_cfgs. iter ( ) . chain ( cfg. iter ( ) ) . cloned ( ) . collect ( ) ,
769+ target_cfgs
770+ . iter ( )
771+ . chain ( cfg. iter ( ) )
772+ . chain ( iter:: once ( & r_a_cfg_flag) )
773+ . cloned ( )
774+ . collect ( ) ,
769775 None ,
770776 env,
771777 * is_proc_macro,
@@ -820,7 +826,7 @@ fn cargo_to_crate_graph(
820826 sysroot : Option < & Sysroot > ,
821827 rustc_cfg : Vec < CfgFlag > ,
822828 override_cfg : & CfgOverrides ,
823- // Don't compute cfg and use this if present
829+ // Don't compute cfg and use this if present, only used for the sysroot experiment hack
824830 forced_cfg : Option < CfgOptions > ,
825831 build_scripts : & WorkspaceBuildScripts ,
826832 target_layout : TargetLayoutLoadResult ,
@@ -842,12 +848,7 @@ fn cargo_to_crate_graph(
842848 None => ( SysrootPublicDeps :: default ( ) , None ) ,
843849 } ;
844850
845- let cfg_options = {
846- let mut cfg_options = CfgOptions :: default ( ) ;
847- cfg_options. extend ( rustc_cfg) ;
848- cfg_options. insert_atom ( "debug_assertions" . into ( ) ) ;
849- cfg_options
850- } ;
851+ let cfg_options = create_cfg_options ( rustc_cfg) ;
851852
852853 // Mapping of a package to its library target
853854 let mut pkg_to_lib_crate = FxHashMap :: default ( ) ;
@@ -866,6 +867,9 @@ fn cargo_to_crate_graph(
866867 if cargo[ pkg] . is_local {
867868 cfg_options. insert_atom ( "test" . into ( ) ) ;
868869 }
870+ if cargo[ pkg] . is_member {
871+ cfg_options. insert_atom ( "rust_analyzer" . into ( ) ) ;
872+ }
869873
870874 if !override_cfg. global . is_empty ( ) {
871875 cfg_options. apply_diff ( override_cfg. global . clone ( ) ) ;
@@ -1029,8 +1033,8 @@ fn detached_files_to_crate_graph(
10291033 None => ( SysrootPublicDeps :: default ( ) , None ) ,
10301034 } ;
10311035
1032- let mut cfg_options = CfgOptions :: default ( ) ;
1033- cfg_options. extend ( rustc_cfg ) ;
1036+ let mut cfg_options = create_cfg_options ( rustc_cfg ) ;
1037+ cfg_options. insert_atom ( "rust_analyzer" . into ( ) ) ;
10341038
10351039 for detached_file in detached_files {
10361040 let file_id = match load ( detached_file) {
@@ -1295,8 +1299,7 @@ fn sysroot_to_crate_graph(
12951299 channel : Option < ReleaseChannel > ,
12961300) -> ( SysrootPublicDeps , Option < CrateId > ) {
12971301 let _p = profile:: span ( "sysroot_to_crate_graph" ) ;
1298- let mut cfg_options = CfgOptions :: default ( ) ;
1299- cfg_options. extend ( rustc_cfg. clone ( ) ) ;
1302+ let cfg_options = create_cfg_options ( rustc_cfg. clone ( ) ) ;
13001303 let sysroot_crates: FxHashMap < SysrootCrate , CrateId > = match & sysroot. hack_cargo_workspace {
13011304 Some ( cargo) => handle_hack_cargo_workspace (
13021305 load,
@@ -1475,3 +1478,10 @@ fn inject_cargo_env(package: &PackageData, env: &mut Env) {
14751478
14761479 env. set ( "CARGO_PKG_LICENSE_FILE" , String :: new ( ) ) ;
14771480}
1481+
1482+ fn create_cfg_options ( rustc_cfg : Vec < CfgFlag > ) -> CfgOptions {
1483+ let mut cfg_options = CfgOptions :: default ( ) ;
1484+ cfg_options. extend ( rustc_cfg) ;
1485+ cfg_options. insert_atom ( "debug_assertions" . into ( ) ) ;
1486+ cfg_options
1487+ }
0 commit comments