2
2
//! metadata` or `rust-project.json`) into representation stored in the salsa
3
3
//! database -- `CrateGraph`.
4
4
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} ;
6
6
7
7
use anyhow:: { format_err, Context } ;
8
8
use base_db:: {
@@ -730,6 +730,7 @@ fn project_json_to_crate_graph(
730
730
)
731
731
} ) ;
732
732
733
+ let r_a_cfg_flag = CfgFlag :: Atom ( "rust_analyzer" . to_owned ( ) ) ;
733
734
let mut cfg_cache: FxHashMap < & str , Vec < CfgFlag > > = FxHashMap :: default ( ) ;
734
735
let crates: FxHashMap < CrateId , CrateId > = project
735
736
. crates ( )
@@ -765,7 +766,12 @@ fn project_json_to_crate_graph(
765
766
* edition,
766
767
display_name. clone ( ) ,
767
768
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 ( ) ,
769
775
None ,
770
776
env,
771
777
* is_proc_macro,
@@ -820,7 +826,7 @@ fn cargo_to_crate_graph(
820
826
sysroot : Option < & Sysroot > ,
821
827
rustc_cfg : Vec < CfgFlag > ,
822
828
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
824
830
forced_cfg : Option < CfgOptions > ,
825
831
build_scripts : & WorkspaceBuildScripts ,
826
832
target_layout : TargetLayoutLoadResult ,
@@ -842,12 +848,7 @@ fn cargo_to_crate_graph(
842
848
None => ( SysrootPublicDeps :: default ( ) , None ) ,
843
849
} ;
844
850
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) ;
851
852
852
853
// Mapping of a package to its library target
853
854
let mut pkg_to_lib_crate = FxHashMap :: default ( ) ;
@@ -866,6 +867,9 @@ fn cargo_to_crate_graph(
866
867
if cargo[ pkg] . is_local {
867
868
cfg_options. insert_atom ( "test" . into ( ) ) ;
868
869
}
870
+ if cargo[ pkg] . is_member {
871
+ cfg_options. insert_atom ( "rust_analyzer" . into ( ) ) ;
872
+ }
869
873
870
874
if !override_cfg. global . is_empty ( ) {
871
875
cfg_options. apply_diff ( override_cfg. global . clone ( ) ) ;
@@ -1029,8 +1033,8 @@ fn detached_files_to_crate_graph(
1029
1033
None => ( SysrootPublicDeps :: default ( ) , None ) ,
1030
1034
} ;
1031
1035
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 ( ) ) ;
1034
1038
1035
1039
for detached_file in detached_files {
1036
1040
let file_id = match load ( detached_file) {
@@ -1295,8 +1299,7 @@ fn sysroot_to_crate_graph(
1295
1299
channel : Option < ReleaseChannel > ,
1296
1300
) -> ( SysrootPublicDeps , Option < CrateId > ) {
1297
1301
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 ( ) ) ;
1300
1303
let sysroot_crates: FxHashMap < SysrootCrate , CrateId > = match & sysroot. hack_cargo_workspace {
1301
1304
Some ( cargo) => handle_hack_cargo_workspace (
1302
1305
load,
@@ -1475,3 +1478,10 @@ fn inject_cargo_env(package: &PackageData, env: &mut Env) {
1475
1478
1476
1479
env. set ( "CARGO_PKG_LICENSE_FILE" , String :: new ( ) ) ;
1477
1480
}
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