Skip to content

fix: deduplicate crate types in cargo rustc command #15314

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

Merged
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
19 changes: 11 additions & 8 deletions src/bin/cargo/commands/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,17 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
return Ok(());
}

let crate_types = args
.get_many::<String>(CRATE_TYPE_ARG_NAME)
.into_iter()
.flatten()
.flat_map(|s| s.split(','))
.filter(|s| !s.is_empty())
.map(String::from)
.collect::<Vec<String>>();
let crate_types = {
let mut seen = std::collections::HashSet::new();
args.get_many::<String>(CRATE_TYPE_ARG_NAME)
.into_iter()
.flatten()
.flat_map(|s| s.split(','))
.filter(|s| !s.is_empty())
.map(String::from)
.filter(|s| seen.insert(s.clone()))
.collect::<Vec<String>>()
};

compile_opts.target_rustc_crate_types = if crate_types.is_empty() {
None
Expand Down
22 changes: 22 additions & 0 deletions tests/testsuite/rustc.rs
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the patch! This looks good.

Would you mind splitting this into two commits?

  1. The first commit adds a test with the snapshot of the current problematic behavior (and the test passes). This can be seen as a reproduction verifying the bug does exist.
  2. The second commit contains both the actual fix, and the test change, so the test snapshot change could beautifully reflect the behavior change.

Copy link
Member

Choose a reason for hiding this comment

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

It is not a hard blocker btw!

Original file line number Diff line number Diff line change
Expand Up @@ -855,3 +855,25 @@ fn precedence() {
"#]])
.run();
}

#[cargo_test]
fn build_with_duplicate_crate_types() {
let p = project().file("src/lib.rs", "").build();

p.cargo("rustc -v --crate-type staticlib --crate-type staticlib")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc [..] --crate-type staticlib --emit[..]
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();

p.cargo("rustc -v --crate-type staticlib --crate-type staticlib")
.with_stderr_data(str![[r#"
[FRESH] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
}