Skip to content

Commit 64036fc

Browse files
committed
Add a warning for invalid cfgs
Signed-off-by: hi-rustin <[email protected]>
1 parent 1945b44 commit 64036fc

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

crates/cargo-platform/src/cfg.rs

+9
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ impl CfgExpr {
8181
}
8282
}
8383

84+
pub fn is_valid_key(key: &str) -> bool {
85+
if key.starts_with("cfg(") && key.ends_with(')') {
86+
let cfg = &key[4..key.len() - 1];
87+
CfgExpr::from_str(cfg).is_ok()
88+
} else {
89+
false
90+
}
91+
}
92+
8493
pub fn matches(&self, cfg: &[Cfg]) -> bool {
8594
match *self {
8695
CfgExpr::Not(ref e) => !e.matches(cfg),

src/cargo/core/compiler/compilation.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{BTreeSet, HashMap};
1+
use std::collections::{BTreeSet, HashMap, HashSet};
22
use std::env;
33
use std::ffi::{OsStr, OsString};
44
use std::path::PathBuf;
@@ -398,14 +398,22 @@ fn target_runner(
398398
return Ok(Some((path, v.args)));
399399
}
400400

401+
let mut invalid_cfgs: HashSet<String> = HashSet::default();
401402
// try target.'cfg(...)'.runner
402403
let target_cfg = bcx.target_data.info(kind).cfg();
403404
let mut cfgs = bcx
404405
.config
405406
.target_cfgs()?
406407
.iter()
407408
.filter_map(|(key, cfg)| cfg.runner.as_ref().map(|runner| (key, runner)))
408-
.filter(|(key, _runner)| CfgExpr::matches_key(key, target_cfg));
409+
.filter(|(key, _runner)| {
410+
if !CfgExpr::is_valid_key(key) {
411+
invalid_cfgs.insert(key.to_string());
412+
return false;
413+
}
414+
CfgExpr::matches_key(key, target_cfg)
415+
});
416+
409417
let matching_runner = cfgs.next();
410418
if let Some((key, runner)) = cfgs.next() {
411419
anyhow::bail!(
@@ -418,6 +426,13 @@ fn target_runner(
418426
runner.definition
419427
);
420428
}
429+
if !invalid_cfgs.is_empty() {
430+
bcx.config.shell().warn(format!(
431+
"invalid target configuration key{}: {}",
432+
if invalid_cfgs.len() == 1 { "" } else { "s" },
433+
invalid_cfgs.into_iter().collect::<Vec<_>>().join(", ")
434+
))?;
435+
}
421436
Ok(matching_runner.map(|(_k, runner)| {
422437
(
423438
runner.val.path.clone().resolve_program(bcx.config),

0 commit comments

Comments
 (0)