Skip to content

Commit 38c03b2

Browse files
committed
fix: deduplicate crate types in cargo rustc command to prevent filename collisions
1 parent 3e96f1a commit 38c03b2

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/bin/cargo/commands/rustc.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,17 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
9292
return Ok(());
9393
}
9494

95-
let crate_types = args
96-
.get_many::<String>(CRATE_TYPE_ARG_NAME)
97-
.into_iter()
98-
.flatten()
99-
.flat_map(|s| s.split(','))
100-
.filter(|s| !s.is_empty())
101-
.map(String::from)
102-
.collect::<Vec<String>>();
95+
let crate_types = {
96+
let mut seen = std::collections::HashSet::new();
97+
args.get_many::<String>(CRATE_TYPE_ARG_NAME)
98+
.into_iter()
99+
.flatten()
100+
.flat_map(|s| s.split(','))
101+
.filter(|s| !s.is_empty())
102+
.map(String::from)
103+
.filter(|s| seen.insert(s.clone()))
104+
.collect::<Vec<String>>()
105+
};
103106

104107
compile_opts.target_rustc_crate_types = if crate_types.is_empty() {
105108
None

tests/testsuite/rustc.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,3 +855,18 @@ fn precedence() {
855855
"#]])
856856
.run();
857857
}
858+
859+
#[cargo_test]
860+
fn build_with_duplicate_crate_types() {
861+
let p = project().file("src/lib.rs", "").build();
862+
863+
// Test with duplicate crate types specified directly
864+
p.cargo("rustc -v --crate-type staticlib --crate-type staticlib")
865+
.with_stderr_data(str![[r#"
866+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
867+
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type staticlib --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=991ef8c99cd510e0 -C extra-filename=-f0d97c77b50b1fb3 --out-dir [ROOT]/foo/target/debug/deps -L dependency=[ROOT]/foo/target/debug/deps`
868+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
869+
870+
"#]])
871+
.run();
872+
}

0 commit comments

Comments
 (0)