Skip to content
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

cargo/init: avoid target.name assignments if possible #13606

Merged
merged 1 commit into from
Mar 19, 2024
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: 6 additions & 13 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ impl fmt::Display for NewProjectKind {

struct SourceFileInformation {
relative_path: String,
target_name: String,
bin: bool,
}

Expand Down Expand Up @@ -344,20 +343,17 @@ 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 => {
let content = paths::read(&path.join(pp.clone()))?;
let isbin = content.contains("fn main");
SourceFileInformation {
relative_path: pp,
target_name: package_name.to_string(),
bin: isbin,
}
}
Expand All @@ -372,7 +368,7 @@ fn detect_source_paths_and_types(

for i in detected_files {
if i.bin {
if let Some(x) = BTreeMap::get::<str>(&duplicates_checker, i.target_name.as_ref()) {
if let Some(x) = BTreeMap::get::<str>(&duplicates_checker, &name) {
anyhow::bail!(
"\
multiple possible binary sources found:
Expand All @@ -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!(
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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(),
};
Expand Down Expand Up @@ -497,7 +491,7 @@ pub fn init(opts: &NewOptions, gctx: &GlobalContext) -> CargoResult<NewProjectKi
let has_bin = kind.is_bin();

if src_paths_types.is_empty() {
src_paths_types.push(plan_new_source_file(has_bin, name.to_string()));
src_paths_types.push(plan_new_source_file(has_bin));
} else if src_paths_types.len() == 1 && !src_paths_types.iter().any(|x| x.bin == has_bin) {
// we've found the only file and it's not the type user wants. Change the type and warn
let file_type = if src_paths_types[0].bin {
Expand Down Expand Up @@ -790,7 +784,7 @@ fn mk(gctx: &GlobalContext, opts: &MkOptions<'_>) -> 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(
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ name = "case"
path = "case.rs"

[lib]
name = "case"
path = "lib.rs"
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ edition = "2021"
[dependencies]

[lib]
name = "case"
path = "case.rs"
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ edition = "2021"
[dependencies]

[lib]
name = "case"
path = "lib.rs"
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ edition = "2021"
[dependencies]

[lib]
name = "case"
path = "lib.rs"