Skip to content

Commit ecb89a1

Browse files
committed
Auto merge of #13606 - dvdhrm:pr/initlib, r=weihanglo
cargo/init: avoid target.name assignments if possible Make `cargo init` skip `target.name` assignments if the default value is suitable. Currently, all paths of `cargo init` will set target-names to the package-name, ensuring that a suitable target-name is set. This is required for all targets but libraries. Unfortunately, library-names have more restrictions than other targets. For example, creating a library with dashes will lead to errors: mkdir foo-bar cd foo-bar touch foo-bar.rs cargo init --lib Fortunately, target-names for libraries are inferred from the package-name. Hence, by omitting the target-name, a valid configuration will be produced. Instead of adjusting `SourceFileInformation::target_name` to use `Option<String>`, this commit strips the field completely, since all callers set it to the package-name. Fixes: #11259
2 parents 3e7852b + b673c10 commit ecb89a1

File tree

5 files changed

+6
-17
lines changed

5 files changed

+6
-17
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ impl fmt::Display for NewProjectKind {
8787

8888
struct SourceFileInformation {
8989
relative_path: String,
90-
target_name: String,
9190
bin: bool,
9291
}
9392

@@ -344,20 +343,17 @@ fn detect_source_paths_and_types(
344343
let sfi = match i.handling {
345344
H::Bin => SourceFileInformation {
346345
relative_path: pp,
347-
target_name: package_name.to_string(),
348346
bin: true,
349347
},
350348
H::Lib => SourceFileInformation {
351349
relative_path: pp,
352-
target_name: package_name.to_string(),
353350
bin: false,
354351
},
355352
H::Detect => {
356353
let content = paths::read(&path.join(pp.clone()))?;
357354
let isbin = content.contains("fn main");
358355
SourceFileInformation {
359356
relative_path: pp,
360-
target_name: package_name.to_string(),
361357
bin: isbin,
362358
}
363359
}
@@ -372,7 +368,7 @@ fn detect_source_paths_and_types(
372368

373369
for i in detected_files {
374370
if i.bin {
375-
if let Some(x) = BTreeMap::get::<str>(&duplicates_checker, i.target_name.as_ref()) {
371+
if let Some(x) = BTreeMap::get::<str>(&duplicates_checker, &name) {
376372
anyhow::bail!(
377373
"\
378374
multiple possible binary sources found:
@@ -383,7 +379,7 @@ cannot automatically generate Cargo.toml as the main target would be ambiguous",
383379
&i.relative_path
384380
);
385381
}
386-
duplicates_checker.insert(i.target_name.as_ref(), i);
382+
duplicates_checker.insert(name, i);
387383
} else {
388384
if let Some(plp) = previous_lib_relpath {
389385
anyhow::bail!(
@@ -401,17 +397,15 @@ cannot automatically generate Cargo.toml as the main target would be ambiguous",
401397
Ok(())
402398
}
403399

404-
fn plan_new_source_file(bin: bool, package_name: String) -> SourceFileInformation {
400+
fn plan_new_source_file(bin: bool) -> SourceFileInformation {
405401
if bin {
406402
SourceFileInformation {
407403
relative_path: "src/main.rs".to_string(),
408-
target_name: package_name,
409404
bin: true,
410405
}
411406
} else {
412407
SourceFileInformation {
413408
relative_path: "src/lib.rs".to_string(),
414-
target_name: package_name,
415409
bin: false,
416410
}
417411
}
@@ -460,7 +454,7 @@ pub fn new(opts: &NewOptions, gctx: &GlobalContext) -> CargoResult<()> {
460454
version_control: opts.version_control,
461455
path,
462456
name,
463-
source_files: vec![plan_new_source_file(opts.kind.is_bin(), name.to_string())],
457+
source_files: vec![plan_new_source_file(opts.kind.is_bin())],
464458
edition: opts.edition.as_deref(),
465459
registry: opts.registry.as_deref(),
466460
};
@@ -497,7 +491,7 @@ pub fn init(opts: &NewOptions, gctx: &GlobalContext) -> CargoResult<NewProjectKi
497491
let has_bin = kind.is_bin();
498492

499493
if src_paths_types.is_empty() {
500-
src_paths_types.push(plan_new_source_file(has_bin, name.to_string()));
494+
src_paths_types.push(plan_new_source_file(has_bin));
501495
} else if src_paths_types.len() == 1 && !src_paths_types.iter().any(|x| x.bin == has_bin) {
502496
// we've found the only file and it's not the type user wants. Change the type and warn
503497
let file_type = if src_paths_types[0].bin {
@@ -790,7 +784,7 @@ fn mk(gctx: &GlobalContext, opts: &MkOptions<'_>) -> CargoResult<()> {
790784
if i.bin {
791785
if i.relative_path != "src/main.rs" {
792786
let mut bin = toml_edit::Table::new();
793-
bin["name"] = toml_edit::value(i.target_name.clone());
787+
bin["name"] = toml_edit::value(name);
794788
bin["path"] = toml_edit::value(i.relative_path.clone());
795789
manifest["bin"]
796790
.or_insert(toml_edit::Item::ArrayOfTables(
@@ -802,7 +796,6 @@ fn mk(gctx: &GlobalContext, opts: &MkOptions<'_>) -> CargoResult<()> {
802796
}
803797
} else if i.relative_path != "src/lib.rs" {
804798
let mut lib = toml_edit::Table::new();
805-
lib["name"] = toml_edit::value(i.target_name.clone());
806799
lib["path"] = toml_edit::value(i.relative_path.clone());
807800
manifest["lib"] = toml_edit::Item::Table(lib);
808801
}

tests/testsuite/cargo_init/creates_binary_when_both_binlib_present/out/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ name = "case"
1010
path = "case.rs"
1111

1212
[lib]
13-
name = "case"
1413
path = "lib.rs"

tests/testsuite/cargo_init/creates_library_when_instructed_and_has_bin_file/out/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ edition = "2021"
66
[dependencies]
77

88
[lib]
9-
name = "case"
109
path = "case.rs"

tests/testsuite/cargo_init/inferred_lib_with_git/out/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ edition = "2021"
66
[dependencies]
77

88
[lib]
9-
name = "case"
109
path = "lib.rs"

tests/testsuite/cargo_init/lib_already_exists_nosrc/out/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ edition = "2021"
66
[dependencies]
77

88
[lib]
9-
name = "case"
109
path = "lib.rs"

0 commit comments

Comments
 (0)