aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src/sysroot.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_project_model/src/sysroot.rs')
-rw-r--r--crates/ra_project_model/src/sysroot.rs41
1 files changed, 11 insertions, 30 deletions
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs
index 55ff5ad80..a8a196e64 100644
--- a/crates/ra_project_model/src/sysroot.rs
+++ b/crates/ra_project_model/src/sysroot.rs
@@ -1,14 +1,16 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use anyhow::{bail, Context, Result};
4use std::{ 3use std::{
5 env, ops, 4 env, ops,
6 path::{Path, PathBuf}, 5 path::{Path, PathBuf},
7 process::{Command, Output}, 6 process::Command,
8}; 7};
9 8
9use anyhow::{bail, Result};
10use ra_arena::{Arena, Idx}; 10use ra_arena::{Arena, Idx};
11 11
12use crate::output;
13
12#[derive(Default, Debug, Clone)] 14#[derive(Default, Debug, Clone)]
13pub struct Sysroot { 15pub struct Sysroot {
14 crates: Arena<SysrootCrateData>, 16 crates: Arena<SysrootCrateData>,
@@ -84,43 +86,22 @@ impl Sysroot {
84 } 86 }
85} 87}
86 88
87fn create_command_text(program: &str, args: &[&str]) -> String {
88 format!("{} {}", program, args.join(" "))
89}
90
91fn run_command_in_cargo_dir(cargo_toml: &Path, program: &str, args: &[&str]) -> Result<Output> {
92 let output = Command::new(program)
93 .current_dir(cargo_toml.parent().unwrap())
94 .args(args)
95 .output()
96 .context(format!("{} failed", create_command_text(program, args)))?;
97 if !output.status.success() {
98 match output.status.code() {
99 Some(code) => bail!(
100 "failed to run the command: '{}' exited with code {}",
101 create_command_text(program, args),
102 code
103 ),
104 None => bail!(
105 "failed to run the command: '{}' terminated by signal",
106 create_command_text(program, args)
107 ),
108 };
109 }
110 Ok(output)
111}
112
113fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> { 89fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
114 if let Ok(path) = env::var("RUST_SRC_PATH") { 90 if let Ok(path) = env::var("RUST_SRC_PATH") {
115 return Ok(path.into()); 91 return Ok(path.into());
116 } 92 }
117 let rustc_output = run_command_in_cargo_dir(cargo_toml, "rustc", &["--print", "sysroot"])?; 93 let current_dir = cargo_toml.parent().unwrap();
94 let mut rustc = Command::new(ra_toolchain::rustc());
95 rustc.current_dir(current_dir).args(&["--print", "sysroot"]);
96 let rustc_output = output(rustc)?;
118 let stdout = String::from_utf8(rustc_output.stdout)?; 97 let stdout = String::from_utf8(rustc_output.stdout)?;
119 let sysroot_path = Path::new(stdout.trim()); 98 let sysroot_path = Path::new(stdout.trim());
120 let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); 99 let src_path = sysroot_path.join("lib/rustlib/src/rust/src");
121 100
122 if !src_path.exists() { 101 if !src_path.exists() {
123 run_command_in_cargo_dir(cargo_toml, "rustup", &["component", "add", "rust-src"])?; 102 let mut rustup = Command::new(ra_toolchain::rustup());
103 rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]);
104 let _output = output(rustup)?;
124 } 105 }
125 if !src_path.exists() { 106 if !src_path.exists() {
126 bail!( 107 bail!(