From b673c1010ffa6010d890ed1b320d53332a077220 Mon Sep 17 00:00:00 2001 From: David Rheinsberg Date: Tue, 19 Mar 2024 11:07:41 +0100 Subject: [PATCH] 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`, this commit strips the field completely, since all callers set it to the package-name. Signed-off-by: David Rheinsberg --- src/cargo/ops/cargo_new.rs | 19 ++++++------------- .../out/Cargo.toml | 1 - .../out/Cargo.toml | 1 - .../inferred_lib_with_git/out/Cargo.toml | 1 - .../lib_already_exists_nosrc/out/Cargo.toml | 1 - 5 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index de3aa052bfa..ca316146266 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -87,7 +87,6 @@ impl fmt::Display for NewProjectKind { struct SourceFileInformation { relative_path: String, - target_name: String, bin: bool, } @@ -344,12 +343,10 @@ fn detect_source_paths_and_types( let sfi = match i.handling { H::Bin => SourceFileInformation { relative_path: pp, - target_name: package_name.to_string(), bin: true, }, H::Lib => SourceFileInformation { relative_path: pp, - target_name: package_name.to_string(), bin: false, }, H::Detect => { @@ -357,7 +354,6 @@ fn detect_source_paths_and_types( let isbin = content.contains("fn main"); SourceFileInformation { relative_path: pp, - target_name: package_name.to_string(), bin: isbin, } } @@ -372,7 +368,7 @@ fn detect_source_paths_and_types( for i in detected_files { if i.bin { - if let Some(x) = BTreeMap::get::(&duplicates_checker, i.target_name.as_ref()) { + if let Some(x) = BTreeMap::get::(&duplicates_checker, &name) { anyhow::bail!( "\ multiple possible binary sources found: @@ -383,7 +379,7 @@ cannot automatically generate Cargo.toml as the main target would be ambiguous", &i.relative_path ); } - duplicates_checker.insert(i.target_name.as_ref(), i); + duplicates_checker.insert(name, i); } else { if let Some(plp) = previous_lib_relpath { anyhow::bail!( @@ -401,17 +397,15 @@ cannot automatically generate Cargo.toml as the main target would be ambiguous", Ok(()) } -fn plan_new_source_file(bin: bool, package_name: String) -> SourceFileInformation { +fn plan_new_source_file(bin: bool) -> SourceFileInformation { if bin { SourceFileInformation { relative_path: "src/main.rs".to_string(), - target_name: package_name, bin: true, } } else { SourceFileInformation { relative_path: "src/lib.rs".to_string(), - target_name: package_name, bin: false, } } @@ -460,7 +454,7 @@ pub fn new(opts: &NewOptions, gctx: &GlobalContext) -> CargoResult<()> { version_control: opts.version_control, path, name, - source_files: vec![plan_new_source_file(opts.kind.is_bin(), name.to_string())], + source_files: vec![plan_new_source_file(opts.kind.is_bin())], edition: opts.edition.as_deref(), registry: opts.registry.as_deref(), }; @@ -497,7 +491,7 @@ pub fn init(opts: &NewOptions, gctx: &GlobalContext) -> CargoResult) -> CargoResult<()> { if i.bin { if i.relative_path != "src/main.rs" { let mut bin = toml_edit::Table::new(); - bin["name"] = toml_edit::value(i.target_name.clone()); + bin["name"] = toml_edit::value(name); bin["path"] = toml_edit::value(i.relative_path.clone()); manifest["bin"] .or_insert(toml_edit::Item::ArrayOfTables( @@ -802,7 +796,6 @@ fn mk(gctx: &GlobalContext, opts: &MkOptions<'_>) -> CargoResult<()> { } } else if i.relative_path != "src/lib.rs" { let mut lib = toml_edit::Table::new(); - lib["name"] = toml_edit::value(i.target_name.clone()); lib["path"] = toml_edit::value(i.relative_path.clone()); manifest["lib"] = toml_edit::Item::Table(lib); } diff --git a/tests/testsuite/cargo_init/creates_binary_when_both_binlib_present/out/Cargo.toml b/tests/testsuite/cargo_init/creates_binary_when_both_binlib_present/out/Cargo.toml index 31363ea8211..7b042d4bb13 100644 --- a/tests/testsuite/cargo_init/creates_binary_when_both_binlib_present/out/Cargo.toml +++ b/tests/testsuite/cargo_init/creates_binary_when_both_binlib_present/out/Cargo.toml @@ -10,5 +10,4 @@ name = "case" path = "case.rs" [lib] -name = "case" path = "lib.rs" diff --git a/tests/testsuite/cargo_init/creates_library_when_instructed_and_has_bin_file/out/Cargo.toml b/tests/testsuite/cargo_init/creates_library_when_instructed_and_has_bin_file/out/Cargo.toml index 4102b7e84d1..6fa93832300 100644 --- a/tests/testsuite/cargo_init/creates_library_when_instructed_and_has_bin_file/out/Cargo.toml +++ b/tests/testsuite/cargo_init/creates_library_when_instructed_and_has_bin_file/out/Cargo.toml @@ -6,5 +6,4 @@ edition = "2021" [dependencies] [lib] -name = "case" path = "case.rs" diff --git a/tests/testsuite/cargo_init/inferred_lib_with_git/out/Cargo.toml b/tests/testsuite/cargo_init/inferred_lib_with_git/out/Cargo.toml index 10c8bf8ea60..290552e56b5 100644 --- a/tests/testsuite/cargo_init/inferred_lib_with_git/out/Cargo.toml +++ b/tests/testsuite/cargo_init/inferred_lib_with_git/out/Cargo.toml @@ -6,5 +6,4 @@ edition = "2021" [dependencies] [lib] -name = "case" path = "lib.rs" diff --git a/tests/testsuite/cargo_init/lib_already_exists_nosrc/out/Cargo.toml b/tests/testsuite/cargo_init/lib_already_exists_nosrc/out/Cargo.toml index 10c8bf8ea60..290552e56b5 100644 --- a/tests/testsuite/cargo_init/lib_already_exists_nosrc/out/Cargo.toml +++ b/tests/testsuite/cargo_init/lib_already_exists_nosrc/out/Cargo.toml @@ -6,5 +6,4 @@ edition = "2021" [dependencies] [lib] -name = "case" path = "lib.rs"