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.rs49
1 files changed, 11 insertions, 38 deletions
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs
index 11c26ad89..a8a196e64 100644
--- a/crates/ra_project_model/src/sysroot.rs
+++ b/crates/ra_project_model/src/sysroot.rs
@@ -1,14 +1,15 @@
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};
11use ra_env::get_path_for_executable; 11
12use crate::output;
12 13
13#[derive(Default, Debug, Clone)] 14#[derive(Default, Debug, Clone)]
14pub struct Sysroot { 15pub struct Sysroot {
@@ -85,50 +86,22 @@ impl Sysroot {
85 } 86 }
86} 87}
87 88
88fn create_command_text(program: &str, args: &[&str]) -> String {
89 format!("{} {}", program, args.join(" "))
90}
91
92fn run_command_in_cargo_dir(
93 cargo_toml: impl AsRef<Path>,
94 program: impl AsRef<Path>,
95 args: &[&str],
96) -> Result<Output> {
97 let program = program.as_ref().as_os_str().to_str().expect("Invalid Unicode in path");
98 let output = Command::new(program)
99 .current_dir(cargo_toml.as_ref().parent().unwrap())
100 .args(args)
101 .output()
102 .context(format!("{} failed", create_command_text(program, args)))?;
103 if !output.status.success() {
104 match output.status.code() {
105 Some(code) => bail!(
106 "failed to run the command: '{}' exited with code {}",
107 create_command_text(program, args),
108 code
109 ),
110 None => bail!(
111 "failed to run the command: '{}' terminated by signal",
112 create_command_text(program, args)
113 ),
114 };
115 }
116 Ok(output)
117}
118
119fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> { 89fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
120 if let Ok(path) = env::var("RUST_SRC_PATH") { 90 if let Ok(path) = env::var("RUST_SRC_PATH") {
121 return Ok(path.into()); 91 return Ok(path.into());
122 } 92 }
123 let rustc = get_path_for_executable("rustc")?; 93 let current_dir = cargo_toml.parent().unwrap();
124 let rustc_output = run_command_in_cargo_dir(cargo_toml, &rustc, &["--print", "sysroot"])?; 94 let mut rustc = Command::new(ra_toolchain::rustc());
95 rustc.current_dir(current_dir).args(&["--print", "sysroot"]);
96 let rustc_output = output(rustc)?;
125 let stdout = String::from_utf8(rustc_output.stdout)?; 97 let stdout = String::from_utf8(rustc_output.stdout)?;
126 let sysroot_path = Path::new(stdout.trim()); 98 let sysroot_path = Path::new(stdout.trim());
127 let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); 99 let src_path = sysroot_path.join("lib/rustlib/src/rust/src");
128 100
129 if !src_path.exists() { 101 if !src_path.exists() {
130 let rustup = get_path_for_executable("rustup")?; 102 let mut rustup = Command::new(ra_toolchain::rustup());
131 run_command_in_cargo_dir(cargo_toml, &rustup, &["component", "add", "rust-src"])?; 103 rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]);
104 let _output = output(rustup)?;
132 } 105 }
133 if !src_path.exists() { 106 if !src_path.exists() {
134 bail!( 107 bail!(