mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Install rust-src when it is not found
This commit is contained in:
parent
326556b090
commit
5cea8a37b7
2 changed files with 34 additions and 9 deletions
|
@ -47,16 +47,19 @@ impl Sysroot {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn discover(cargo_toml: &Path) -> Result<Sysroot> {
|
pub fn discover(cargo_toml: &Path) -> Result<Sysroot> {
|
||||||
let src = try_find_src_path(cargo_toml)?;
|
let mut src = try_find_src_path(cargo_toml)?;
|
||||||
|
|
||||||
if !src.exists() {
|
if !src.exists() {
|
||||||
Err(anyhow!(
|
src = try_install_rust_src(cargo_toml)?;
|
||||||
"can't load standard library from sysroot\n\
|
if !src.exists() {
|
||||||
{}\n\
|
Err(anyhow!(
|
||||||
(discovered via `rustc --print sysroot`)\n\
|
"can't load standard library from sysroot\n\
|
||||||
try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
|
{}\n\
|
||||||
src.display(),
|
(discovered via `rustc --print sysroot`)\n\
|
||||||
))?;
|
try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
|
||||||
|
src.display(),
|
||||||
|
))?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut sysroot = Sysroot { crates: Arena::default() };
|
let mut sysroot = Sysroot { crates: Arena::default() };
|
||||||
|
@ -113,6 +116,26 @@ fn try_find_src_path(cargo_toml: &Path) -> Result<PathBuf> {
|
||||||
Ok(sysroot_path.join("lib/rustlib/src/rust/src"))
|
Ok(sysroot_path.join("lib/rustlib/src/rust/src"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn try_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
|
||||||
|
let rustup_output = Command::new("rustup")
|
||||||
|
.current_dir(cargo_toml.parent().unwrap())
|
||||||
|
.args(&["component", "add", "rust-src"])
|
||||||
|
.output()
|
||||||
|
.context("rustup component add rust-src failed")?;
|
||||||
|
if !rustup_output.status.success() {
|
||||||
|
match rustup_output.status.code() {
|
||||||
|
Some(code) => bail!(
|
||||||
|
"failed to install rust-src: rustup component add rust-src exited with code {}",
|
||||||
|
code
|
||||||
|
),
|
||||||
|
None => bail!(
|
||||||
|
"failed to install rust-src: rustup component add rust-src terminated by signal"
|
||||||
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
try_find_src_path(cargo_toml)
|
||||||
|
}
|
||||||
|
|
||||||
impl SysrootCrate {
|
impl SysrootCrate {
|
||||||
pub fn name(self, sysroot: &Sysroot) -> &str {
|
pub fn name(self, sysroot: &Sysroot) -> &str {
|
||||||
&sysroot.crates[self].name
|
&sysroot.crates[self].name
|
||||||
|
|
|
@ -20,7 +20,9 @@ In theory, one should be able to just install the server binary and have it auto
|
||||||
We are not there yet, so some editor specific setup is required.
|
We are not there yet, so some editor specific setup is required.
|
||||||
|
|
||||||
Additionally, rust-analyzer needs sources of the standard library.
|
Additionally, rust-analyzer needs sources of the standard library.
|
||||||
This commands adds them:
|
When fails to locate them, rust-analyzer attempts to install them automatically.
|
||||||
|
|
||||||
|
To add the sources manually, run the following command:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ rustup component add rust-src
|
$ rustup component add rust-src
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue