rust/crates/rust-analyzer/src/cargo_target_spec.rs

225 lines
7.4 KiB
Rust
Raw Normal View History

2020-02-18 05:25:26 -06:00
//! See `CargoTargetSpec`
2020-10-21 06:57:12 -05:00
use cfg::{CfgAtom, CfgExpr};
2020-08-13 10:42:52 -05:00
use ide::{FileId, RunnableKind, TestId};
use project_model::{self, ManifestPath, TargetKind};
2020-07-08 11:22:57 -05:00
use vfs::AbsPathBuf;
2019-08-31 06:47:37 -05:00
2020-06-03 04:16:08 -05:00
use crate::{global_state::GlobalStateSnapshot, Result};
2020-02-18 05:25:26 -06:00
/// Abstract representation of Cargo target.
///
/// We use it to cook up the set of cli args we need to pass to Cargo to
/// build/test/run the target.
#[derive(Clone)]
2020-02-18 05:16:40 -06:00
pub(crate) struct CargoTargetSpec {
2020-06-24 08:52:07 -05:00
pub(crate) workspace_root: AbsPathBuf,
pub(crate) cargo_toml: ManifestPath,
2020-02-18 05:16:40 -06:00
pub(crate) package: String,
pub(crate) target: String,
pub(crate) target_kind: TargetKind,
}
impl CargoTargetSpec {
2020-02-18 05:17:47 -06:00
pub(crate) fn runnable_args(
snap: &GlobalStateSnapshot,
2020-02-18 05:17:47 -06:00
spec: Option<CargoTargetSpec>,
kind: &RunnableKind,
2020-10-22 12:19:18 -05:00
cfg: &Option<CfgExpr>,
2020-03-09 16:06:45 -05:00
) -> Result<(Vec<String>, Vec<String>)> {
let mut args = Vec::new();
let mut extra_args = Vec::new();
2020-02-18 05:17:47 -06:00
match kind {
2020-04-22 15:52:12 -05:00
RunnableKind::Test { test_id, attr } => {
2020-03-09 16:06:45 -05:00
args.push("test".to_string());
2020-02-18 05:17:47 -06:00
if let Some(spec) = spec {
spec.push_to(&mut args, kind);
2020-02-18 05:17:47 -06:00
}
2020-03-09 16:06:45 -05:00
extra_args.push(test_id.to_string());
2020-02-18 05:17:47 -06:00
if let TestId::Path(_) = test_id {
2020-03-09 16:06:45 -05:00
extra_args.push("--exact".to_string());
2020-02-18 05:17:47 -06:00
}
2020-03-09 16:06:45 -05:00
extra_args.push("--nocapture".to_string());
2020-04-22 15:52:12 -05:00
if attr.ignore {
extra_args.push("--ignored".to_string());
2020-04-22 15:52:12 -05:00
}
2020-02-18 05:17:47 -06:00
}
RunnableKind::TestMod { path } => {
2020-03-09 16:06:45 -05:00
args.push("test".to_string());
2020-02-18 05:17:47 -06:00
if let Some(spec) = spec {
spec.push_to(&mut args, kind);
2020-02-18 05:17:47 -06:00
}
2020-03-09 16:06:45 -05:00
extra_args.push(path.to_string());
extra_args.push("--nocapture".to_string());
2020-02-18 05:17:47 -06:00
}
RunnableKind::Bench { test_id } => {
2020-03-09 16:06:45 -05:00
args.push("bench".to_string());
2020-02-18 05:17:47 -06:00
if let Some(spec) = spec {
spec.push_to(&mut args, kind);
2020-02-18 05:17:47 -06:00
}
2020-03-09 16:06:45 -05:00
extra_args.push(test_id.to_string());
2020-02-18 05:17:47 -06:00
if let TestId::Path(_) = test_id {
2020-03-09 16:06:45 -05:00
extra_args.push("--exact".to_string());
2020-02-18 05:17:47 -06:00
}
2020-03-09 16:06:45 -05:00
extra_args.push("--nocapture".to_string());
2020-02-18 05:17:47 -06:00
}
RunnableKind::DocTest { test_id } => {
args.push("test".to_string());
args.push("--doc".to_string());
if let Some(spec) = spec {
spec.push_to(&mut args, kind);
}
extra_args.push(test_id.to_string());
extra_args.push("--nocapture".to_string());
}
2020-02-18 05:17:47 -06:00
RunnableKind::Bin => {
2020-08-26 10:33:03 -05:00
let subcommand = match spec {
Some(CargoTargetSpec { target_kind: TargetKind::Test, .. }) => "test",
_ => "run",
};
args.push(subcommand.to_string());
2020-02-18 05:17:47 -06:00
if let Some(spec) = spec {
spec.push_to(&mut args, kind);
2020-02-18 05:17:47 -06:00
}
}
}
let cargo_config = snap.config.cargo();
if cargo_config.all_features {
args.push("--all-features".to_string());
} else {
let mut features = Vec::new();
2020-10-22 12:19:18 -05:00
if let Some(cfg) = cfg.as_ref() {
required_features(cfg, &mut features);
}
for feature in cargo_config.features {
features.push(feature.clone());
}
features.dedup();
for feature in features {
args.push("--features".to_string());
args.push(feature);
}
2020-06-02 09:30:26 -05:00
}
2020-03-09 16:06:45 -05:00
Ok((args, extra_args))
2020-02-18 05:17:47 -06:00
}
2020-02-18 05:16:40 -06:00
pub(crate) fn for_file(
global_state_snapshot: &GlobalStateSnapshot,
2020-02-18 05:16:40 -06:00
file_id: FileId,
) -> Result<Option<CargoTargetSpec>> {
2020-06-24 17:41:08 -05:00
let crate_id = match global_state_snapshot.analysis.crate_for(file_id)?.first() {
Some(crate_id) => *crate_id,
None => return Ok(None),
};
let (cargo_ws, target) = match global_state_snapshot.cargo_target_for_crate_root(crate_id) {
Some(it) => it,
None => return Ok(None),
};
let target_data = &cargo_ws[target];
let package_data = &cargo_ws[target_data.package];
let res = CargoTargetSpec {
2020-06-11 04:04:09 -05:00
workspace_root: cargo_ws.workspace_root().to_path_buf(),
cargo_toml: package_data.manifest.clone(),
2021-06-12 22:54:16 -05:00
package: cargo_ws.package_flag(package_data),
target: target_data.name.clone(),
target_kind: target_data.kind,
};
Ok(Some(res))
}
pub(crate) fn push_to(self, buf: &mut Vec<String>, kind: &RunnableKind) {
buf.push("--package".to_string());
buf.push(self.package);
// Can't mix --doc with other target flags
if let RunnableKind::DocTest { .. } = kind {
return;
}
match self.target_kind {
TargetKind::Bin => {
buf.push("--bin".to_string());
buf.push(self.target);
}
TargetKind::Test => {
buf.push("--test".to_string());
buf.push(self.target);
}
TargetKind::Bench => {
buf.push("--bench".to_string());
buf.push(self.target);
}
TargetKind::Example => {
buf.push("--example".to_string());
buf.push(self.target);
}
TargetKind::Lib => {
buf.push("--lib".to_string());
}
2021-05-12 07:16:51 -05:00
TargetKind::Other | TargetKind::BuildScript => (),
}
}
}
2020-06-02 09:30:26 -05:00
/// Fill minimal features needed
fn required_features(cfg_expr: &CfgExpr, features: &mut Vec<String>) {
match cfg_expr {
2020-10-21 06:57:12 -05:00
CfgExpr::Atom(CfgAtom::KeyValue { key, value }) if key == "feature" => {
features.push(value.to_string())
}
2020-06-02 09:30:26 -05:00
CfgExpr::All(preds) => {
preds.iter().for_each(|cfg| required_features(cfg, features));
}
CfgExpr::Any(preds) => {
for cfg in preds {
let len_features = features.len();
required_features(cfg, features);
if len_features != features.len() {
break;
}
}
}
_ => {}
}
}
#[cfg(test)]
mod tests {
use super::*;
2020-08-13 03:19:09 -05:00
use cfg::CfgExpr;
use mbe::syntax_node_to_token_tree;
2020-08-12 11:26:51 -05:00
use syntax::{
2020-06-02 09:30:26 -05:00
ast::{self, AstNode},
SmolStr,
};
2020-07-23 09:22:17 -05:00
fn check(cfg: &str, expected_features: &[&str]) {
let cfg_expr = {
let source_file = ast::SourceFile::parse(cfg).ok().unwrap();
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
let (tt, _) = syntax_node_to_token_tree(tt.syntax());
2020-07-23 09:22:17 -05:00
CfgExpr::parse(&tt)
};
2020-06-02 09:30:26 -05:00
2020-07-23 09:22:17 -05:00
let mut features = vec![];
required_features(&cfg_expr, &mut features);
2020-06-02 09:30:26 -05:00
2020-07-23 09:22:17 -05:00
let expected_features =
expected_features.iter().map(|&it| SmolStr::new(it)).collect::<Vec<_>>();
2020-06-02 09:30:26 -05:00
2020-07-23 09:22:17 -05:00
assert_eq!(features, expected_features);
}
2020-06-02 09:30:26 -05:00
2020-07-23 09:22:17 -05:00
#[test]
fn test_cfg_expr_minimal_features_needed() {
check(r#"#![cfg(feature = "baz")]"#, &["baz"]);
check(r#"#![cfg(all(feature = "baz", feature = "foo"))]"#, &["baz", "foo"]);
check(r#"#![cfg(any(feature = "baz", feature = "foo", unix))]"#, &["baz"]);
check(r#"#![cfg(foo)]"#, &[]);
2020-06-02 09:30:26 -05:00
}
}