Skip to content

Commit 437ff04

Browse files
authored
fix: deduplicate crate types in cargo rustc command (#15314)
### What does this PR try to resolve? close: #15312 ### How should we test and review this PR? deduplicate `crate-type` in cargo `rustc` command
2 parents 3e96f1a + 36de02f commit 437ff04

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,3 +855,25 @@ 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+
p.cargo("rustc -v --crate-type staticlib --crate-type staticlib")
864+
.with_stderr_data(str![[r#"
865+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
866+
[RUNNING] `rustc [..] --crate-type staticlib --emit[..]
867+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
868+
869+
"#]])
870+
.run();
871+
872+
p.cargo("rustc -v --crate-type staticlib --crate-type staticlib")
873+
.with_stderr_data(str![[r#"
874+
[FRESH] foo v0.0.1 ([ROOT]/foo)
875+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
876+
877+
"#]])
878+
.run();
879+
}

0 commit comments

Comments
 (0)