aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_project_model/src/lib.rs')
-rw-r--r--crates/ra_project_model/src/lib.rs88
1 files changed, 36 insertions, 52 deletions
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
14use anyhow::{bail, Context, Result}; 14use anyhow::{bail, Context, Result};
15use ra_cfg::CfgOptions; 15use ra_cfg::CfgOptions;
16use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId}; 16use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId};
17use ra_env::get_path_for_executable;
18use rustc_hash::FxHashMap; 17use rustc_hash::FxHashMap;
19use serde_json::from_reader; 18use 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
579fn 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}