aboutsummaryrefslogtreecommitdiff
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
parent326556b09078a398e641dd221ab870ee0fe47f68 (diff)
Install rust-src when it is not found
-rw-r--r--crates/ra_project_model/src/sysroot.rs39
-rw-r--r--docs/user/readme.adoc4
2 files changed, 34 insertions, 9 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
diff --git a/docs/user/readme.adoc b/docs/user/readme.adoc
index 57a8cbe31..cb171defd 100644
--- a/docs/user/readme.adoc
+++ b/docs/user/readme.adoc
@@ -20,7 +20,9 @@ In theory, one should be able to just install the server binary and have it auto
20We are not there yet, so some editor specific setup is required. 20We are not there yet, so some editor specific setup is required.
21 21
22Additionally, rust-analyzer needs sources of the standard library. 22Additionally, rust-analyzer needs sources of the standard library.
23This commands adds them: 23When fails to locate them, rust-analyzer attempts to install them automatically.
24
25To add the sources manually, run the following command:
24 26
25```bash 27```bash
26$ rustup component add rust-src 28$ rustup component add rust-src