Skip to content

Commit

Permalink
Use Path objects to compare values.
Browse files Browse the repository at this point in the history
- Fixes issues comparing paths on Windows.
- Replace backslashes with slashes to display the path in the Cargo.toml
  file. This prevents issues serializing the value on different
  platforms.

Signed-off-by: David Calavera <[email protected]>
  • Loading branch information
calavera committed Oct 10, 2023
1 parent dcd0c49 commit fdfecac
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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(());
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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");
}
Expand All @@ -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);
}
Expand Down

0 comments on commit fdfecac

Please sign in to comment.