@@ -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
}
@@ -479,21 +491,25 @@ impl ProjectWorkspace {
479
491
include : vec ! [ detached_file. clone( ) ] ,
480
492
exclude : Vec :: new ( ) ,
481
493
} )
482
- . chain ( mk_sysroot ( Some ( sysroot) ) )
494
+ . chain ( mk_sysroot ( sysroot. as_ref ( ) ) )
483
495
. collect ( ) ,
484
496
}
485
497
}
486
498
487
499
pub fn n_packages ( & self ) -> usize {
488
500
match self {
489
- ProjectWorkspace :: Json { project, .. } => project. n_crates ( ) ,
501
+ ProjectWorkspace :: Json { project, sysroot, .. } => {
502
+ let sysroot_package_len = sysroot. as_ref ( ) . map_or ( 0 , |it| it. crates ( ) . len ( ) ) ;
503
+ sysroot_package_len + project. n_crates ( )
504
+ }
490
505
ProjectWorkspace :: Cargo { cargo, sysroot, rustc, .. } => {
491
506
let rustc_package_len = rustc. as_ref ( ) . map_or ( 0 , |it| it. packages ( ) . len ( ) ) ;
492
507
let sysroot_package_len = sysroot. as_ref ( ) . map_or ( 0 , |it| it. crates ( ) . len ( ) ) ;
493
508
cargo. packages ( ) . len ( ) + sysroot_package_len + rustc_package_len
494
509
}
495
510
ProjectWorkspace :: DetachedFiles { sysroot, files, .. } => {
496
- sysroot. crates ( ) . len ( ) + files. len ( )
511
+ let sysroot_package_len = sysroot. as_ref ( ) . map_or ( 0 , |it| it. crates ( ) . len ( ) ) ;
512
+ sysroot_package_len + files. len ( )
497
513
}
498
514
}
499
515
}
@@ -805,12 +821,14 @@ fn detached_files_to_crate_graph(
805
821
rustc_cfg : Vec < CfgFlag > ,
806
822
load : & mut dyn FnMut ( & AbsPath ) -> Option < FileId > ,
807
823
detached_files : & [ AbsPathBuf ] ,
808
- sysroot : & Sysroot ,
824
+ sysroot : & Option < Sysroot > ,
809
825
) -> CrateGraph {
810
826
let _p = profile:: span ( "detached_files_to_crate_graph" ) ;
811
827
let mut crate_graph = CrateGraph :: default ( ) ;
812
- let ( public_deps, _libproc_macro) =
813
- sysroot_to_crate_graph ( & mut crate_graph, sysroot, rustc_cfg. clone ( ) , load) ;
828
+ let ( public_deps, _libproc_macro) = match sysroot {
829
+ Some ( sysroot) => sysroot_to_crate_graph ( & mut crate_graph, sysroot, rustc_cfg. clone ( ) , load) ,
830
+ None => ( SysrootPublicDeps :: default ( ) , None ) ,
831
+ } ;
814
832
815
833
let mut cfg_options = CfgOptions :: default ( ) ;
816
834
cfg_options. extend ( rustc_cfg) ;
0 commit comments