diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ccc0bbbf..0992bd45e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +- #681 - Warn on unknown fields and confusable targets - #665 - when not using [env.volumes](https://github.com/cross-rs/cross#mounting-volumes-into-the-build-environment), mount project in /project - #624 - Add `build.default-target` - #670 - Use serde for deserialization of Cross.toml diff --git a/Cargo.lock b/Cargo.lock index e6494382c..ffe034fdc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -108,6 +108,7 @@ dependencies = [ "nix", "rustc_version", "serde", + "serde_ignored", "serde_json", "shell-escape", "toml", @@ -315,6 +316,15 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_ignored" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c2c7d39d14f2f2ea82239de71594782f186fd03501ac81f0ce08e674819ff2f" +dependencies = [ + "serde", +] + [[package]] name = "serde_json" version = "1.0.79" diff --git a/Cargo.toml b/Cargo.toml index 6ff964273..14e3ba4af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ which = { version = "4", default_features = false } shell-escape = "0.1" serde = { version = "1", features = ["derive"] } serde_json = "1" +serde_ignored = "0.1.2" [target.'cfg(not(windows))'.dependencies] nix = "0.23" diff --git a/src/config.rs b/src/config.rs index 5b4a22c29..3ec2ab8a0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -117,6 +117,25 @@ impl Config { } } + pub fn confusable_target(&self, target: &Target) { + if let Some(keys) = self.toml.as_ref().map(|t| t.targets.keys()) { + for mentioned_target in keys { + let mentioned_target_norm = mentioned_target + .to_string() + .replace(|c| c == '-' || c == '_', "") + .to_lowercase(); + let target_norm = target + .to_string() + .replace(|c| c == '-' || c == '_', "") + .to_lowercase(); + if mentioned_target != target && mentioned_target_norm == target_norm { + eprintln!("Warning: a target named \"{mentioned_target}\" is mentioned in the Cross configuration, but the current specified target is \"{target}\"."); + eprintln!(" > Is the target misspelled in the Cross configuration?"); + } + } + } + } + #[cfg(test)] fn new_with(toml: Option, env: Environment) -> Self { Config { toml, env } @@ -293,6 +312,7 @@ mod tests { } } + #[cfg(test)] mod test_config { use super::*; diff --git a/src/cross_toml.rs b/src/cross_toml.rs index 2a571f175..f080157e6 100644 --- a/src/cross_toml.rs +++ b/src/cross_toml.rs @@ -39,14 +39,28 @@ pub struct CrossTargetConfig { #[derive(Debug, Deserialize, PartialEq)] pub struct CrossToml { #[serde(default, rename = "target")] - targets: HashMap, - build: Option, + pub targets: HashMap, + pub build: Option, } impl CrossToml { /// Parses the [`CrossToml`] from a string pub fn from_str(toml_str: &str) -> Result { - let cfg: CrossToml = toml::from_str(toml_str)?; + let tomld = &mut toml::Deserializer::new(toml_str); + + let mut unused = std::collections::BTreeSet::new(); + + let cfg = serde_ignored::deserialize(tomld, |path| { + unused.insert(path.to_string()); + })?; + + if !unused.is_empty() { + eprintln!( + "Warning: found unused key(s) in Cross configuration:\n > {}", + unused.into_iter().collect::>().join(", ") + ); + } + Ok(cfg) } diff --git a/src/main.rs b/src/main.rs index eb7ad5943..f05d1ba4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -276,7 +276,7 @@ fn run() -> Result { .target .or_else(|| config.target(&target_list)) .unwrap_or_else(|| Target::from(host.triple(), &target_list)); - + config.confusable_target(&target); if host.is_supported(Some(&target)) { let mut sysroot = rustc::sysroot(&host, &target, verbose)?; let default_toolchain = sysroot