From 2980ba1fde50a6fc8863750b9dd7f09e3c1227ce Mon Sep 17 00:00:00 2001 From: robojumper Date: Mon, 4 May 2020 13:29:09 +0200 Subject: Support build.rs cargo:rustc-cfg --- crates/ra_project_model/src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 731cbd291..2d5d61b61 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -399,6 +399,13 @@ impl ProjectWorkspace { let cfg_options = { let mut opts = default_cfg_options.clone(); opts.insert_features(cargo[pkg].features.iter().map(Into::into)); + opts.insert_cfgs( + cargo[pkg] + .cfgs + .iter() + .filter_map(|c| c.to_str()) + .map(Into::into), + ); opts }; let mut env = Env::default(); -- cgit v1.2.3 From f2dd233ddc60b647fe9c32ea2d712224005ae99e Mon Sep 17 00:00:00 2001 From: robojumper Date: Tue, 5 May 2020 14:53:52 +0200 Subject: Assume cargo_metadata uses String for cfgs soon --- crates/ra_project_model/src/lib.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 2d5d61b61..7231da221 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -399,13 +399,7 @@ impl ProjectWorkspace { let cfg_options = { let mut opts = default_cfg_options.clone(); opts.insert_features(cargo[pkg].features.iter().map(Into::into)); - opts.insert_cfgs( - cargo[pkg] - .cfgs - .iter() - .filter_map(|c| c.to_str()) - .map(Into::into), - ); + opts.insert_cfgs(cargo[pkg].cfgs.iter().map(Into::into)); opts }; let mut env = Env::default(); -- cgit v1.2.3 From 5aa1bba107ef434e61c3136120b9478a307d67a9 Mon Sep 17 00:00:00 2001 From: Craig Disselkoen Date: Tue, 5 May 2020 13:44:43 -0700 Subject: more generic, find rustc as well --- crates/ra_project_model/src/lib.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index c2b33c1dc..5028b6b6d 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -1,6 +1,7 @@ //! FIXME: write short doc here mod cargo_workspace; +mod find_executables; mod json_project; mod sysroot; -- cgit v1.2.3 From 303b444dbb66019fc916dd350e54f7675aa3007f Mon Sep 17 00:00:00 2001 From: Craig Disselkoen Date: Tue, 5 May 2020 14:07:10 -0700 Subject: pull function out into new crate ra_env; use in ra_flycheck as well --- crates/ra_project_model/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 5028b6b6d..e4b86f1e2 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -1,7 +1,6 @@ //! FIXME: write short doc here mod cargo_workspace; -mod find_executables; mod json_project; mod sysroot; @@ -15,6 +14,7 @@ use std::{ use anyhow::{bail, Context, Result}; use ra_cfg::CfgOptions; use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId}; +use ra_env::get_path_for_executable; use rustc_hash::FxHashMap; use serde_json::from_reader; @@ -559,7 +559,7 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { match (|| -> Result { // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here. - let mut cmd = Command::new("rustc"); + let mut cmd = Command::new(get_path_for_executable("rustc")?); cmd.args(&["--print", "cfg", "-O"]); if let Some(target) = target { cmd.args(&["--target", target.as_str()]); -- cgit v1.2.3 From d3110859ba4e97cf17d2c997befa92fb63bfb138 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 May 2020 02:56:53 +0200 Subject: Move feature desugaring to the right abstraction layer --- crates/ra_project_model/src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 8703429d4..c226ffa57 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -398,8 +398,18 @@ impl ProjectWorkspace { let edition = cargo[pkg].edition; let cfg_options = { let mut opts = default_cfg_options.clone(); - opts.insert_features(cargo[pkg].features.iter().map(Into::into)); - opts.insert_cfgs(cargo[pkg].cfgs.iter().map(Into::into)); + for feature in cargo[pkg].features.iter() { + opts.insert_key_value("feature".into(), feature.into()); + } + for cfg in cargo[pkg].cfgs.iter() { + match cfg.find('=') { + Some(split) => opts.insert_key_value( + cfg[..split].into(), + cfg[split + 1..].trim_matches('"').into(), + ), + None => opts.insert_atom(cfg.into()), + }; + } opts }; let mut env = Env::default(); -- cgit v1.2.3 From 6713be0b130670324c61d9deb38b7b6aee6a8bac Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 May 2020 12:25:36 +0200 Subject: Rename ra_env -> ra_toolchain --- crates/ra_project_model/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 88a6ffb2a..4f0b9c77e 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -14,7 +14,7 @@ use std::{ use anyhow::{bail, Context, Result}; use ra_cfg::CfgOptions; use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId}; -use ra_env::get_path_for_executable; +use ra_toolchain::get_path_for_executable; use rustc_hash::FxHashMap; use serde_json::from_reader; -- cgit v1.2.3 From ecff5dc141046c5b9e40639657247a05fb9b0344 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 May 2020 14:54:29 +0200 Subject: Cleanup --- crates/ra_project_model/src/lib.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 4f0b9c77e..5a0a87ed7 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -8,13 +8,12 @@ use std::{ fs::{read_dir, File, ReadDir}, io::{self, BufReader}, path::{Path, PathBuf}, - process::Command, + process::{Command, Output}, }; use anyhow::{bail, Context, Result}; use ra_cfg::CfgOptions; use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId}; -use ra_toolchain::get_path_for_executable; use rustc_hash::FxHashMap; use serde_json::from_reader; @@ -568,25 +567,18 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { } } - match (|| -> Result { + let rustc_cfgs = || -> Result { // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here. - let mut cmd = Command::new(get_path_for_executable("rustc")?); + let mut cmd = Command::new(ra_toolchain::rustc()); cmd.args(&["--print", "cfg", "-O"]); if let Some(target) = target { cmd.args(&["--target", target.as_str()]); } - let output = cmd.output().context("Failed to get output from rustc --print cfg -O")?; - if !output.status.success() { - bail!( - "rustc --print cfg -O exited with exit code ({})", - output - .status - .code() - .map_or(String::from("no exit code"), |code| format!("{}", code)) - ); - } + let output = output(cmd)?; Ok(String::from_utf8(output.stdout)?) - })() { + }(); + + match rustc_cfgs { Ok(rustc_cfgs) => { for line in rustc_cfgs.lines() { match line.find('=') { @@ -599,8 +591,16 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { } } } - Err(e) => log::error!("failed to get rustc cfgs: {}", e), + Err(e) => log::error!("failed to get rustc cfgs: {:#}", e), } cfg_options } + +fn output(mut cmd: Command) -> Result { + let output = cmd.output().with_context(|| format!("{:?} failed", cmd))?; + if !output.status.success() { + bail!("{:?} failed, {}", cmd, output.status) + } + Ok(output) +} -- cgit v1.2.3 From f739e0119c0d74c155f91ad27616a638fe494e2d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 May 2020 18:53:53 +0200 Subject: Add stderr to error message --- crates/ra_project_model/src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 5a0a87ed7..3adb8baf6 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -600,7 +600,12 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { fn output(mut cmd: Command) -> Result { let output = cmd.output().with_context(|| format!("{:?} failed", cmd))?; if !output.status.success() { - bail!("{:?} failed, {}", cmd, output.status) + match String::from_utf8(output.stderr) { + Ok(stderr) if !stderr.is_empty() => { + bail!("{:?} failed, {}\nstderr:\n{}", cmd, output.status, stderr) + } + _ => bail!("{:?} failed, {}", cmd, output.status), + } } Ok(output) } -- cgit v1.2.3 From e83a2912b8deaab560d1ea39232c06a29530d6e5 Mon Sep 17 00:00:00 2001 From: veetaha Date: Sat, 9 May 2020 02:51:59 +0300 Subject: Simpify project discovery --- crates/ra_project_model/src/lib.rs | 51 +++++++++++--------------------------- 1 file changed, 15 insertions(+), 36 deletions(-) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 3adb8baf6..4f098b706 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -88,46 +88,28 @@ impl ProjectRoot { } pub fn discover(path: &Path) -> io::Result> { - if let Some(project_json) = find_rust_project_json(path) { + if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") { return Ok(vec![ProjectRoot::ProjectJson(project_json)]); } return find_cargo_toml(path) .map(|paths| paths.into_iter().map(ProjectRoot::CargoToml).collect()); - fn find_rust_project_json(path: &Path) -> Option { - if path.ends_with("rust-project.json") { - return Some(path.to_path_buf()); - } - - let mut curr = Some(path); - while let Some(path) = curr { - let candidate = path.join("rust-project.json"); - if candidate.exists() { - return Some(candidate); - } - curr = path.parent(); - } - - None - } - fn find_cargo_toml(path: &Path) -> io::Result> { - if path.ends_with("Cargo.toml") { - return Ok(vec![path.to_path_buf()]); + match find_in_parent_dirs(path, "Cargo.toml") { + Some(it) => Ok(vec![it]), + None => Ok(find_cargo_toml_in_child_dir(read_dir(path)?)), } + } - if let Some(p) = find_cargo_toml_in_parent_dir(path) { - return Ok(vec![p]); + fn find_in_parent_dirs(path: &Path, target_file_name: &str) -> Option { + if path.ends_with(target_file_name) { + return Some(path.to_owned()); } - let entities = read_dir(path)?; - Ok(find_cargo_toml_in_child_dir(entities)) - } - - fn find_cargo_toml_in_parent_dir(path: &Path) -> Option { let mut curr = Some(path); + while let Some(path) = curr { - let candidate = path.join("Cargo.toml"); + let candidate = path.join(target_file_name); if candidate.exists() { return Some(candidate); } @@ -139,14 +121,11 @@ impl ProjectRoot { fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec { // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects - let mut valid_canditates = vec![]; - for entity in entities.filter_map(Result::ok) { - let candidate = entity.path().join("Cargo.toml"); - if candidate.exists() { - valid_canditates.push(candidate) - } - } - valid_canditates + entities + .filter_map(Result::ok) + .map(|it| it.path().join("Cargo.toml")) + .filter(|it| it.exists()) + .collect() } } } -- cgit v1.2.3