Skip to content

Commit 0391b14

Browse files
committed
fix: Don't add the new package to workspace.members if there is no existing workspace in Cargo.toml
1 parent 3a4fdba commit 0391b14

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -970,33 +970,34 @@ fn update_manifest_with_new_member(
970970
// in the array already includes the new package's relative path.
971971
// - Add the relative path if the members don't match the new package's path.
972972
// - Create a new members array if there are no members element in the workspace yet.
973-
if let Some(members) = workspace_document
974-
.get_mut("workspace")
975-
.and_then(|workspace| workspace.get_mut("members"))
976-
.and_then(|members| members.as_array_mut())
977-
{
978-
for member in members.iter() {
979-
let pat = member
980-
.as_str()
981-
.with_context(|| format!("invalid non-string member `{}`", member))?;
982-
let pattern = glob::Pattern::new(pat)
983-
.with_context(|| format!("cannot build glob pattern from `{}`", pat))?;
973+
if let Some(workspace) = workspace_document.get_mut("workspace") {
974+
if let Some(members) = workspace
975+
.get_mut("members")
976+
.and_then(|members| members.as_array_mut())
977+
{
978+
for member in members.iter() {
979+
let pat = member
980+
.as_str()
981+
.with_context(|| format!("invalid non-string member `{}`", member))?;
982+
let pattern = glob::Pattern::new(pat)
983+
.with_context(|| format!("cannot build glob pattern from `{}`", pat))?;
984+
985+
if pattern.matches(&display_path) {
986+
return Ok(());
987+
}
988+
}
984989

985-
if pattern.matches(&display_path) {
986-
return Ok(());
990+
let was_sorted = is_sorted(members.iter().map(Value::as_str));
991+
members.push(display_path);
992+
if was_sorted {
993+
members.sort_by(|lhs, rhs| lhs.as_str().cmp(&rhs.as_str()));
987994
}
988-
}
995+
} else {
996+
let mut array = Array::new();
997+
array.push(display_path);
989998

990-
let was_sorted = is_sorted(members.iter().map(Value::as_str));
991-
members.push(display_path);
992-
if was_sorted {
993-
members.sort_by(|lhs, rhs| lhs.as_str().cmp(&rhs.as_str()));
999+
workspace["members"] = toml_edit::value(array);
9941000
}
995-
} else {
996-
let mut array = Array::new();
997-
array.push(display_path);
998-
999-
workspace_document["workspace"]["members"] = toml_edit::value(array);
10001001
}
10011002

10021003
write_atomic(

tests/testsuite/cargo_new/add_members_to_non_workspace/out/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
workspace = { members = ["bar"] }
21
[package]
32
name = "foo"
43
version = "0.1.0"

0 commit comments

Comments
 (0)