Skip to content

Commit 16fb171

Browse files
committed
Validate the uniqueness of build scripts
1 parent 2b610dc commit 16fb171

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! It is a bit tricky because we need match explicit information from `Cargo.toml`
1111
//! with implicit info in directory layout.
1212
13-
use std::collections::HashSet;
13+
use std::collections::{HashMap, HashSet};
1414
use std::fs::{self, DirEntry};
1515
use std::path::{Path, PathBuf};
1616

@@ -104,6 +104,7 @@ pub(super) fn to_targets(
104104
if metabuild.is_some() {
105105
anyhow::bail!("cannot specify both `metabuild` and `build`");
106106
}
107+
validate_unique_build_scripts(custom_build)?;
107108
for script in custom_build {
108109
let script_path = Path::new(script);
109110
let name = format!(
@@ -901,6 +902,35 @@ fn validate_unique_names(targets: &[TomlTarget], target_kind: &str) -> CargoResu
901902
Ok(())
902903
}
903904

905+
/// Will check a list of build scripts, and make sure script file stems are unique within a vector.
906+
fn validate_unique_build_scripts(scripts: &[String]) -> CargoResult<()> {
907+
let mut seen = HashMap::new();
908+
for script in scripts {
909+
let stem = Path::new(script).file_stem().unwrap().to_str().unwrap();
910+
seen.entry(stem)
911+
.or_insert_with(Vec::new)
912+
.push(script.as_str());
913+
}
914+
let mut conflict_file_stem = false;
915+
let mut err_msg = String::from(
916+
"found build scripts with duplicate file stems, but all build scripts must have a unique file stem",
917+
);
918+
for (stem, paths) in seen {
919+
if paths.len() > 1 {
920+
conflict_file_stem = true;
921+
err_msg += &format!(
922+
"\nBuild Scripts : {} have the same file stem, that is {}",
923+
paths.join(", "),
924+
stem
925+
);
926+
}
927+
}
928+
if conflict_file_stem {
929+
anyhow::bail!(err_msg);
930+
}
931+
Ok(())
932+
}
933+
904934
fn configure(
905935
toml: &TomlTarget,
906936
target: &mut Target,

tests/testsuite/build_scripts_multiple.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -870,32 +870,12 @@ fn duplicate_build_script_stems() {
870870
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
871871
.with_status(101)
872872
.with_stderr_data(str![[r#"
873-
[WARNING] output filename collision.
874-
The build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)` has the same output filename as the build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)`.
875-
Colliding filename is: [ROOT]/foo/target/debug/build/foo-[HASH]/build_script_build1-[HASH]
876-
The targets should have unique names.
877-
Consider changing their names to be unique or compiling them separately.
878-
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
879-
[WARNING] output filename collision.
880-
The build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)` has the same output filename as the build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)`.
881-
Colliding filename is: [ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build1
882-
The targets should have unique names.
883-
Consider changing their names to be unique or compiling them separately.
884-
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
885-
[WARNING] output filename collision.
886-
The build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)` has the same output filename as the build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)`.
887-
Colliding filename is: [ROOT]/foo/target/debug/build/foo-[HASH]/build_script_build1-[HASH].dwp
888-
The targets should have unique names.
889-
Consider changing their names to be unique or compiling them separately.
890-
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
891-
[WARNING] output filename collision.
892-
The build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)` has the same output filename as the build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)`.
893-
Colliding filename is: [ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build1.dwp
894-
The targets should have unique names.
895-
Consider changing their names to be unique or compiling them separately.
896-
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
897-
[COMPILING] foo v0.1.0 ([ROOT]/foo)
898-
...
873+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
874+
875+
Caused by:
876+
found build scripts with duplicate file stems, but all build scripts must have a unique file stem
877+
Build Scripts : build1.rs, foo/build1.rs have the same file stem, that is build1
878+
899879
"#]])
900880
.run();
901881
}

0 commit comments

Comments
 (0)