2020-02-18 12:25:26 +01:00
|
|
|
//! See `CargoTargetSpec`
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2022-02-06 18:56:25 +03:00
|
|
|
use std::mem;
|
|
|
|
|
2020-10-21 13:57:12 +02:00
|
|
|
use cfg::{CfgAtom, CfgExpr};
|
2020-08-13 17:42:52 +02:00
|
|
|
use ide::{FileId, RunnableKind, TestId};
|
2022-09-19 16:40:43 +02:00
|
|
|
use project_model::{self, CargoFeatures, ManifestPath, TargetKind};
|
2020-07-08 18:22:57 +02:00
|
|
|
use vfs::AbsPathBuf;
|
2019-08-31 14:47:37 +03:00
|
|
|
|
2020-06-03 11:16:08 +02:00
|
|
|
use crate::{global_state::GlobalStateSnapshot, Result};
|
2019-01-12 13:00:58 -05:00
|
|
|
|
2020-02-18 12:25:26 +01: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.
|
2020-04-26 10:40:13 +02:00
|
|
|
#[derive(Clone)]
|
2020-02-18 12:16:40 +01:00
|
|
|
pub(crate) struct CargoTargetSpec {
|
2020-06-24 15:52:07 +02:00
|
|
|
pub(crate) workspace_root: AbsPathBuf,
|
2021-07-19 21:20:10 +03:00
|
|
|
pub(crate) cargo_toml: ManifestPath,
|
2020-02-18 12:16:40 +01:00
|
|
|
pub(crate) package: String,
|
|
|
|
pub(crate) target: String,
|
|
|
|
pub(crate) target_kind: TargetKind,
|
2022-02-06 18:56:25 +03:00
|
|
|
pub(crate) required_features: Vec<String>,
|
2019-01-12 13:00:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CargoTargetSpec {
|
2020-02-18 12:17:47 +01:00
|
|
|
pub(crate) fn runnable_args(
|
2020-07-02 16:13:24 +02:00
|
|
|
snap: &GlobalStateSnapshot,
|
2022-04-27 19:53:56 +02:00
|
|
|
spec: Option<CargoTargetSpec>,
|
2020-02-18 12:17:47 +01:00
|
|
|
kind: &RunnableKind,
|
2020-10-22 19:19:18 +02:00
|
|
|
cfg: &Option<CfgExpr>,
|
2020-03-09 22:06:45 +01:00
|
|
|
) -> Result<(Vec<String>, Vec<String>)> {
|
|
|
|
let mut args = Vec::new();
|
|
|
|
let mut extra_args = Vec::new();
|
2022-02-06 18:56:25 +03:00
|
|
|
|
2020-02-18 12:17:47 +01:00
|
|
|
match kind {
|
2020-04-22 22:52:12 +02:00
|
|
|
RunnableKind::Test { test_id, attr } => {
|
2022-09-19 16:40:43 +02:00
|
|
|
args.push("test".to_owned());
|
2020-03-09 22:06:45 +01:00
|
|
|
extra_args.push(test_id.to_string());
|
2020-02-18 12:17:47 +01:00
|
|
|
if let TestId::Path(_) = test_id {
|
2022-09-19 16:40:43 +02:00
|
|
|
extra_args.push("--exact".to_owned());
|
2020-02-18 12:17:47 +01:00
|
|
|
}
|
2022-09-19 16:40:43 +02:00
|
|
|
extra_args.push("--nocapture".to_owned());
|
2020-04-22 22:52:12 +02:00
|
|
|
if attr.ignore {
|
2022-09-19 16:40:43 +02:00
|
|
|
extra_args.push("--ignored".to_owned());
|
2020-04-22 22:52:12 +02:00
|
|
|
}
|
2020-02-18 12:17:47 +01:00
|
|
|
}
|
|
|
|
RunnableKind::TestMod { path } => {
|
2022-09-19 16:40:43 +02:00
|
|
|
args.push("test".to_owned());
|
|
|
|
extra_args.push(path.clone());
|
|
|
|
extra_args.push("--nocapture".to_owned());
|
2020-02-18 12:17:47 +01:00
|
|
|
}
|
|
|
|
RunnableKind::Bench { test_id } => {
|
2022-09-19 16:40:43 +02:00
|
|
|
args.push("bench".to_owned());
|
2020-03-09 22:06:45 +01:00
|
|
|
extra_args.push(test_id.to_string());
|
2020-02-18 12:17:47 +01:00
|
|
|
if let TestId::Path(_) = test_id {
|
2022-09-19 16:40:43 +02:00
|
|
|
extra_args.push("--exact".to_owned());
|
2020-02-18 12:17:47 +01:00
|
|
|
}
|
2022-09-19 16:40:43 +02:00
|
|
|
extra_args.push("--nocapture".to_owned());
|
2020-02-18 12:17:47 +01:00
|
|
|
}
|
2020-05-05 17:43:28 +02:00
|
|
|
RunnableKind::DocTest { test_id } => {
|
2022-09-19 16:40:43 +02:00
|
|
|
args.push("test".to_owned());
|
|
|
|
args.push("--doc".to_owned());
|
2020-05-05 17:43:28 +02:00
|
|
|
extra_args.push(test_id.to_string());
|
2022-09-19 16:40:43 +02:00
|
|
|
extra_args.push("--nocapture".to_owned());
|
2020-05-05 17:43:28 +02:00
|
|
|
}
|
2020-02-18 12:17:47 +01:00
|
|
|
RunnableKind::Bin => {
|
2020-08-26 17:33:03 +02:00
|
|
|
let subcommand = match spec {
|
|
|
|
Some(CargoTargetSpec { target_kind: TargetKind::Test, .. }) => "test",
|
|
|
|
_ => "run",
|
|
|
|
};
|
2022-09-19 16:40:43 +02:00
|
|
|
args.push(subcommand.to_owned());
|
2020-02-18 12:17:47 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-21 10:48:42 +02:00
|
|
|
|
2022-04-27 19:53:56 +02:00
|
|
|
let target_required_features = if let Some(mut spec) = spec {
|
|
|
|
let required_features = mem::take(&mut spec.required_features);
|
|
|
|
spec.push_to(&mut args, kind);
|
|
|
|
required_features
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
|
|
|
|
2021-01-06 13:54:28 +03:00
|
|
|
let cargo_config = snap.config.cargo();
|
2022-02-06 18:56:25 +03:00
|
|
|
|
2022-09-19 16:40:43 +02:00
|
|
|
match &cargo_config.features {
|
|
|
|
CargoFeatures::All => {
|
|
|
|
args.push("--all-features".to_owned());
|
|
|
|
for feature in target_required_features {
|
|
|
|
args.push("--features".to_owned());
|
|
|
|
args.push(feature);
|
|
|
|
}
|
2020-07-02 16:13:24 +02:00
|
|
|
}
|
2022-09-19 16:40:43 +02:00
|
|
|
CargoFeatures::Selected { features, no_default_features } => {
|
|
|
|
let mut feats = Vec::new();
|
|
|
|
if let Some(cfg) = cfg.as_ref() {
|
|
|
|
required_features(cfg, &mut feats);
|
|
|
|
}
|
2022-02-06 18:56:25 +03:00
|
|
|
|
2022-09-19 16:40:43 +02:00
|
|
|
feats.extend(features.iter().cloned());
|
|
|
|
feats.extend(target_required_features);
|
2022-02-06 18:56:25 +03:00
|
|
|
|
2022-09-19 16:40:43 +02:00
|
|
|
feats.dedup();
|
|
|
|
for feature in feats {
|
|
|
|
args.push("--features".to_owned());
|
|
|
|
args.push(feature);
|
|
|
|
}
|
|
|
|
|
|
|
|
if *no_default_features {
|
|
|
|
args.push("--no-default-features".to_owned());
|
|
|
|
}
|
2020-07-02 16:13:24 +02:00
|
|
|
}
|
2020-06-02 16:30:26 +02:00
|
|
|
}
|
2020-03-09 22:06:45 +01:00
|
|
|
Ok((args, extra_args))
|
2020-02-18 12:17:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 12:16:40 +01:00
|
|
|
pub(crate) fn for_file(
|
2020-06-17 17:51:46 +02:00
|
|
|
global_state_snapshot: &GlobalStateSnapshot,
|
2020-02-18 12:16:40 +01:00
|
|
|
file_id: FileId,
|
|
|
|
) -> Result<Option<CargoTargetSpec>> {
|
2022-10-17 17:53:50 +02:00
|
|
|
let (cargo_ws, target) = match global_state_snapshot.cargo_target_for_file_id(file_id) {
|
2020-06-17 17:51:46 +02:00
|
|
|
Some(it) => it,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2020-12-08 16:28:21 +03:00
|
|
|
|
|
|
|
let target_data = &cargo_ws[target];
|
|
|
|
let package_data = &cargo_ws[target_data.package];
|
2020-06-17 17:51:46 +02:00
|
|
|
let res = CargoTargetSpec {
|
2020-06-11 11:04:09 +02:00
|
|
|
workspace_root: cargo_ws.workspace_root().to_path_buf(),
|
2020-12-08 16:28:21 +03:00
|
|
|
cargo_toml: package_data.manifest.clone(),
|
2021-06-13 09:24:16 +05:30
|
|
|
package: cargo_ws.package_flag(package_data),
|
2020-12-08 16:28:21 +03:00
|
|
|
target: target_data.name.clone(),
|
|
|
|
target_kind: target_data.kind,
|
2022-02-06 18:56:25 +03:00
|
|
|
required_features: target_data.required_features.clone(),
|
2020-06-17 17:51:46 +02:00
|
|
|
};
|
2020-12-08 16:28:21 +03:00
|
|
|
|
2020-06-17 17:51:46 +02:00
|
|
|
Ok(Some(res))
|
2019-01-12 13:00:58 -05:00
|
|
|
}
|
|
|
|
|
2020-05-05 17:43:28 +02:00
|
|
|
pub(crate) fn push_to(self, buf: &mut Vec<String>, kind: &RunnableKind) {
|
2022-09-19 16:40:43 +02:00
|
|
|
buf.push("--package".to_owned());
|
2019-01-12 13:00:58 -05:00
|
|
|
buf.push(self.package);
|
2020-05-05 17:43:28 +02:00
|
|
|
|
|
|
|
// Can't mix --doc with other target flags
|
|
|
|
if let RunnableKind::DocTest { .. } = kind {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-12 13:00:58 -05:00
|
|
|
match self.target_kind {
|
|
|
|
TargetKind::Bin => {
|
2022-09-19 16:40:43 +02:00
|
|
|
buf.push("--bin".to_owned());
|
2019-01-12 13:00:58 -05:00
|
|
|
buf.push(self.target);
|
|
|
|
}
|
|
|
|
TargetKind::Test => {
|
2022-09-19 16:40:43 +02:00
|
|
|
buf.push("--test".to_owned());
|
2019-01-12 13:00:58 -05:00
|
|
|
buf.push(self.target);
|
|
|
|
}
|
|
|
|
TargetKind::Bench => {
|
2022-09-19 16:40:43 +02:00
|
|
|
buf.push("--bench".to_owned());
|
2019-01-12 13:00:58 -05:00
|
|
|
buf.push(self.target);
|
|
|
|
}
|
|
|
|
TargetKind::Example => {
|
2022-09-19 16:40:43 +02:00
|
|
|
buf.push("--example".to_owned());
|
2019-01-12 13:00:58 -05:00
|
|
|
buf.push(self.target);
|
|
|
|
}
|
|
|
|
TargetKind::Lib => {
|
2022-09-19 16:40:43 +02:00
|
|
|
buf.push("--lib".to_owned());
|
2019-01-12 13:00:58 -05:00
|
|
|
}
|
2021-05-12 14:16:51 +02:00
|
|
|
TargetKind::Other | TargetKind::BuildScript => (),
|
2019-01-12 13:00:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-02 16:30:26 +02:00
|
|
|
|
|
|
|
/// Fill minimal features needed
|
|
|
|
fn required_features(cfg_expr: &CfgExpr, features: &mut Vec<String>) {
|
|
|
|
match cfg_expr {
|
2020-10-21 13:57:12 +02:00
|
|
|
CfgExpr::Atom(CfgAtom::KeyValue { key, value }) if key == "feature" => {
|
|
|
|
features.push(value.to_string())
|
|
|
|
}
|
2020-06-02 16:30:26 +02: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 10:19:09 +02:00
|
|
|
use cfg::CfgExpr;
|
2021-08-09 15:41:19 +03:00
|
|
|
use mbe::syntax_node_to_token_tree;
|
2020-08-12 18:26:51 +02:00
|
|
|
use syntax::{
|
2020-06-02 16:30:26 +02:00
|
|
|
ast::{self, AstNode},
|
|
|
|
SmolStr,
|
|
|
|
};
|
|
|
|
|
2020-07-23 16:22:17 +02: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();
|
2021-08-09 15:41:19 +03:00
|
|
|
let (tt, _) = syntax_node_to_token_tree(tt.syntax());
|
2020-07-23 16:22:17 +02:00
|
|
|
CfgExpr::parse(&tt)
|
|
|
|
};
|
2020-06-02 16:30:26 +02:00
|
|
|
|
2020-07-23 16:22:17 +02:00
|
|
|
let mut features = vec![];
|
|
|
|
required_features(&cfg_expr, &mut features);
|
2020-06-02 16:30:26 +02:00
|
|
|
|
2020-07-23 16:22:17 +02:00
|
|
|
let expected_features =
|
|
|
|
expected_features.iter().map(|&it| SmolStr::new(it)).collect::<Vec<_>>();
|
2020-06-02 16:30:26 +02:00
|
|
|
|
2020-07-23 16:22:17 +02:00
|
|
|
assert_eq!(features, expected_features);
|
|
|
|
}
|
2020-06-02 16:30:26 +02:00
|
|
|
|
2020-07-23 16:22:17 +02: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 16:30:26 +02:00
|
|
|
}
|
|
|
|
}
|