diff options
Diffstat (limited to 'crates/ra_project_model')
-rw-r--r-- | crates/ra_project_model/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_project_model/src/cargo_workspace.rs | 5 | ||||
-rw-r--r-- | crates/ra_project_model/src/lib.rs | 88 | ||||
-rw-r--r-- | crates/ra_project_model/src/sysroot.rs | 49 |
4 files changed, 50 insertions, 94 deletions
diff --git a/crates/ra_project_model/Cargo.toml b/crates/ra_project_model/Cargo.toml index 946deed5d..e4a60f4c0 100644 --- a/crates/ra_project_model/Cargo.toml +++ b/crates/ra_project_model/Cargo.toml | |||
@@ -16,7 +16,7 @@ cargo_metadata = "0.10.0" | |||
16 | ra_arena = { path = "../ra_arena" } | 16 | ra_arena = { path = "../ra_arena" } |
17 | ra_cfg = { path = "../ra_cfg" } | 17 | ra_cfg = { path = "../ra_cfg" } |
18 | ra_db = { path = "../ra_db" } | 18 | ra_db = { path = "../ra_db" } |
19 | ra_env = { path = "../ra_env" } | 19 | ra_toolchain = { path = "../ra_toolchain" } |
20 | ra_proc_macro = { path = "../ra_proc_macro" } | 20 | ra_proc_macro = { path = "../ra_proc_macro" } |
21 | 21 | ||
22 | serde = { version = "1.0.106", features = ["derive"] } | 22 | serde = { version = "1.0.106", features = ["derive"] } |
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 9152a6d9d..a306ce95f 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs | |||
@@ -11,7 +11,6 @@ use anyhow::{Context, Result}; | |||
11 | use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}; | 11 | use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}; |
12 | use ra_arena::{Arena, Idx}; | 12 | use ra_arena::{Arena, Idx}; |
13 | use ra_db::Edition; | 13 | use ra_db::Edition; |
14 | use ra_env::get_path_for_executable; | ||
15 | use rustc_hash::FxHashMap; | 14 | use rustc_hash::FxHashMap; |
16 | 15 | ||
17 | /// `CargoWorkspace` represents the logical structure of, well, a Cargo | 16 | /// `CargoWorkspace` represents the logical structure of, well, a Cargo |
@@ -147,7 +146,7 @@ impl CargoWorkspace { | |||
147 | cargo_features: &CargoConfig, | 146 | cargo_features: &CargoConfig, |
148 | ) -> Result<CargoWorkspace> { | 147 | ) -> Result<CargoWorkspace> { |
149 | let mut meta = MetadataCommand::new(); | 148 | let mut meta = MetadataCommand::new(); |
150 | meta.cargo_path(get_path_for_executable("cargo")?); | 149 | meta.cargo_path(ra_toolchain::cargo()); |
151 | meta.manifest_path(cargo_toml); | 150 | meta.manifest_path(cargo_toml); |
152 | if cargo_features.all_features { | 151 | if cargo_features.all_features { |
153 | meta.features(CargoOpt::AllFeatures); | 152 | meta.features(CargoOpt::AllFeatures); |
@@ -289,7 +288,7 @@ pub fn load_extern_resources( | |||
289 | cargo_toml: &Path, | 288 | cargo_toml: &Path, |
290 | cargo_features: &CargoConfig, | 289 | cargo_features: &CargoConfig, |
291 | ) -> Result<ExternResources> { | 290 | ) -> Result<ExternResources> { |
292 | let mut cmd = Command::new(get_path_for_executable("cargo")?); | 291 | let mut cmd = Command::new(ra_toolchain::cargo()); |
293 | cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml); | 292 | cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml); |
294 | if cargo_features.all_features { | 293 | if cargo_features.all_features { |
295 | cmd.arg("--all-features"); | 294 | cmd.arg("--all-features"); |
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 88a6ffb2a..4f098b706 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs | |||
@@ -8,13 +8,12 @@ use std::{ | |||
8 | fs::{read_dir, File, ReadDir}, | 8 | fs::{read_dir, File, ReadDir}, |
9 | io::{self, BufReader}, | 9 | io::{self, BufReader}, |
10 | path::{Path, PathBuf}, | 10 | path::{Path, PathBuf}, |
11 | process::Command, | 11 | process::{Command, Output}, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | use anyhow::{bail, Context, Result}; | 14 | use anyhow::{bail, Context, Result}; |
15 | use ra_cfg::CfgOptions; | 15 | use ra_cfg::CfgOptions; |
16 | use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId}; | 16 | use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId}; |
17 | use ra_env::get_path_for_executable; | ||
18 | use rustc_hash::FxHashMap; | 17 | use rustc_hash::FxHashMap; |
19 | use serde_json::from_reader; | 18 | use serde_json::from_reader; |
20 | 19 | ||
@@ -89,46 +88,28 @@ impl ProjectRoot { | |||
89 | } | 88 | } |
90 | 89 | ||
91 | pub fn discover(path: &Path) -> io::Result<Vec<ProjectRoot>> { | 90 | pub fn discover(path: &Path) -> io::Result<Vec<ProjectRoot>> { |
92 | if let Some(project_json) = find_rust_project_json(path) { | 91 | if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") { |
93 | return Ok(vec![ProjectRoot::ProjectJson(project_json)]); | 92 | return Ok(vec![ProjectRoot::ProjectJson(project_json)]); |
94 | } | 93 | } |
95 | return find_cargo_toml(path) | 94 | return find_cargo_toml(path) |
96 | .map(|paths| paths.into_iter().map(ProjectRoot::CargoToml).collect()); | 95 | .map(|paths| paths.into_iter().map(ProjectRoot::CargoToml).collect()); |
97 | 96 | ||
98 | fn find_rust_project_json(path: &Path) -> Option<PathBuf> { | ||
99 | if path.ends_with("rust-project.json") { | ||
100 | return Some(path.to_path_buf()); | ||
101 | } | ||
102 | |||
103 | let mut curr = Some(path); | ||
104 | while let Some(path) = curr { | ||
105 | let candidate = path.join("rust-project.json"); | ||
106 | if candidate.exists() { | ||
107 | return Some(candidate); | ||
108 | } | ||
109 | curr = path.parent(); | ||
110 | } | ||
111 | |||
112 | None | ||
113 | } | ||
114 | |||
115 | fn find_cargo_toml(path: &Path) -> io::Result<Vec<PathBuf>> { | 97 | fn find_cargo_toml(path: &Path) -> io::Result<Vec<PathBuf>> { |
116 | if path.ends_with("Cargo.toml") { | 98 | match find_in_parent_dirs(path, "Cargo.toml") { |
117 | return Ok(vec![path.to_path_buf()]); | 99 | Some(it) => Ok(vec![it]), |
100 | None => Ok(find_cargo_toml_in_child_dir(read_dir(path)?)), | ||
118 | } | 101 | } |
102 | } | ||
119 | 103 | ||
120 | if let Some(p) = find_cargo_toml_in_parent_dir(path) { | 104 | fn find_in_parent_dirs(path: &Path, target_file_name: &str) -> Option<PathBuf> { |
121 | return Ok(vec![p]); | 105 | if path.ends_with(target_file_name) { |
106 | return Some(path.to_owned()); | ||
122 | } | 107 | } |
123 | 108 | ||
124 | let entities = read_dir(path)?; | ||
125 | Ok(find_cargo_toml_in_child_dir(entities)) | ||
126 | } | ||
127 | |||
128 | fn find_cargo_toml_in_parent_dir(path: &Path) -> Option<PathBuf> { | ||
129 | let mut curr = Some(path); | 109 | let mut curr = Some(path); |
110 | |||
130 | while let Some(path) = curr { | 111 | while let Some(path) = curr { |
131 | let candidate = path.join("Cargo.toml"); | 112 | let candidate = path.join(target_file_name); |
132 | if candidate.exists() { | 113 | if candidate.exists() { |
133 | return Some(candidate); | 114 | return Some(candidate); |
134 | } | 115 | } |
@@ -140,14 +121,11 @@ impl ProjectRoot { | |||
140 | 121 | ||
141 | fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> { | 122 | fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> { |
142 | // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects | 123 | // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects |
143 | let mut valid_canditates = vec![]; | 124 | entities |
144 | for entity in entities.filter_map(Result::ok) { | 125 | .filter_map(Result::ok) |
145 | let candidate = entity.path().join("Cargo.toml"); | 126 | .map(|it| it.path().join("Cargo.toml")) |
146 | if candidate.exists() { | 127 | .filter(|it| it.exists()) |
147 | valid_canditates.push(candidate) | 128 | .collect() |
148 | } | ||
149 | } | ||
150 | valid_canditates | ||
151 | } | 129 | } |
152 | } | 130 | } |
153 | } | 131 | } |
@@ -568,25 +546,18 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { | |||
568 | } | 546 | } |
569 | } | 547 | } |
570 | 548 | ||
571 | match (|| -> Result<String> { | 549 | let rustc_cfgs = || -> Result<String> { |
572 | // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here. | 550 | // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here. |
573 | let mut cmd = Command::new(get_path_for_executable("rustc")?); | 551 | let mut cmd = Command::new(ra_toolchain::rustc()); |
574 | cmd.args(&["--print", "cfg", "-O"]); | 552 | cmd.args(&["--print", "cfg", "-O"]); |
575 | if let Some(target) = target { | 553 | if let Some(target) = target { |
576 | cmd.args(&["--target", target.as_str()]); | 554 | cmd.args(&["--target", target.as_str()]); |
577 | } | 555 | } |
578 | let output = cmd.output().context("Failed to get output from rustc --print cfg -O")?; | 556 | let output = output(cmd)?; |
579 | if !output.status.success() { | ||
580 | bail!( | ||
581 | "rustc --print cfg -O exited with exit code ({})", | ||
582 | output | ||
583 | .status | ||
584 | .code() | ||
585 | .map_or(String::from("no exit code"), |code| format!("{}", code)) | ||
586 | ); | ||
587 | } | ||
588 | Ok(String::from_utf8(output.stdout)?) | 557 | Ok(String::from_utf8(output.stdout)?) |
589 | })() { | 558 | }(); |
559 | |||
560 | match rustc_cfgs { | ||
590 | Ok(rustc_cfgs) => { | 561 | Ok(rustc_cfgs) => { |
591 | for line in rustc_cfgs.lines() { | 562 | for line in rustc_cfgs.lines() { |
592 | match line.find('=') { | 563 | match line.find('=') { |
@@ -599,8 +570,21 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { | |||
599 | } | 570 | } |
600 | } | 571 | } |
601 | } | 572 | } |
602 | Err(e) => log::error!("failed to get rustc cfgs: {}", e), | 573 | Err(e) => log::error!("failed to get rustc cfgs: {:#}", e), |
603 | } | 574 | } |
604 | 575 | ||
605 | cfg_options | 576 | cfg_options |
606 | } | 577 | } |
578 | |||
579 | fn output(mut cmd: Command) -> Result<Output> { | ||
580 | let output = cmd.output().with_context(|| format!("{:?} failed", cmd))?; | ||
581 | if !output.status.success() { | ||
582 | match String::from_utf8(output.stderr) { | ||
583 | Ok(stderr) if !stderr.is_empty() => { | ||
584 | bail!("{:?} failed, {}\nstderr:\n{}", cmd, output.status, stderr) | ||
585 | } | ||
586 | _ => bail!("{:?} failed, {}", cmd, output.status), | ||
587 | } | ||
588 | } | ||
589 | Ok(output) | ||
590 | } | ||
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 | ||
3 | use anyhow::{bail, Context, Result}; | ||
4 | use std::{ | 3 | use 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 | ||
9 | use anyhow::{bail, Result}; | ||
10 | use ra_arena::{Arena, Idx}; | 10 | use ra_arena::{Arena, Idx}; |
11 | use ra_env::get_path_for_executable; | 11 | |
12 | use crate::output; | ||
12 | 13 | ||
13 | #[derive(Default, Debug, Clone)] | 14 | #[derive(Default, Debug, Clone)] |
14 | pub struct Sysroot { | 15 | pub struct Sysroot { |
@@ -85,50 +86,22 @@ impl Sysroot { | |||
85 | } | 86 | } |
86 | } | 87 | } |
87 | 88 | ||
88 | fn create_command_text(program: &str, args: &[&str]) -> String { | ||
89 | format!("{} {}", program, args.join(" ")) | ||
90 | } | ||
91 | |||
92 | fn 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 | |||
119 | fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> { | 89 | fn 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!( |