Skip to content

Commit 0b390cd

Browse files
committed
Auto merge of #14499 - felixmoebius:rustc-crate-type-parsing, r=weihanglo
Fix parsing of comma separated values in --crate-type flag ### What does this PR try to resolve? According to the documentation the `--crate-type` flag accepts a comma separated list of crate types. However, these are never actually split into individual items and passed verbatim to rustc. Since cargo fails to associate cases such as 'staticlib,cdylib' to a specific crate type internally, it has to invoke rustc to determine the output file types for this unknown crate type, which returns only the first file type of the first crate type in the list. Consequently cargo will be looking only for a single '.a' artifact on Linux to be copied to the target directory. Fix this by splitting the list of provided crate types into individual items before further processing them. Fixes #14498 ### How should we test and review this PR? Updated corresponding test cases ### Additional information
2 parents b958d79 + fb672fa commit 0b390cd

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/bin/cargo/commands/rustc.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,16 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
9191
ops::print(&ws, &compile_opts, opt_value)?;
9292
return Ok(());
9393
}
94-
let crate_types = values(args, CRATE_TYPE_ARG_NAME);
94+
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>>();
103+
95104
compile_opts.target_rustc_crate_types = if crate_types.is_empty() {
96105
None
97106
} else {

tests/testsuite/rustc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ fn build_with_crate_types_for_foo() {
238238
p.cargo("rustc -v --crate-type lib,cdylib")
239239
.with_stderr_data(str![[r#"
240240
[COMPILING] foo v0.0.1 ([ROOT]/foo)
241-
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib,cdylib [..]`
241+
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib --crate-type cdylib [..]`
242242
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
243243
244244
"#]])
@@ -302,7 +302,7 @@ fn build_with_crate_types_to_example() {
302302
.with_stderr_data(str![[r#"
303303
[COMPILING] foo v0.0.1 ([ROOT]/foo)
304304
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib [..]`
305-
[RUNNING] `rustc --crate-name ex --edition=2015 examples/ex.rs [..]--crate-type lib,cdylib [..]`
305+
[RUNNING] `rustc --crate-name ex --edition=2015 examples/ex.rs [..]--crate-type lib --crate-type cdylib [..]`
306306
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
307307
308308
"#]])
@@ -338,7 +338,7 @@ fn build_with_crate_types_to_one_of_multi_examples() {
338338
.with_stderr_data(str![[r#"
339339
[COMPILING] foo v0.0.1 ([ROOT]/foo)
340340
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib [..]`
341-
[RUNNING] `rustc --crate-name ex1 --edition=2015 examples/ex1.rs [..]--crate-type lib,cdylib [..]`
341+
[RUNNING] `rustc --crate-name ex1 --edition=2015 examples/ex1.rs [..]--crate-type lib --crate-type cdylib [..]`
342342
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
343343
344344
"#]])

0 commit comments

Comments
 (0)