Skip to content

Commit 18c7a1e

Browse files
committed
Make sysroot use RUST_SRC_PATH if set
1 parent 08e5d39 commit 18c7a1e

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

crates/ra_project_model/src/sysroot.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
env,
23
path::{Path, PathBuf},
34
process::Command,
45
};
@@ -33,21 +34,13 @@ impl Sysroot {
3334
}
3435

3536
pub fn discover(cargo_toml: &Path) -> Result<Sysroot> {
36-
let rustc_output = Command::new("rustc")
37-
.current_dir(cargo_toml.parent().unwrap())
38-
.args(&["--print", "sysroot"])
39-
.output()?;
40-
if !rustc_output.status.success() {
41-
Err("failed to locate sysroot")?
42-
}
43-
let stdout = String::from_utf8(rustc_output.stdout)?;
44-
let sysroot_path = Path::new(stdout.trim());
45-
let src = sysroot_path.join("lib/rustlib/src/rust/src");
37+
let src = try_find_src_path(cargo_toml)?;
38+
4639
if !src.exists() {
4740
Err(format!(
4841
"can't load standard library from sysroot\n\
4942
{:?}\n\
50-
try running `rustup component add rust-src`",
43+
try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
5144
src,
5245
))?;
5346
}
@@ -83,6 +76,23 @@ impl Sysroot {
8376
}
8477
}
8578

79+
fn try_find_src_path(cargo_toml: &Path) -> Result<PathBuf> {
80+
if let Ok(path) = env::var("RUST_SRC_PATH") {
81+
return Ok(path.into());
82+
}
83+
84+
let rustc_output = Command::new("rustc")
85+
.current_dir(cargo_toml.parent().unwrap())
86+
.args(&["--print", "sysroot"])
87+
.output()?;
88+
if !rustc_output.status.success() {
89+
Err("failed to locate sysroot")?;
90+
}
91+
let stdout = String::from_utf8(rustc_output.stdout)?;
92+
let sysroot_path = Path::new(stdout.trim());
93+
Ok(sysroot_path.join("lib/rustlib/src/rust/src"))
94+
}
95+
8696
impl SysrootCrate {
8797
pub fn name(self, sysroot: &Sysroot) -> &str {
8898
&sysroot.crates[self].name

docs/user/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ See https://github.com/microsoft/vscode/issues/72308[microsoft/vscode#72308] for
7878
(e.g: `--features="shumway,pdf"` will run as `cargo watch -x "check --features="shumway,pdf""` )
7979
* `rust-analyzer.trace.server`: enables internal logging
8080
* `rust-analyzer.trace.cargo-watch`: enables cargo-watch logging
81+
* `RUST_SRC_PATH`: environment variable that overwrites the sysroot
8182

8283

8384
## Emacs

0 commit comments

Comments
 (0)