diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index b78ba243e3eb..58c13aa6cebe 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -960,9 +960,6 @@ fn update_manifest_with_new_member( workspace_root.display() ) })?; - let relpath = relpath - .to_str() - .with_context(|| format!("invalid non-unicode path `{}`", relpath.display()))?; // Don't add the new package to the workspace's members // if there is an exclusion match for it. @@ -972,13 +969,20 @@ fn update_manifest_with_new_member( .and_then(|members| members.as_array()) { for member in members { - let pat = member.as_str().unwrap(); - if pat == relpath { + let pat = member + .as_str() + .with_context(|| format!("invalid members' element `{}`", member))?; + if Path::new(pat) == relpath { return Ok(()); } } } + let display_path = relpath + .to_str() + .map(|s| s.replace('\\', "/")) + .with_context(|| format!("invalid non-unicode path `{}`", relpath.display()))?; + // 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. @@ -993,7 +997,7 @@ fn update_manifest_with_new_member( let pattern = glob::Pattern::new(pat) .with_context(|| format!("cannot build glob pattern from `{}`", pat))?; - if pattern.matches(relpath) { + if pattern.matches_path(&relpath) { return Ok(()); } } @@ -1007,7 +1011,7 @@ fn update_manifest_with_new_member( anyhow::bail!("[workspace.members] is not an array"); }; - toml_members.push(relpath); + toml_members.push(&display_path); if let Some(decor) = decor { if let Some(last) = toml_members @@ -1041,7 +1045,7 @@ fn update_manifest_with_new_member( } let mut array = Array::new(); - array.push(relpath); + array.push(&display_path); if !assume_new_line { array.decor_mut().set_suffix("\n"); } @@ -1052,7 +1056,7 @@ fn update_manifest_with_new_member( ws.insert("members", toml_edit::value(array)); } else { let mut array = Array::new(); - array.push(relpath); + array.push(&display_path); array.decor_mut().set_suffix("\n"); workspace_document["workspace"]["members"] = toml_edit::value(array); }