@@ -24,7 +24,7 @@ use crate::{
2424 rustc_cfg,
2525 sysroot:: SysrootCrate ,
2626 target_data_layout, utf8_stdout, CargoConfig , CargoWorkspace , InvocationStrategy , ManifestPath ,
27- Package , ProjectJson , ProjectManifest , Sysroot , TargetKind , WorkspaceBuildScripts ,
27+ Package , ProjectJson , ProjectManifest , Sysroot , TargetData , TargetKind , WorkspaceBuildScripts ,
2828} ;
2929
3030/// A set of cfg-overrides per crate.
@@ -900,7 +900,24 @@ fn cargo_to_crate_graph(
900900 // https://github.com/rust-lang/rust-analyzer/issues/11300
901901 continue ;
902902 }
903- let Some ( file_id) = load ( & cargo[ tgt] . root ) else { continue } ;
903+ let & TargetData { ref name, kind, is_proc_macro, ref root, .. } = & cargo[ tgt] ;
904+
905+ if kind == TargetKind :: Lib
906+ && sysroot. map_or ( false , |sysroot| root. starts_with ( sysroot. src_root ( ) ) )
907+ {
908+ if let Some ( & ( _, crate_id, _) ) =
909+ public_deps. deps . iter ( ) . find ( |( dep_name, ..) | dep_name. as_smol_str ( ) == name)
910+ {
911+ pkg_crates. entry ( pkg) . or_insert_with ( Vec :: new) . push ( ( crate_id, kind) ) ;
912+
913+ lib_tgt = Some ( ( crate_id, name. clone ( ) ) ) ;
914+ pkg_to_lib_crate. insert ( pkg, crate_id) ;
915+ // sysroot is inside the workspace, prevent the sysroot crates from being duplicated here
916+ continue ;
917+ }
918+ }
919+
920+ let Some ( file_id) = load ( root) else { continue } ;
904921
905922 let crate_id = add_target_crate_root (
906923 crate_graph,
@@ -909,23 +926,23 @@ fn cargo_to_crate_graph(
909926 build_scripts. get_output ( pkg) ,
910927 cfg_options. clone ( ) ,
911928 file_id,
912- & cargo [ tgt ] . name ,
913- cargo [ tgt ] . is_proc_macro ,
929+ name,
930+ is_proc_macro,
914931 target_layout. clone ( ) ,
915932 false ,
916933 channel,
917934 ) ;
918- if cargo [ tgt ] . kind == TargetKind :: Lib {
919- lib_tgt = Some ( ( crate_id, cargo [ tgt ] . name . clone ( ) ) ) ;
935+ if kind == TargetKind :: Lib {
936+ lib_tgt = Some ( ( crate_id, name. clone ( ) ) ) ;
920937 pkg_to_lib_crate. insert ( pkg, crate_id) ;
921938 }
922939 // Even crates that don't set proc-macro = true are allowed to depend on proc_macro
923940 // (just none of the APIs work when called outside of a proc macro).
924941 if let Some ( proc_macro) = libproc_macro {
925- add_proc_macro_dep ( crate_graph, crate_id, proc_macro, cargo [ tgt ] . is_proc_macro ) ;
942+ add_proc_macro_dep ( crate_graph, crate_id, proc_macro, is_proc_macro) ;
926943 }
927944
928- pkg_crates. entry ( pkg) . or_insert_with ( Vec :: new) . push ( ( crate_id, cargo [ tgt ] . kind ) ) ;
945+ pkg_crates. entry ( pkg) . or_insert_with ( Vec :: new) . push ( ( crate_id, kind) ) ;
929946 }
930947
931948 // Set deps to the core, std and to the lib target of the current package
0 commit comments