From 5cea8a37b75d84bbc95cb66487cc768181d440d5 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 17 Feb 2020 23:33:48 +0200 Subject: Install rust-src when it is not found --- crates/ra_project_model/src/sysroot.rs | 39 +++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'crates/ra_project_model/src') 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 { } pub fn discover(cargo_toml: &Path) -> Result { - let src = try_find_src_path(cargo_toml)?; + let mut src = try_find_src_path(cargo_toml)?; if !src.exists() { - Err(anyhow!( - "can't load standard library from sysroot\n\ - {}\n\ - (discovered via `rustc --print sysroot`)\n\ - try running `rustup component add rust-src` or set `RUST_SRC_PATH`", - src.display(), - ))?; + src = try_install_rust_src(cargo_toml)?; + if !src.exists() { + Err(anyhow!( + "can't load standard library from sysroot\n\ + {}\n\ + (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() }; @@ -113,6 +116,26 @@ fn try_find_src_path(cargo_toml: &Path) -> Result { Ok(sysroot_path.join("lib/rustlib/src/rust/src")) } +fn try_install_rust_src(cargo_toml: &Path) -> Result { + 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 { pub fn name(self, sysroot: &Sysroot) -> &str { &sysroot.crates[self].name -- cgit v1.2.3 From addb61df36bff1e54e89c103922774e6f9dd4f21 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 18 Feb 2020 00:03:57 +0200 Subject: Idempotent location and installation of rust src --- crates/ra_project_model/src/sysroot.rs | 103 ++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 47 deletions(-) (limited to 'crates/ra_project_model/src') diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index 28756c7ca..43b062023 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs @@ -47,19 +47,16 @@ impl Sysroot { } pub fn discover(cargo_toml: &Path) -> Result { - let mut src = try_find_src_path(cargo_toml)?; + let src = get_or_install_rust_src(cargo_toml)?; if !src.exists() { - src = try_install_rust_src(cargo_toml)?; - if !src.exists() { - Err(anyhow!( - "can't load standard library from sysroot\n\ - {}\n\ - (discovered via `rustc --print sysroot`)\n\ - try running `rustup component add rust-src` or set `RUST_SRC_PATH`", - src.display(), - ))?; - } + Err(anyhow!( + "can't load standard library from sysroot\n\ + {}\n\ + (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() }; @@ -93,47 +90,59 @@ impl Sysroot { } } -fn try_find_src_path(cargo_toml: &Path) -> Result { - if let Ok(path) = env::var("RUST_SRC_PATH") { - return Ok(path.into()); +fn get_or_install_rust_src(cargo_toml: &Path) -> Result { + fn try_find_src_path(cargo_toml: &Path) -> Result { + if let Ok(path) = env::var("RUST_SRC_PATH") { + return Ok(path.into()); + } + + let rustc_output = Command::new("rustc") + .current_dir(cargo_toml.parent().unwrap()) + .args(&["--print", "sysroot"]) + .output() + .context("rustc --print sysroot failed")?; + if !rustc_output.status.success() { + match rustc_output.status.code() { + Some(code) => bail!( + "failed to locate sysroot: rustc --print sysroot exited with code {}", + code + ), + None => { + bail!("failed to locate sysroot: rustc --print sysroot terminated by signal") + } + }; + } + let stdout = String::from_utf8(rustc_output.stdout)?; + let sysroot_path = Path::new(stdout.trim()); + Ok(sysroot_path.join("lib/rustlib/src/rust/src")) } - let rustc_output = Command::new("rustc") - .current_dir(cargo_toml.parent().unwrap()) - .args(&["--print", "sysroot"]) - .output() - .context("rustc --print sysroot failed")?; - if !rustc_output.status.success() { - match rustc_output.status.code() { - Some(code) => { - bail!("failed to locate sysroot: rustc --print sysroot exited with code {}", code) - } - None => bail!("failed to locate sysroot: rustc --print sysroot terminated by signal"), - }; + fn try_install_rust_src(cargo_toml: &Path) -> Result { + 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) } - let stdout = String::from_utf8(rustc_output.stdout)?; - let sysroot_path = Path::new(stdout.trim()); - Ok(sysroot_path.join("lib/rustlib/src/rust/src")) -} -fn try_install_rust_src(cargo_toml: &Path) -> Result { - 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" - ), - }; + let src = try_find_src_path(cargo_toml)?; + if !src.exists() { + try_install_rust_src(cargo_toml) + } else { + Ok(src) } - try_find_src_path(cargo_toml) } impl SysrootCrate { -- cgit v1.2.3 From 67d30312d0741d3bcc45d794dc8c333f1a7d9ed8 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 18 Feb 2020 00:18:26 +0200 Subject: Simplify the code further --- crates/ra_project_model/src/sysroot.rs | 80 +++++++++++++++------------------- 1 file changed, 35 insertions(+), 45 deletions(-) (limited to 'crates/ra_project_model/src') diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index 43b062023..b426fc107 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use anyhow::{anyhow, bail, Context, Result}; +use anyhow::{bail, Context, Result}; use std::{ env, path::{Path, PathBuf}, @@ -48,17 +48,6 @@ impl Sysroot { pub fn discover(cargo_toml: &Path) -> Result { let src = get_or_install_rust_src(cargo_toml)?; - - if !src.exists() { - Err(anyhow!( - "can't load standard library from sysroot\n\ - {}\n\ - (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() }; for name in SYSROOT_CRATES.trim().lines() { let root = src.join(format!("lib{}", name)).join("lib.rs"); @@ -91,33 +80,7 @@ impl Sysroot { } fn get_or_install_rust_src(cargo_toml: &Path) -> Result { - fn try_find_src_path(cargo_toml: &Path) -> Result { - if let Ok(path) = env::var("RUST_SRC_PATH") { - return Ok(path.into()); - } - - let rustc_output = Command::new("rustc") - .current_dir(cargo_toml.parent().unwrap()) - .args(&["--print", "sysroot"]) - .output() - .context("rustc --print sysroot failed")?; - if !rustc_output.status.success() { - match rustc_output.status.code() { - Some(code) => bail!( - "failed to locate sysroot: rustc --print sysroot exited with code {}", - code - ), - None => { - bail!("failed to locate sysroot: rustc --print sysroot terminated by signal") - } - }; - } - let stdout = String::from_utf8(rustc_output.stdout)?; - let sysroot_path = Path::new(stdout.trim()); - Ok(sysroot_path.join("lib/rustlib/src/rust/src")) - } - - fn try_install_rust_src(cargo_toml: &Path) -> Result { + fn try_install_rust_src(cargo_toml: &Path) -> Result<()> { let rustup_output = Command::new("rustup") .current_dir(cargo_toml.parent().unwrap()) .args(&["component", "add", "rust-src"]) @@ -134,15 +97,42 @@ fn get_or_install_rust_src(cargo_toml: &Path) -> Result { ), }; } - try_find_src_path(cargo_toml) + Ok(()) } - let src = try_find_src_path(cargo_toml)?; - if !src.exists() { - try_install_rust_src(cargo_toml) - } else { - Ok(src) + if let Ok(path) = env::var("RUST_SRC_PATH") { + return Ok(path.into()); + } + let rustc_output = Command::new("rustc") + .current_dir(cargo_toml.parent().unwrap()) + .args(&["--print", "sysroot"]) + .output() + .context("rustc --print sysroot failed")?; + if !rustc_output.status.success() { + match rustc_output.status.code() { + Some(code) => { + bail!("failed to locate sysroot: rustc --print sysroot exited with code {}", code) + } + None => bail!("failed to locate sysroot: rustc --print sysroot terminated by signal"), + }; + } + let stdout = String::from_utf8(rustc_output.stdout)?; + let sysroot_path = Path::new(stdout.trim()); + let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); + + if !src_path.exists() { + try_install_rust_src(cargo_toml)? + } + if !src_path.exists() { + bail!( + "can't load standard library from sysroot\n\ + {}\n\ + (discovered via `rustc --print sysroot`)\n\ + try running `rustup component add rust-src` or set `RUST_SRC_PATH`", + src_path.display(), + ) } + Ok(src_path) } impl SysrootCrate { -- cgit v1.2.3 From e29dbdb139fb3a35902b4b3d11a92d45861d210b Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 18 Feb 2020 00:38:01 +0200 Subject: Simplify the command execution --- crates/ra_project_model/src/sysroot.rs | 62 +++++++++++++++------------------- 1 file changed, 28 insertions(+), 34 deletions(-) (limited to 'crates/ra_project_model/src') diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index b426fc107..db779a2d2 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs @@ -4,7 +4,7 @@ use anyhow::{bail, Context, Result}; use std::{ env, path::{Path, PathBuf}, - process::Command, + process::{Command, Output}, }; use ra_arena::{impl_arena_id, Arena, RawId}; @@ -79,49 +79,43 @@ impl Sysroot { } } -fn get_or_install_rust_src(cargo_toml: &Path) -> Result { - fn try_install_rust_src(cargo_toml: &Path) -> Result<()> { - 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" - ), - }; - } - Ok(()) - } +fn create_command_text(program: &str, args: &[&str]) -> String { + format!("{} {}", program, args.join(" ")) +} - if let Ok(path) = env::var("RUST_SRC_PATH") { - return Ok(path.into()); - } - let rustc_output = Command::new("rustc") +fn run_command_in_cargo_dir(cargo_toml: &Path, program: &str, args: &[&str]) -> Result { + let output = Command::new(program) .current_dir(cargo_toml.parent().unwrap()) - .args(&["--print", "sysroot"]) + .args(args) .output() - .context("rustc --print sysroot failed")?; - if !rustc_output.status.success() { - match rustc_output.status.code() { - Some(code) => { - bail!("failed to locate sysroot: rustc --print sysroot exited with code {}", code) - } - None => bail!("failed to locate sysroot: rustc --print sysroot terminated by signal"), + .context(format!("{} failed", create_command_text(program, args)))?; + if !output.status.success() { + match output.status.code() { + Some(code) => bail!( + "failed to run the command: '{}' exited with code {}", + create_command_text(program, args), + code + ), + None => bail!( + "failed to run the command: '{}' terminated by signal", + create_command_text(program, args) + ), }; } + Ok(output) +} + +fn get_or_install_rust_src(cargo_toml: &Path) -> Result { + if let Ok(path) = env::var("RUST_SRC_PATH") { + return Ok(path.into()); + } + let rustc_output = run_command_in_cargo_dir(cargo_toml, "rustc", &["--print", "sysroot"])?; let stdout = String::from_utf8(rustc_output.stdout)?; let sysroot_path = Path::new(stdout.trim()); let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); if !src_path.exists() { - try_install_rust_src(cargo_toml)? + run_command_in_cargo_dir(cargo_toml, "rustup", &["component", "add", "rust-src"])?; } if !src_path.exists() { bail!( -- cgit v1.2.3