From 18c7a1ebe7ced9d8039132da06dcd954affec2eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Thu, 22 Aug 2019 21:08:34 +0200 Subject: Make sysroot use `RUST_SRC_PATH` if set --- crates/ra_project_model/src/sysroot.rs | 32 +++++++++++++++++++++----------- docs/user/README.md | 1 + 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index 2a7927f7e..0c27d4f4b 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs @@ -1,4 +1,5 @@ use std::{ + env, path::{Path, PathBuf}, process::Command, }; @@ -33,21 +34,13 @@ impl Sysroot { } pub fn discover(cargo_toml: &Path) -> Result { - let rustc_output = Command::new("rustc") - .current_dir(cargo_toml.parent().unwrap()) - .args(&["--print", "sysroot"]) - .output()?; - if !rustc_output.status.success() { - Err("failed to locate sysroot")? - } - let stdout = String::from_utf8(rustc_output.stdout)?; - let sysroot_path = Path::new(stdout.trim()); - let src = sysroot_path.join("lib/rustlib/src/rust/src"); + let src = try_find_src_path(cargo_toml)?; + if !src.exists() { Err(format!( "can't load standard library from sysroot\n\ {:?}\n\ - try running `rustup component add rust-src`", + try running `rustup component add rust-src` or set `RUST_SRC_PATH`", src, ))?; } @@ -83,6 +76,23 @@ impl Sysroot { } } +fn try_find_src_path(cargo_toml: &Path) -> Result { + if let Ok(path) = env::var("RUST_SRC_PATH") { + return Ok(path.into()); + } + + let rustc_output = Command::new("rustc") + .current_dir(cargo_toml.parent().unwrap()) + .args(&["--print", "sysroot"]) + .output()?; + if !rustc_output.status.success() { + Err("failed to locate sysroot")?; + } + let stdout = String::from_utf8(rustc_output.stdout)?; + let sysroot_path = Path::new(stdout.trim()); + Ok(sysroot_path.join("lib/rustlib/src/rust/src")) +} + impl SysrootCrate { pub fn name(self, sysroot: &Sysroot) -> &str { &sysroot.crates[self].name diff --git a/docs/user/README.md b/docs/user/README.md index 7990d1d31..453e8e273 100644 --- a/docs/user/README.md +++ b/docs/user/README.md @@ -78,6 +78,7 @@ See https://github.com/microsoft/vscode/issues/72308[microsoft/vscode#72308] for (e.g: `--features="shumway,pdf"` will run as `cargo watch -x "check --features="shumway,pdf""` ) * `rust-analyzer.trace.server`: enables internal logging * `rust-analyzer.trace.cargo-watch`: enables cargo-watch logging +* `RUST_SRC_PATH`: environment variable that overwrites the sysroot ## Emacs -- cgit v1.2.3