Skip to content

Commit

Permalink
Improve file found in multiple build targets warning
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Oct 27, 2022
1 parent 1985caf commit 48072bc
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::collections::hash_map::Entry;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fmt;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -1737,12 +1738,22 @@ impl TomlManifest {
debug!("manifest has no build targets");
}

if let Err(e) = unique_build_targets(&targets, package_root) {
warnings.push(format!(
"file found to be present in multiple \
build targets: {}",
e
));
if let Err(conflict_targets) = unique_build_targets(&targets, package_root) {
conflict_targets.iter().for_each(|c| {
warnings.push(format!(
"file `{}` found to be present in multiple \
build targets:\n{}",
c.0.display().to_string(),
c.1.iter()
.map(|t| format!(
" * This file exist in `{}` as a `{}` target.",
t.name(),
t.kind().description()
))
.collect::<Vec<String>>()
.join("\n")
));
})
}

if let Some(links) = &package.links {
Expand Down Expand Up @@ -2442,16 +2453,34 @@ fn default_readme_from_package_root(package_root: &Path) -> Option<String> {

/// Checks a list of build targets, and ensures the target names are unique within a vector.
/// If not, the name of the offending build target is returned.
fn unique_build_targets(targets: &[Target], package_root: &Path) -> Result<(), String> {
let mut seen = HashSet::new();
fn unique_build_targets(
targets: &[Target],
package_root: &Path,
) -> Result<(), HashMap<PathBuf, Vec<Target>>> {
let mut source_targets = HashMap::new();
for target in targets {
if let TargetSourcePath::Path(path) = target.src_path() {
let full = package_root.join(path);
if !seen.insert(full.clone()) {
return Err(full.display().to_string());
match source_targets.entry(full) {
Entry::Vacant(e) => {
e.insert(vec![target.clone()]);
}
Entry::Occupied(mut e) => {
e.get_mut().push(target.clone());
}
}
}
}

let conflict_targets = source_targets
.into_iter()
.filter(|(_, targets)| targets.len() > 1)
.collect::<HashMap<_, _>>();

if !conflict_targets.is_empty() {
return Err(conflict_targets);
}

Ok(())
}

Expand Down

0 comments on commit 48072bc

Please sign in to comment.