mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Pass required features to cargo when using run action
When using `F1`->`Rust Analyzer: Run` action on an `example`, pass its `required-features` to `cargo run`. This allows to run examples that were otherwise impossible to run with RA.
This commit is contained in:
parent
ba33054802
commit
662dd7c27d
2 changed files with 21 additions and 4 deletions
|
@ -210,6 +210,8 @@ pub struct TargetData {
|
||||||
pub kind: TargetKind,
|
pub kind: TargetKind,
|
||||||
/// Is this target a proc-macro
|
/// Is this target a proc-macro
|
||||||
pub is_proc_macro: bool,
|
pub is_proc_macro: bool,
|
||||||
|
/// Required features of the target without which it won't build
|
||||||
|
pub required_features: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
@ -348,6 +350,7 @@ impl CargoWorkspace {
|
||||||
root: AbsPathBuf::assert(PathBuf::from(&meta_tgt.src_path)),
|
root: AbsPathBuf::assert(PathBuf::from(&meta_tgt.src_path)),
|
||||||
kind: TargetKind::new(meta_tgt.kind.as_slice()),
|
kind: TargetKind::new(meta_tgt.kind.as_slice()),
|
||||||
is_proc_macro,
|
is_proc_macro,
|
||||||
|
required_features: meta_tgt.required_features.clone(),
|
||||||
});
|
});
|
||||||
pkg_data.targets.push(tgt);
|
pkg_data.targets.push(tgt);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
//! See `CargoTargetSpec`
|
//! See `CargoTargetSpec`
|
||||||
|
|
||||||
|
use std::mem;
|
||||||
|
|
||||||
use cfg::{CfgAtom, CfgExpr};
|
use cfg::{CfgAtom, CfgExpr};
|
||||||
use ide::{FileId, RunnableKind, TestId};
|
use ide::{FileId, RunnableKind, TestId};
|
||||||
use project_model::{self, ManifestPath, TargetKind};
|
use project_model::{self, ManifestPath, TargetKind};
|
||||||
|
@ -18,17 +20,22 @@ pub(crate) struct CargoTargetSpec {
|
||||||
pub(crate) package: String,
|
pub(crate) package: String,
|
||||||
pub(crate) target: String,
|
pub(crate) target: String,
|
||||||
pub(crate) target_kind: TargetKind,
|
pub(crate) target_kind: TargetKind,
|
||||||
|
pub(crate) required_features: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CargoTargetSpec {
|
impl CargoTargetSpec {
|
||||||
pub(crate) fn runnable_args(
|
pub(crate) fn runnable_args(
|
||||||
snap: &GlobalStateSnapshot,
|
snap: &GlobalStateSnapshot,
|
||||||
spec: Option<CargoTargetSpec>,
|
mut spec: Option<CargoTargetSpec>,
|
||||||
kind: &RunnableKind,
|
kind: &RunnableKind,
|
||||||
cfg: &Option<CfgExpr>,
|
cfg: &Option<CfgExpr>,
|
||||||
) -> Result<(Vec<String>, Vec<String>)> {
|
) -> Result<(Vec<String>, Vec<String>)> {
|
||||||
let mut args = Vec::new();
|
let mut args = Vec::new();
|
||||||
let mut extra_args = Vec::new();
|
let mut extra_args = Vec::new();
|
||||||
|
|
||||||
|
let target_required_features =
|
||||||
|
spec.as_mut().map(|spec| mem::take(&mut spec.required_features)).unwrap_or(Vec::new());
|
||||||
|
|
||||||
match kind {
|
match kind {
|
||||||
RunnableKind::Test { test_id, attr } => {
|
RunnableKind::Test { test_id, attr } => {
|
||||||
args.push("test".to_string());
|
args.push("test".to_string());
|
||||||
|
@ -87,14 +94,20 @@ impl CargoTargetSpec {
|
||||||
let cargo_config = snap.config.cargo();
|
let cargo_config = snap.config.cargo();
|
||||||
if cargo_config.all_features {
|
if cargo_config.all_features {
|
||||||
args.push("--all-features".to_string());
|
args.push("--all-features".to_string());
|
||||||
|
|
||||||
|
for feature in target_required_features {
|
||||||
|
args.push("--features".to_string());
|
||||||
|
args.push(feature);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut features = Vec::new();
|
let mut features = Vec::new();
|
||||||
if let Some(cfg) = cfg.as_ref() {
|
if let Some(cfg) = cfg.as_ref() {
|
||||||
required_features(cfg, &mut features);
|
required_features(cfg, &mut features);
|
||||||
}
|
}
|
||||||
for feature in cargo_config.features {
|
|
||||||
features.push(feature.clone());
|
features.extend(cargo_config.features);
|
||||||
}
|
features.extend(target_required_features);
|
||||||
|
|
||||||
features.dedup();
|
features.dedup();
|
||||||
for feature in features {
|
for feature in features {
|
||||||
args.push("--features".to_string());
|
args.push("--features".to_string());
|
||||||
|
@ -126,6 +139,7 @@ impl CargoTargetSpec {
|
||||||
package: cargo_ws.package_flag(package_data),
|
package: cargo_ws.package_flag(package_data),
|
||||||
target: target_data.name.clone(),
|
target: target_data.name.clone(),
|
||||||
target_kind: target_data.kind,
|
target_kind: target_data.kind,
|
||||||
|
required_features: target_data.required_features.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Some(res))
|
Ok(Some(res))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue