Skip to content

Commit 8a11e15

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

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
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-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::path::PathBuf;
55

66
use cargo_platform::CfgExpr;
77
use cargo_util::{paths, ProcessBuilder};
8+
use im_rc::HashSet;
89

910
use super::BuildContext;
1011
use crate::core::compiler::{CompileKind, Metadata, Unit};
@@ -398,14 +399,22 @@ fn target_runner(
398399
return Ok(Some((path, v.args)));
399400
}
400401

402+
let mut invalid_cfgs: HashSet<String> = HashSet::default();
401403
// try target.'cfg(...)'.runner
402404
let target_cfg = bcx.target_data.info(kind).cfg();
403405
let mut cfgs = bcx
404406
.config
405407
.target_cfgs()?
406408
.iter()
407409
.filter_map(|(key, cfg)| cfg.runner.as_ref().map(|runner| (key, runner)))
408-
.filter(|(key, _runner)| CfgExpr::matches_key(key, target_cfg));
410+
.filter(|(key, _runner)| {
411+
if !CfgExpr::is_valid_key(key) {
412+
invalid_cfgs.insert(key.to_string());
413+
return false;
414+
}
415+
CfgExpr::matches_key(key, target_cfg)
416+
});
417+
409418
let matching_runner = cfgs.next();
410419
if let Some((key, runner)) = cfgs.next() {
411420
anyhow::bail!(
@@ -418,6 +427,13 @@ fn target_runner(
418427
runner.definition
419428
);
420429
}
430+
if !invalid_cfgs.is_empty() {
431+
bcx.config.shell().warn(format!(
432+
"invalid target configuration key{}: {}",
433+
if invalid_cfgs.len() == 1 { "" } else { "s" },
434+
invalid_cfgs.into_iter().collect::<Vec<_>>().join(", ")
435+
))?;
436+
}
421437
Ok(matching_runner.map(|(_k, runner)| {
422438
(
423439
runner.val.path.clone().resolve_program(bcx.config),

0 commit comments

Comments
 (0)