aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-02-17 21:33:48 +0000
committerKirill Bulatov <[email protected]>2020-02-17 21:40:34 +0000
commit5cea8a37b75d84bbc95cb66487cc768181d440d5 (patch)
treedadf1de543d96c75d6e3a30cc6dfd0478ad818fa /crates/ra_project_model
parent326556b09078a398e641dd221ab870ee0fe47f68 (diff)
Install rust-src when it is not found
Diffstat (limited to 'crates/ra_project_model')
-rw-r--r--crates/ra_project_model/src/sysroot.rs39
1 files changed, 31 insertions, 8 deletions
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs
index 7b9cc899c..28756c7ca 100644
--- a/crates/ra_project_model/src/sysroot.rs
+++ b/crates/ra_project_model/src/sysroot.rs
@@ -47,16 +47,19 @@ impl Sysroot {
47 } 47 }
48 48
49 pub fn discover(cargo_toml: &Path) -> Result<Sysroot> { 49 pub fn discover(cargo_toml: &Path) -> Result<Sysroot> {
50 let src = try_find_src_path(cargo_toml)?; 50 let mut src = try_find_src_path(cargo_toml)?;
51 51
52 if !src.exists() { 52 if !src.exists() {
53 Err(anyhow!( 53 src = try_install_rust_src(cargo_toml)?;
54 "can't load standard library from sysroot\n\ 54 if !src.exists() {
55 {}\n\ 55 Err(anyhow!(
56 (discovered via `rustc --print sysroot`)\n\ 56 "can't load standard library from sysroot\n\
57 try running `rustup component add rust-src` or set `RUST_SRC_PATH`", 57 {}\n\
58 src.display(), 58 (discovered via `rustc --print sysroot`)\n\
59 ))?; 59 try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
60 src.display(),
61 ))?;
62 }
60 } 63 }
61 64
62 let mut sysroot = Sysroot { crates: Arena::default() }; 65 let mut sysroot = Sysroot { crates: Arena::default() };
@@ -113,6 +116,26 @@ fn try_find_src_path(cargo_toml: &Path) -> Result<PathBuf> {
113 Ok(sysroot_path.join("lib/rustlib/src/rust/src")) 116 Ok(sysroot_path.join("lib/rustlib/src/rust/src"))
114} 117}
115 118
119fn try_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
120 let rustup_output = Command::new("rustup")
121 .current_dir(cargo_toml.parent().unwrap())
122 .args(&["component", "add", "rust-src"])
123 .output()
124 .context("rustup component add rust-src failed")?;
125 if !rustup_output.status.success() {
126 match rustup_output.status.code() {
127 Some(code) => bail!(
128 "failed to install rust-src: rustup component add rust-src exited with code {}",
129 code
130 ),
131 None => bail!(
132 "failed to install rust-src: rustup component add rust-src terminated by signal"
133 ),
134 };
135 }
136 try_find_src_path(cargo_toml)
137}
138
116impl SysrootCrate { 139impl SysrootCrate {
117 pub fn name(self, sysroot: &Sysroot) -> &str { 140 pub fn name(self, sysroot: &Sysroot) -> &str {
118 &sysroot.crates[self].name 141 &sysroot.crates[self].name