aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_project_model/src/sysroot.rs72
-rw-r--r--docs/user/readme.adoc4
-rw-r--r--editors/code/package-lock.json14
-rw-r--r--editors/code/package.json2
4 files changed, 55 insertions, 37 deletions
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs
index 7b9cc899c..db779a2d2 100644
--- a/crates/ra_project_model/src/sysroot.rs
+++ b/crates/ra_project_model/src/sysroot.rs
@@ -1,10 +1,10 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use anyhow::{anyhow, bail, Context, Result}; 3use anyhow::{bail, Context, Result};
4use std::{ 4use std::{
5 env, 5 env,
6 path::{Path, PathBuf}, 6 path::{Path, PathBuf},
7 process::Command, 7 process::{Command, Output},
8}; 8};
9 9
10use ra_arena::{impl_arena_id, Arena, RawId}; 10use ra_arena::{impl_arena_id, Arena, RawId};
@@ -47,18 +47,7 @@ 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 src = get_or_install_rust_src(cargo_toml)?;
51
52 if !src.exists() {
53 Err(anyhow!(
54 "can't load standard library from sysroot\n\
55 {}\n\
56 (discovered via `rustc --print sysroot`)\n\
57 try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
58 src.display(),
59 ))?;
60 }
61
62 let mut sysroot = Sysroot { crates: Arena::default() }; 51 let mut sysroot = Sysroot { crates: Arena::default() };
63 for name in SYSROOT_CRATES.trim().lines() { 52 for name in SYSROOT_CRATES.trim().lines() {
64 let root = src.join(format!("lib{}", name)).join("lib.rs"); 53 let root = src.join(format!("lib{}", name)).join("lib.rs");
@@ -90,27 +79,54 @@ impl Sysroot {
90 } 79 }
91} 80}
92 81
93fn try_find_src_path(cargo_toml: &Path) -> Result<PathBuf> { 82fn create_command_text(program: &str, args: &[&str]) -> String {
94 if let Ok(path) = env::var("RUST_SRC_PATH") { 83 format!("{} {}", program, args.join(" "))
95 return Ok(path.into()); 84}
96 }
97 85
98 let rustc_output = Command::new("rustc") 86fn run_command_in_cargo_dir(cargo_toml: &Path, program: &str, args: &[&str]) -> Result<Output> {
87 let output = Command::new(program)
99 .current_dir(cargo_toml.parent().unwrap()) 88 .current_dir(cargo_toml.parent().unwrap())
100 .args(&["--print", "sysroot"]) 89 .args(args)
101 .output() 90 .output()
102 .context("rustc --print sysroot failed")?; 91 .context(format!("{} failed", create_command_text(program, args)))?;
103 if !rustc_output.status.success() { 92 if !output.status.success() {
104 match rustc_output.status.code() { 93 match output.status.code() {
105 Some(code) => { 94 Some(code) => bail!(
106 bail!("failed to locate sysroot: rustc --print sysroot exited with code {}", code) 95 "failed to run the command: '{}' exited with code {}",
107 } 96 create_command_text(program, args),
108 None => bail!("failed to locate sysroot: rustc --print sysroot terminated by signal"), 97 code
98 ),
99 None => bail!(
100 "failed to run the command: '{}' terminated by signal",
101 create_command_text(program, args)
102 ),
109 }; 103 };
110 } 104 }
105 Ok(output)
106}
107
108fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
109 if let Ok(path) = env::var("RUST_SRC_PATH") {
110 return Ok(path.into());
111 }
112 let rustc_output = run_command_in_cargo_dir(cargo_toml, "rustc", &["--print", "sysroot"])?;
111 let stdout = String::from_utf8(rustc_output.stdout)?; 113 let stdout = String::from_utf8(rustc_output.stdout)?;
112 let sysroot_path = Path::new(stdout.trim()); 114 let sysroot_path = Path::new(stdout.trim());
113 Ok(sysroot_path.join("lib/rustlib/src/rust/src")) 115 let src_path = sysroot_path.join("lib/rustlib/src/rust/src");
116
117 if !src_path.exists() {
118 run_command_in_cargo_dir(cargo_toml, "rustup", &["component", "add", "rust-src"])?;
119 }
120 if !src_path.exists() {
121 bail!(
122 "can't load standard library from sysroot\n\
123 {}\n\
124 (discovered via `rustc --print sysroot`)\n\
125 try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
126 src_path.display(),
127 )
128 }
129 Ok(src_path)
114} 130}
115 131
116impl SysrootCrate { 132impl SysrootCrate {
diff --git a/docs/user/readme.adoc b/docs/user/readme.adoc
index 57a8cbe31..8780a2995 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: 23If the source code is not present, rust-analyzer will attempt to install it 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
diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json
index 3f6bbdbdd..d7da47089 100644
--- a/editors/code/package-lock.json
+++ b/editors/code/package-lock.json
@@ -1671,18 +1671,18 @@
1671 "integrity": "sha512-JvONPptw3GAQGXlVV2utDcHx0BiY34FupW/kI6mZ5x06ER5DdPG/tXWMVHjTNULF5uKPOUUD0SaXg5QaubJL0A==" 1671 "integrity": "sha512-JvONPptw3GAQGXlVV2utDcHx0BiY34FupW/kI6mZ5x06ER5DdPG/tXWMVHjTNULF5uKPOUUD0SaXg5QaubJL0A=="
1672 }, 1672 },
1673 "vscode-languageclient": { 1673 "vscode-languageclient": {
1674 "version": "6.1.0", 1674 "version": "6.1.1",
1675 "resolved": "https://registry.npmjs.org/vscode-languageclient/-/vscode-languageclient-6.1.0.tgz", 1675 "resolved": "https://registry.npmjs.org/vscode-languageclient/-/vscode-languageclient-6.1.1.tgz",
1676 "integrity": "sha512-Tcp0VoOaa0YzxL4nEfK9tsmcy76Eo8jNLvFQZwh2c8oMm02luL8uGYPLQNAiZ3XGgegfcwiQFZMqbW7DNV0vxA==", 1676 "integrity": "sha512-mB6d8Tg+82l8EFUfR+SBu0+lCshyKVgC5E5+MQ0/BJa+9AgeBjtG5npoGaCo4/VvWzK0ZRGm85zU5iRp1RYPIA==",
1677 "requires": { 1677 "requires": {
1678 "semver": "^6.3.0", 1678 "semver": "^6.3.0",
1679 "vscode-languageserver-protocol": "^3.15.2" 1679 "vscode-languageserver-protocol": "^3.15.3"
1680 } 1680 }
1681 }, 1681 },
1682 "vscode-languageserver-protocol": { 1682 "vscode-languageserver-protocol": {
1683 "version": "3.15.2", 1683 "version": "3.15.3",
1684 "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.15.2.tgz", 1684 "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.15.3.tgz",
1685 "integrity": "sha512-GdL05JKOgZ76RDg3suiGCl9enESM7iQgGw4x93ibTh4sldvZmakHmTeZ4iUApPPGKf6O3OVBtrsksBXnHYaxNg==", 1685 "integrity": "sha512-zrMuwHOAQRhjDSnflWdJG+O2ztMWss8GqUUB8dXLR/FPenwkiBNkMIJJYfSN6sgskvsF0rHAoBowNQfbyZnnvw==",
1686 "requires": { 1686 "requires": {
1687 "vscode-jsonrpc": "^5.0.1", 1687 "vscode-jsonrpc": "^5.0.1",
1688 "vscode-languageserver-types": "3.15.1" 1688 "vscode-languageserver-types": "3.15.1"
diff --git a/editors/code/package.json b/editors/code/package.json
index f2be5fb3b..002293b2e 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -28,7 +28,7 @@
28 "jsonc-parser": "^2.1.0", 28 "jsonc-parser": "^2.1.0",
29 "node-fetch": "^2.6.0", 29 "node-fetch": "^2.6.0",
30 "throttle-debounce": "^2.1.0", 30 "throttle-debounce": "^2.1.0",
31 "vscode-languageclient": "^6.1.0" 31 "vscode-languageclient": "^6.1.1"
32 }, 32 },
33 "devDependencies": { 33 "devDependencies": {
34 "@rollup/plugin-commonjs": "^11.0.2", 34 "@rollup/plugin-commonjs": "^11.0.2",