Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn on unknown fields and confusable targets #681

Merged
merged 3 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 20 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CrossToml>, env: Environment) -> Self {
Config { toml, env }
Expand Down Expand Up @@ -293,6 +312,7 @@ mod tests {
}
}

#[cfg(test)]
mod test_config {

use super::*;
Expand Down
20 changes: 17 additions & 3 deletions src/cross_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,28 @@ pub struct CrossTargetConfig {
#[derive(Debug, Deserialize, PartialEq)]
pub struct CrossToml {
#[serde(default, rename = "target")]
targets: HashMap<Target, CrossTargetConfig>,
build: Option<CrossBuildConfig>,
pub targets: HashMap<Target, CrossTargetConfig>,
pub build: Option<CrossBuildConfig>,
}

impl CrossToml {
/// Parses the [`CrossToml`] from a string
pub fn from_str(toml_str: &str) -> Result<Self> {
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::<Vec<_>>().join(", ")
);
}

Ok(cfg)
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ fn run() -> Result<ExitStatus> {
.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
Expand Down