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 ( ) ;
@@ -1029,8 +1030,7 @@ fn detached_files_to_crate_graph(
10291030 None => ( SysrootPublicDeps :: default ( ) , None ) ,
10301031 } ;
10311032
1032- let mut cfg_options = CfgOptions :: default ( ) ;
1033- cfg_options. extend ( rustc_cfg) ;
1033+ let cfg_options = create_cfg_options ( rustc_cfg) ;
10341034
10351035 for detached_file in detached_files {
10361036 let file_id = match load ( detached_file) {
@@ -1295,8 +1295,7 @@ fn sysroot_to_crate_graph(
12951295 channel : Option < ReleaseChannel > ,
12961296) -> ( SysrootPublicDeps , Option < CrateId > ) {
12971297 let _p = profile:: span ( "sysroot_to_crate_graph" ) ;
1298- let mut cfg_options = CfgOptions :: default ( ) ;
1299- cfg_options. extend ( rustc_cfg. clone ( ) ) ;
1298+ let cfg_options = create_cfg_options ( rustc_cfg. clone ( ) ) ;
13001299 let sysroot_crates: FxHashMap < SysrootCrate , CrateId > = match & sysroot. hack_cargo_workspace {
13011300 Some ( cargo) => handle_hack_cargo_workspace (
13021301 load,
@@ -1475,3 +1474,11 @@ fn inject_cargo_env(package: &PackageData, env: &mut Env) {
14751474
14761475 env. set ( "CARGO_PKG_LICENSE_FILE" , String :: new ( ) ) ;
14771476}
1477+
1478+ fn create_cfg_options ( rustc_cfg : Vec < CfgFlag > ) -> CfgOptions {
1479+ let mut cfg_options = CfgOptions :: default ( ) ;
1480+ cfg_options. extend ( rustc_cfg) ;
1481+ cfg_options. insert_atom ( "debug_assertions" . into ( ) ) ;
1482+ cfg_options. insert_atom ( "rust_analyzer" . into ( ) ) ;
1483+ cfg_options
1484+ }
0 commit comments