Skip to content

Commit bb9b25f

Browse files
committed
Auto merge of #13391 - linyihai:non_workspace_add_lib, r=weihanglo
Don't add the new package to workspace.members if there is no existing workspace in Cargo.toml. ### What does this PR try to resolve? Fixed #13345 If the current package has no workspace table in Cargo.toml, then if you run `cargo add foo`, don't create the workspace inline item and don't add `foo` into the workspace.members. ### How should we test and review this PR? Reviewed by commit by commit. ### Additional information
2 parents e3de3bf + 0391b14 commit bb9b25f

File tree

20 files changed

+143
-23
lines changed

20 files changed

+143
-23
lines changed

src/cargo/ops/cargo_new.rs

+24-23
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(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "foo"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::curr_dir;
3+
use cargo_test_support::CargoCommand;
4+
use cargo_test_support::Project;
5+
6+
#[cargo_test]
7+
fn case() {
8+
let project = Project::from_template(curr_dir!().join("in"));
9+
let project_root = project.root();
10+
let cwd = &project_root;
11+
12+
snapbox::cmd::Command::cargo_ui()
13+
.arg("new")
14+
.args(["bar", "--lib"])
15+
.current_dir(cwd)
16+
.assert()
17+
.success()
18+
.stdout_matches_path(curr_dir!().join("stdout.log"))
19+
.stderr_matches_path(curr_dir!().join("stderr.log"));
20+
21+
assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "foo"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "bar"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn add(left: usize, right: usize) -> usize {
2+
left + right
3+
}
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use super::*;
8+
9+
#[test]
10+
fn it_works() {
11+
let result = add(2, 2);
12+
assert_eq!(result, 4);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Creating library `bar` package
2+
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

tests/testsuite/cargo_new/add_members_to_non_workspace/stdout.log

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[workspace]
2+
3+
[package]
4+
name = "foo"
5+
version = "0.1.0"
6+
edition = "2021"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::curr_dir;
3+
use cargo_test_support::CargoCommand;
4+
use cargo_test_support::Project;
5+
6+
#[cargo_test]
7+
fn case() {
8+
let project = Project::from_template(curr_dir!().join("in"));
9+
let project_root = project.root();
10+
let cwd = &project_root;
11+
12+
snapbox::cmd::Command::cargo_ui()
13+
.arg("new")
14+
.args(["bar", "--lib"])
15+
.current_dir(cwd)
16+
.assert()
17+
.success()
18+
.stdout_matches_path(curr_dir!().join("stdout.log"))
19+
.stderr_matches_path(curr_dir!().join("stderr.log"));
20+
21+
assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[workspace]
2+
members = ["bar"]
3+
4+
[package]
5+
name = "foo"
6+
version = "0.1.0"
7+
edition = "2021"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "bar"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn add(left: usize, right: usize) -> usize {
2+
left + right
3+
}
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use super::*;
8+
9+
#[test]
10+
fn it_works() {
11+
let result = add(2, 2);
12+
assert_eq!(result, 4);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Creating library `bar` package
2+
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

tests/testsuite/cargo_new/add_members_to_workspace_without_members/stdout.log

Whitespace-only changes.

tests/testsuite/cargo_new/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
mod add_members_to_non_workspace;
12
mod add_members_to_workspace_format_previous_items;
23
mod add_members_to_workspace_format_sorted;
34
mod add_members_to_workspace_with_absolute_package_path;
45
mod add_members_to_workspace_with_empty_members;
56
mod add_members_to_workspace_with_exclude_list;
67
mod add_members_to_workspace_with_members_glob;
8+
mod add_members_to_workspace_without_members;
79
mod empty_name;
810
mod help;
911
mod inherit_workspace_lints;

0 commit comments

Comments
 (0)