Repository: rust-lang/cargo (rust)

Issue:
cargo new: Confusing error message about adding to a workspace
### Problem

`cargo add` seems to generate a confusing message about adding a package to a workspace when there is no workspace, and it isn't actually added to the workspace.

### Steps

1. `cargo new foo`
2. `cd foo`
3. `cargo new bar`
    This prints: Adding `bar` as member of workspace at `/Temp/foo`
4. `cargo metadata | jq .workspace_members`
    Notice that `bar` was not added as a workspace member.  Also notice that foo's `Cargo.toml` was not modified.

### Possible Solution(s)

Should not print a message about adding `bar` to a workspace when it isn't actually added.

### Notes

_No response_

### Version

```text
cargo 1.80.0-nightly (431db31d0 2024-05-28)
release: 1.80.0-nightly
commit-hash: 431db31d0dbeda320caf8ef8535ea48eb3093407
commit-date: 2024-05-28
host: aarch64-apple-darwin
libgit2: 1.7.2 (sys:0.18.3 vendored)
libcurl: 8.6.0 (sys:0.4.72+curl-8.6.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Mac OS 14.5.0 [64-bit]
```


Pull request description:
fix(new): Dont say were adding to a workspace when a regular package is in root

Fixes #13985

<!-- homu-ignore:start -->
<!--
Thanks for submitting a pull request 🎉! Here are some tips for you:

* If this is your first contribution, read "Cargo Contribution Guide" first:
  https://doc.crates.io/contrib/
* Run `cargo fmt --all` to format your code changes.
* Small commits and pull requests are always preferable and easy to review.
* If your idea is large and needs feedback from the community, read how:
  https://doc.crates.io/contrib/process/#working-on-large-features
* Cargo takes care of compatibility. Read our design principles:
  https://doc.crates.io/contrib/design.html
* When changing help text of cargo commands, follow the steps to generate docs:
  https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages
* If your PR is not finished, set it as "draft" PR or add "WIP" in its title.
* It's ok to use the CI resources to test your PR, but please don't abuse them.

### What does this PR try to resolve?

Explain the motivation behind this change.
A clear overview along with an in-depth explanation are helpful.

You can use `Fixes #<issue number>` to associate this PR to an existing issue.

### How should we test and review this PR?

Demonstrate how you test this change and guide reviewers through your PR.
With a smooth review process, a pull request usually gets reviewed quicker.

If you don't know how to write and run your tests, please read the guide:
https://doc.crates.io/contrib/tests

### Additional information

Other information you want to mention in this PR, such as prior arts,
future extensions, an unresolved problem, or a TODO list.
-->
<!-- homu-ignore:end -->


