Skip to content

Commit d48d421

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

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,22 @@ 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 = HashSet::new();
908+
for script in scripts {
909+
let stem = Path::new(script).file_stem().unwrap().to_str().unwrap();
910+
if !seen.insert(stem) {
911+
anyhow::bail!(
912+
"found duplicate build script file stem {}, \
913+
but all build scripts must have a unique file stem",
914+
stem
915+
);
916+
}
917+
}
918+
Ok(())
919+
}
920+
904921
fn configure(
905922
toml: &TomlTarget,
906923
target: &mut Target,

tests/testsuite/build_scripts_multiple.rs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -870,32 +870,11 @@ 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 duplicate build script file stem build1, but all build scripts must have a unique file stem
877+
899878
"#]])
900879
.run();
901880
}

0 commit comments

Comments
 (0)