Skip to content

Commit

Permalink
fix(new): Dont say were adding to a workspace when a regular package …
Browse files Browse the repository at this point in the history
…is in root

Fixes #13985
  • Loading branch information
epage committed May 30, 2024
1 parent 0b72605 commit 26c1678
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
50 changes: 26 additions & 24 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,38 +971,40 @@ fn update_manifest_with_new_member(
workspace_document: &mut toml_edit::DocumentMut,
display_path: &str,
) -> CargoResult<bool> {
let Some(workspace) = workspace_document.get_mut("workspace") else {
return Ok(false);
};

// If the members element already exist, check if one of the patterns
// in the array already includes the new package's relative path.
// - Add the relative path if the members don't match the new package's path.
// - Create a new members array if there are no members element in the workspace yet.
if let Some(workspace) = workspace_document.get_mut("workspace") {
if let Some(members) = workspace
.get_mut("members")
.and_then(|members| members.as_array_mut())
{
for member in members.iter() {
let pat = member
.as_str()
.with_context(|| format!("invalid non-string member `{}`", member))?;
let pattern = glob::Pattern::new(pat)
.with_context(|| format!("cannot build glob pattern from `{}`", pat))?;

if pattern.matches(&display_path) {
return Ok(false);
}
}
if let Some(members) = workspace
.get_mut("members")
.and_then(|members| members.as_array_mut())
{
for member in members.iter() {
let pat = member
.as_str()
.with_context(|| format!("invalid non-string member `{}`", member))?;
let pattern = glob::Pattern::new(pat)
.with_context(|| format!("cannot build glob pattern from `{}`", pat))?;

let was_sorted = is_sorted(members.iter().map(Value::as_str));
members.push(display_path);
if was_sorted {
members.sort_by(|lhs, rhs| lhs.as_str().cmp(&rhs.as_str()));
if pattern.matches(&display_path) {
return Ok(false);
}
} else {
let mut array = Array::new();
array.push(display_path);
}

workspace["members"] = toml_edit::value(array);
let was_sorted = is_sorted(members.iter().map(Value::as_str));
members.push(display_path);
if was_sorted {
members.sort_by(|lhs, rhs| lhs.as_str().cmp(&rhs.as_str()));
}
} else {
let mut array = Array::new();
array.push(display_path);

workspace["members"] = toml_edit::value(array);
}

write_atomic(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 26c1678

Please sign in to comment.