Patch:
diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs
index fc0ac5931..4598db07e 100644
--- a/src/cargo/ops/cargo_new.rs
+++ b/src/cargo/ops/cargo_new.rs
@@ -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(
diff --git a/src/doc/man/cargo-package.md b/src/doc/man/cargo-package.md
index 05d092597..5057e59fe 100644
--- a/src/doc/man/cargo-package.md
+++ b/src/doc/man/cargo-package.md
@@ -61,13 +61,6 @@ Will generate a `.cargo_vcs_info.json` in the following format
 `path_in_vcs` will be set to a repo-relative path for packages
 in subdirectories of the version control repository.
 
-The compatibility of this file is maintained under the same policy
-as the JSON output of {{man "cargo-metadata" 1}}.
-
-Note that this file provides a best-effort snapshot of the VCS information.
-However, the provenance of the package is not verified.
-There is no guarantee that the source code in the tarball matches the VCS information.
-
 ## OPTIONS
 
 ### Package Options
diff --git a/src/doc/man/generated_txt/cargo-package.txt b/src/doc/man/generated_txt/cargo-package.txt
index 1cbee4807..f24d09b75 100644
--- a/src/doc/man/generated_txt/cargo-package.txt
+++ b/src/doc/man/generated_txt/cargo-package.txt
@@ -59,14 +59,6 @@ DESCRIPTION
        path_in_vcs will be set to a repo-relative path for packages in
        subdirectories of the version control repository.
 
-       The compatibility of this file is maintained under the same policy as
-       the JSON output of cargo-metadata(1).
-
-       Note that this file provides a best-effort snapshot of the VCS
-       information. However, the provenance of the package is not verified.
-       There is no guarantee that the source code in the tarball matches the
-       VCS information.
-
 OPTIONS
    Package Options
        -l, --list
diff --git a/src/doc/src/commands/cargo-package.md b/src/doc/src/commands/cargo-package.md
index 87f56f988..94fd89bd6 100644
--- a/src/doc/src/commands/cargo-package.md
+++ b/src/doc/src/commands/cargo-package.md
@@ -56,13 +56,6 @@ Will generate a `.cargo_vcs_info.json` in the following format
 `path_in_vcs` will be set to a repo-relative path for packages
 in subdirectories of the version control repository.
 
-The compatibility of this file is maintained under the same policy
-as the JSON output of [cargo-metadata(1)](cargo-metadata.html).
-
-Note that this file provides a best-effort snapshot of the VCS information.
-However, the provenance of the package is not verified.
-There is no guarantee that the source code in the tarball matches the VCS information.
-
 ## OPTIONS
 
 ### Package Options
diff --git a/src/etc/man/cargo-package.1 b/src/etc/man/cargo-package.1
index f515beaad..a6ab96cb6 100644
--- a/src/etc/man/cargo-package.1
+++ b/src/etc/man/cargo-package.1
@@ -83,13 +83,6 @@ Will generate a \fB\&.cargo_vcs_info.json\fR in the following format
 .sp
 \fBpath_in_vcs\fR will be set to a repo\-relative path for packages
 in subdirectories of the version control repository.
-.sp
-The compatibility of this file is maintained under the same policy
-as the JSON output of \fBcargo\-metadata\fR(1).
-.sp
-Note that this file provides a best\-effort snapshot of the VCS information.
-However, the provenance of the package is not verified.
-There is no guarantee that the source code in the tarball matches the VCS information.
 .SH "OPTIONS"
 .SS "Package Options"
 .sp


Test patch:
diff --git a/tests/testsuite/cargo_new/add_members_to_non_workspace/stderr.term.svg b/tests/testsuite/cargo_new/add_members_to_non_workspace/stderr.term.svg
index 395e46a58..c54470e14 100644
--- a/tests/testsuite/cargo_new/add_members_to_non_workspace/stderr.term.svg
+++ b/tests/testsuite/cargo_new/add_members_to_non_workspace/stderr.term.svg
@@ -1,4 +1,4 @@
-<svg width="970px" height="92px" xmlns="http://www.w3.org/2000/svg">
+<svg width="970px" height="74px" xmlns="http://www.w3.org/2000/svg">
   <style>
     .fg { fill: #AAAAAA }
     .bg { background: #000000 }
@@ -21,11 +21,9 @@
   <text xml:space="preserve" class="container fg">
     <tspan x="10px" y="28px"><tspan class="fg-green bold">    Creating</tspan><tspan> library `bar` package</tspan>
 </tspan>
-    <tspan x="10px" y="46px"><tspan class="fg-green bold">      Adding</tspan><tspan> `bar` as member of workspace at `[ROOT]/case`</tspan>
+    <tspan x="10px" y="46px"><tspan class="fg-cyan bold">note</tspan><tspan class="bold">:</tspan><tspan> see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html</tspan>
 </tspan>
-    <tspan x="10px" y="64px"><tspan class="fg-cyan bold">note</tspan><tspan class="bold">:</tspan><tspan> see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html</tspan>
-</tspan>
-    <tspan x="10px" y="82px">
+    <tspan x="10px" y="64px">
 </tspan>
   </text>