@@ -93,7 +93,7 @@ pub enum ProjectWorkspace {
93
93
// //
94
94
/// Project with a set of disjoint files, not belonging to any particular workspace.
95
95
/// Backed by basic sysroot crates for basic completion and highlighting.
96
- DetachedFiles { files : Vec < AbsPathBuf > , sysroot : Sysroot , rustc_cfg : Vec < CfgFlag > } ,
96
+ DetachedFiles { files : Vec < AbsPathBuf > , sysroot : Option < Sysroot > , rustc_cfg : Vec < CfgFlag > } ,
97
97
}
98
98
99
99
impl fmt:: Debug for ProjectWorkspace {
@@ -133,7 +133,7 @@ impl fmt::Debug for ProjectWorkspace {
133
133
ProjectWorkspace :: DetachedFiles { files, sysroot, rustc_cfg } => f
134
134
. debug_struct ( "DetachedFiles" )
135
135
. field ( "n_files" , & files. len ( ) )
136
- . field ( "n_sysroot_crates " , & sysroot. crates ( ) . len ( ) )
136
+ . field ( "sysroot " , & sysroot. is_some ( ) )
137
137
. field ( "n_rustc_cfg" , & rustc_cfg. len ( ) )
138
138
. finish ( ) ,
139
139
}
@@ -191,10 +191,7 @@ impl ProjectWorkspace {
191
191
let sysroot = match & config. sysroot {
192
192
Some ( RustcSource :: Path ( path) ) => {
193
193
Some ( Sysroot :: with_sysroot_dir ( path. clone ( ) ) . with_context ( || {
194
- format ! (
195
- "Failed to find sysroot for Cargo.toml file {}." ,
196
- cargo_toml. display( )
197
- )
194
+ format ! ( "Failed to find sysroot at {}." , path. display( ) )
198
195
} ) ?)
199
196
}
200
197
Some ( RustcSource :: Discover ) => Some (
@@ -291,14 +288,29 @@ impl ProjectWorkspace {
291
288
Ok ( ProjectWorkspace :: Json { project : project_json, sysroot, rustc_cfg } )
292
289
}
293
290
294
- pub fn load_detached_files ( detached_files : Vec < AbsPathBuf > ) -> Result < ProjectWorkspace > {
295
- let sysroot = Sysroot :: discover (
296
- detached_files
297
- . first ( )
298
- . and_then ( |it| it. parent ( ) )
299
- . ok_or_else ( || format_err ! ( "No detached files to load" ) ) ?,
300
- & Default :: default ( ) ,
301
- ) ?;
291
+ pub fn load_detached_files (
292
+ detached_files : Vec < AbsPathBuf > ,
293
+ config : & CargoConfig ,
294
+ ) -> Result < ProjectWorkspace > {
295
+ let sysroot = match & config. sysroot {
296
+ Some ( RustcSource :: Path ( path) ) => Some (
297
+ Sysroot :: with_sysroot_dir ( path. clone ( ) )
298
+ . with_context ( || format ! ( "Failed to find sysroot at {}." , path. display( ) ) ) ?,
299
+ ) ,
300
+ Some ( RustcSource :: Discover ) => {
301
+ let dir = & detached_files
302
+ . first ( )
303
+ . and_then ( |it| it. parent ( ) )
304
+ . ok_or_else ( || format_err ! ( "No detached files to load" ) ) ?;
305
+ Some ( Sysroot :: discover ( dir, & config. extra_env ) . with_context ( || {
306
+ format ! ( "Failed to find sysroot in {}. Is rust-src installed?" , dir. display( ) )
307
+ } ) ?)
308
+ }
309
+ None => None ,
310
+ } ;
311
+ if let Some ( sysroot) = & sysroot {
312
+ tracing:: info!( src_root = %sysroot. src_root( ) . display( ) , root = %sysroot. root( ) . display( ) , "Using sysroot" ) ;
313
+ }
302
314
let rustc_cfg = rustc_cfg:: get ( None , None , & Default :: default ( ) ) ;
303
315
Ok ( ProjectWorkspace :: DetachedFiles { files : detached_files, sysroot, rustc_cfg } )
304
316
}
@@ -464,21 +476,25 @@ impl ProjectWorkspace {
464
476
include : vec ! [ detached_file. clone( ) ] ,
465
477
exclude : Vec :: new ( ) ,
466
478
} )
467
- . chain ( mk_sysroot ( Some ( sysroot) ) )
479
+ . chain ( mk_sysroot ( sysroot. as_ref ( ) ) )
468
480
. collect ( ) ,
469
481
}
470
482
}
471
483
472
484
pub fn n_packages ( & self ) -> usize {
473
485
match self {
474
- ProjectWorkspace :: Json { project, .. } => project. n_crates ( ) ,
486
+ ProjectWorkspace :: Json { project, sysroot, .. } => {
487
+ let sysroot_package_len = sysroot. as_ref ( ) . map_or ( 0 , |it| it. crates ( ) . len ( ) ) ;
488
+ sysroot_package_len + project. n_crates ( )
489
+ }
475
490
ProjectWorkspace :: Cargo { cargo, sysroot, rustc, .. } => {
476
491
let rustc_package_len = rustc. as_ref ( ) . map_or ( 0 , |it| it. packages ( ) . len ( ) ) ;
477
492
let sysroot_package_len = sysroot. as_ref ( ) . map_or ( 0 , |it| it. crates ( ) . len ( ) ) ;
478
493
cargo. packages ( ) . len ( ) + sysroot_package_len + rustc_package_len
479
494
}
480
495
ProjectWorkspace :: DetachedFiles { sysroot, files, .. } => {
481
- sysroot. crates ( ) . len ( ) + files. len ( )
496
+ let sysroot_package_len = sysroot. as_ref ( ) . map_or ( 0 , |it| it. crates ( ) . len ( ) ) ;
497
+ sysroot_package_len + files. len ( )
482
498
}
483
499
}
484
500
}
@@ -790,12 +806,14 @@ fn detached_files_to_crate_graph(
790
806
rustc_cfg : Vec < CfgFlag > ,
791
807
load : & mut dyn FnMut ( & AbsPath ) -> Option < FileId > ,
792
808
detached_files : & [ AbsPathBuf ] ,
793
- sysroot : & Sysroot ,
809
+ sysroot : & Option < Sysroot > ,
794
810
) -> CrateGraph {
795
811
let _p = profile:: span ( "detached_files_to_crate_graph" ) ;
796
812
let mut crate_graph = CrateGraph :: default ( ) ;
797
- let ( public_deps, _libproc_macro) =
798
- sysroot_to_crate_graph ( & mut crate_graph, sysroot, rustc_cfg. clone ( ) , load) ;
813
+ let ( public_deps, _libproc_macro) = match sysroot {
814
+ Some ( sysroot) => sysroot_to_crate_graph ( & mut crate_graph, sysroot, rustc_cfg. clone ( ) , load) ,
815
+ None => ( SysrootPublicDeps :: default ( ) , None ) ,
816
+ } ;
799
817
800
818
let mut cfg_options = CfgOptions :: default ( ) ;
801
819
cfg_options. extend ( rustc_cfg) ;
0 commit comments