|
1 | 1 | use std::{
|
| 2 | + env, |
2 | 3 | path::{Path, PathBuf},
|
3 | 4 | process::Command,
|
4 | 5 | };
|
@@ -33,21 +34,13 @@ impl Sysroot {
|
33 | 34 | }
|
34 | 35 |
|
35 | 36 | 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 | + |
46 | 39 | if !src.exists() {
|
47 | 40 | Err(format!(
|
48 | 41 | "can't load standard library from sysroot\n\
|
49 | 42 | {:?}\n\
|
50 |
| - try running `rustup component add rust-src`", |
| 43 | + try running `rustup component add rust-src` or set `RUST_SRC_PATH`", |
51 | 44 | src,
|
52 | 45 | ))?;
|
53 | 46 | }
|
@@ -83,6 +76,23 @@ impl Sysroot {
|
83 | 76 | }
|
84 | 77 | }
|
85 | 78 |
|
| 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 | + |
86 | 96 | impl SysrootCrate {
|
87 | 97 | pub fn name(self, sysroot: &Sysroot) -> &str {
|
88 | 98 | &sysroot.crates[self].name
|
|
0 commit comments