@@ -15,6 +15,7 @@ use crate::utf8_stdout;
15
15
#[ derive( Default , Debug , Clone , Eq , PartialEq ) ]
16
16
pub struct Sysroot {
17
17
crates : Arena < SysrootCrateData > ,
18
+ pub rustc_src_dir : Option < AbsPathBuf > ,
18
19
}
19
20
20
21
pub ( crate ) type SysrootCrate = Idx < SysrootCrateData > ;
@@ -51,13 +52,15 @@ impl Sysroot {
51
52
pub fn discover ( cargo_toml : & AbsPath ) -> Result < Sysroot > {
52
53
log:: debug!( "Discovering sysroot for {}" , cargo_toml. display( ) ) ;
53
54
let current_dir = cargo_toml. parent ( ) . unwrap ( ) ;
54
- let sysroot_src_dir = discover_sysroot_src_dir ( current_dir) ?;
55
- let res = Sysroot :: load ( & sysroot_src_dir) ?;
55
+ let sysroot_dir = discover_sysroot_dir ( current_dir) ?;
56
+ let sysroot_src_dir = discover_sysroot_src_dir ( & sysroot_dir, current_dir) ?;
57
+ let rustc_src_dir = get_rustc_src ( & sysroot_dir) ;
58
+ let res = Sysroot :: load ( & sysroot_src_dir, rustc_src_dir) ?;
56
59
Ok ( res)
57
60
}
58
61
59
- pub fn load ( sysroot_src_dir : & AbsPath ) -> Result < Sysroot > {
60
- let mut sysroot = Sysroot { crates : Arena :: default ( ) } ;
62
+ pub fn load ( sysroot_src_dir : & AbsPath , rustc_src_dir : Option < AbsPathBuf > ) -> Result < Sysroot > {
63
+ let mut sysroot = Sysroot { crates : Arena :: default ( ) , rustc_src_dir } ;
61
64
62
65
for name in SYSROOT_CRATES . trim ( ) . lines ( ) {
63
66
let root = [ format ! ( "{}/src/lib.rs" , name) , format ! ( "lib{}/lib.rs" , name) ]
@@ -110,7 +113,18 @@ impl Sysroot {
110
113
}
111
114
}
112
115
113
- fn discover_sysroot_src_dir ( current_dir : & AbsPath ) -> Result < AbsPathBuf > {
116
+ fn discover_sysroot_dir ( current_dir : & AbsPath ) -> Result < AbsPathBuf > {
117
+ let mut rustc = Command :: new ( toolchain:: rustc ( ) ) ;
118
+ rustc. current_dir ( current_dir) . args ( & [ "--print" , "sysroot" ] ) ;
119
+ log:: debug!( "Discovering sysroot by {:?}" , rustc) ;
120
+ let stdout = utf8_stdout ( rustc) ?;
121
+ Ok ( AbsPathBuf :: assert ( PathBuf :: from ( stdout) ) )
122
+ }
123
+
124
+ fn discover_sysroot_src_dir (
125
+ sysroot_path : & AbsPathBuf ,
126
+ current_dir : & AbsPath ,
127
+ ) -> Result < AbsPathBuf > {
114
128
if let Ok ( path) = env:: var ( "RUST_SRC_PATH" ) {
115
129
let path = AbsPathBuf :: try_from ( path. as_str ( ) )
116
130
. map_err ( |path| format_err ! ( "RUST_SRC_PATH must be absolute: {}" , path. display( ) ) ) ?;
@@ -122,14 +136,6 @@ fn discover_sysroot_src_dir(current_dir: &AbsPath) -> Result<AbsPathBuf> {
122
136
log:: debug!( "RUST_SRC_PATH is set, but is invalid (no core: {:?}), ignoring" , core) ;
123
137
}
124
138
125
- let sysroot_path = {
126
- let mut rustc = Command :: new ( toolchain:: rustc ( ) ) ;
127
- rustc. current_dir ( current_dir) . args ( & [ "--print" , "sysroot" ] ) ;
128
- log:: debug!( "Discovering sysroot by {:?}" , rustc) ;
129
- let stdout = utf8_stdout ( rustc) ?;
130
- AbsPathBuf :: assert ( PathBuf :: from ( stdout) )
131
- } ;
132
-
133
139
get_rust_src ( & sysroot_path)
134
140
. or_else ( || {
135
141
let mut rustup = Command :: new ( toolchain:: rustup ( ) ) ;
@@ -149,6 +155,16 @@ try installing the Rust source the same way you installed rustc",
149
155
} )
150
156
}
151
157
158
+ fn get_rustc_src ( sysroot_path : & AbsPath ) -> Option < AbsPathBuf > {
159
+ let rustc_src = sysroot_path. join ( "lib/rustlib/rustc-src/rust/compiler/rustc/Cargo.toml" ) ;
160
+ log:: debug!( "Checking for rustc source code: {}" , rustc_src. display( ) ) ;
161
+ if rustc_src. exists ( ) {
162
+ Some ( rustc_src)
163
+ } else {
164
+ None
165
+ }
166
+ }
167
+
152
168
fn get_rust_src ( sysroot_path : & AbsPath ) -> Option < AbsPathBuf > {
153
169
// Try the new path first since the old one still exists.
154
170
let rust_src = sysroot_path. join ( "lib/rustlib/src/rust" ) ;
0 commit comments