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

Improve file found in multiple build targets warning #11299

Merged
merged 2 commits into from
Dec 2, 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
48 changes: 36 additions & 12 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fmt;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
Expand All @@ -8,6 +8,7 @@ use std::str::{self, FromStr};
use anyhow::{anyhow, bail, Context as _};
use cargo_platform::Platform;
use cargo_util::paths;
use itertools::Itertools;
use lazycell::LazyCell;
use log::{debug, trace};
use semver::{self, VersionReq};
Expand Down Expand Up @@ -1737,12 +1738,24 @@ 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(|(target_path, conflicts)| {
warnings.push(format!(
"file `{}` found to be present in multiple \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed that the term build target is not accurate and could be mistaken for config build.target. No need to fix it in this pull request (it is all over this file 😆)

build targets:\n{}",
target_path.display().to_string(),
conflicts
.iter()
.map(|t| format!(
" * `{}` target `{}`",
t.kind().description(),
t.name(),
))
.join("\n")
));
})
}

if let Some(links) = &package.links {
Expand Down Expand Up @@ -2442,16 +2455,27 @@ 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::<_, Vec<_>>::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());
}
source_targets.entry(full).or_default().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
4 changes: 3 additions & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ fn cargo_compile_duplicate_build_targets() {
p.cargo("build")
.with_stderr(
"\
warning: file found to be present in multiple build targets: [..]main.rs
warning: file `[..]main.rs` found to be present in multiple build targets:
* `lib` target `main`
* `bin` target `foo`
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
Expand Down