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

feat: Add hint for adding members to workspace #13411

Merged
merged 1 commit into from
Feb 8, 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
24 changes: 19 additions & 5 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,11 +844,24 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
}

// Try to add the new package to the workspace members.
update_manifest_with_new_member(
if update_manifest_with_new_member(
&root_manifest_path,
&mut workspace_document,
&display_path,
)?;
)? {
config.shell().status(
"Adding",
format!(
"`{}` as member of workspace at `{}`",
PathBuf::from(&display_path)
.file_name()
.unwrap()
.to_str()
.unwrap(),
root_manifest_path.parent().unwrap().display()
),
)?
epage marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Expand Down Expand Up @@ -965,7 +978,7 @@ fn update_manifest_with_new_member(
root_manifest_path: &Path,
workspace_document: &mut toml_edit::Document,
display_path: &str,
) -> CargoResult<()> {
) -> CargoResult<bool> {
// 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 @@ -983,7 +996,7 @@ fn update_manifest_with_new_member(
.with_context(|| format!("cannot build glob pattern from `{}`", pat))?;

if pattern.matches(&display_path) {
return Ok(());
return Ok(false);
}
}

Expand All @@ -1003,7 +1016,8 @@ fn update_manifest_with_new_member(
write_atomic(
&root_manifest_path,
workspace_document.to_string().to_string().as_bytes(),
)
)?;
Ok(true)
}

fn get_display_path(root_manifest_path: &Path, package_path: &Path) -> CargoResult<String> {
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_init/workspace_add_member/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating binary (application) package
Adding `foo` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating library `bar` package
Adding `bar` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating binary (application) `foo` package
Adding `foo` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating binary (application) `foo` package
Adding `foo` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating binary (application) `foo` package
Adding `foo` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating binary (application) `foo` package
Adding `foo` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating library `bar` package
Adding `bar` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1 change: 1 addition & 0 deletions tests/testsuite/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@ fn new_creates_members_list() {
p.cargo("new --lib bar")
.with_stderr("\
[CREATING] library `bar` package
[ADDING] `bar` as member of workspace at `[ROOT]/foo`
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
")
.run();
Expand Down