Repository: pdm-project/pdm

Issue:
Inconsistent behavior between `pdm remove` and `pdm update`
## Is your feature/enhancement proposal related to a problem? Please describe.

I don't know if it's intentional or something that needs to be improved. When you delete a dev package, you will need to select the dependency group correctly:

```
$ pdm remove ruff
Removing packages from default dependencies: ruff
[ProjectError]: ruff does not exist in default dependencies.
```

You have to use `pdm remove --dev` or `pdm remove -dG` based on the specific group:

```
$ pdm remove --dev ruff
Removing packages from dev dev-dependencies: ruff
...
  ✔ Remove ruff 0.4.4 successful

🎉 All complete!
```

But the `pdm update` doesn't check the dependency group:

```
$ pdm update ruff
Updating packages: ruff.
🔒 Lock successful
All packages are synced to date, nothing to do.

🎉 All complete!
```

## Describe the solution you'd like

<!--A clear and concise description of what you ant to happen.-->

Make the behavior consistent between `pdm remove` and `pdm update` if possible.


Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pdm-project/pdm/pull/2886?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) Report
Attention: Patch coverage is `33.33333%` with `2 lines` in your changes are missing coverage. Please review.
> Project coverage is 85.31%. Comparing base [(`e0753f7`)](https://app.codecov.io/gh/pdm-project/pdm/commit/e0753f7c93969f3a87f94a66c417c9a37870db6e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) to head [(`8c1c49b`)](https://app.codecov.io/gh/pdm-project/pdm/pull/2886?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).
> Report is 1 commits behind head on main.

| [Files](https://app.codecov.io/gh/pdm-project/pdm/pull/2886?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) | Patch % | Lines |
|---|---|---|
| [src/pdm/cli/commands/update.py](https://app.codecov.io/gh/pdm-project/pdm/pull/2886?src=pr&el=tree&filepath=src%2Fpdm%2Fcli%2Fcommands%2Fupdate.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project#diff-c3JjL3BkbS9jbGkvY29tbWFuZHMvdXBkYXRlLnB5) | 33.33% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/pdm-project/pdm/pull/2886?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #2886      +/-   ##
==========================================
- Coverage   85.33%   85.31%   -0.02%     
==========================================
  Files         106      106              
  Lines       10587    10590       +3     
  Branches     2303     2305       +2     
==========================================
+ Hits         9034     9035       +1     
- Misses       1088     1089       +1     
- Partials      465      466       +1     
```

| [Flag](https://app.codecov.io/gh/pdm-project/pdm/pull/2886/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/pdm-project/pdm/pull/2886/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) | `85.09% <33.33%> (-0.02%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project#carryforward-flags-in-the-pull-request-comment) to find out more.


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pdm-project/pdm/pull/2886?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).


Patch:
diff --git a/news/2885.bugfix.md b/news/2885.bugfix.md
new file mode 100644
index 00000000..27ccb2b9
--- /dev/null
+++ b/news/2885.bugfix.md
@@ -0,0 +1,1 @@
+Raise an error if the package given by `pdm update` does not exist in the select dependency group but in other groups.
diff --git a/src/pdm/cli/commands/update.py b/src/pdm/cli/commands/update.py
index 1ccaeb05..c52a4fdb 100644
--- a/src/pdm/cli/commands/update.py
+++ b/src/pdm/cli/commands/update.py
@@ -149,6 +149,14 @@ class Command(BaseCommand):
                             f"[req]{name}[/] does not exist in [primary]{group}[/] "
                             f"{'dev-' if selection.dev else ''}dependencies nor is a transitive dependency."
                         )
+                    look_in_other_group = next(
+                        (g for g, deps in all_dependencies.items() if normalized_name in deps and g != group), None
+                    )
+                    if look_in_other_group is not None:
+                        raise ProjectError(
+                            f"[req]{name}[/] does not exist in [primary]{group}[/], but exists in [primary]{look_in_other_group}[/]."
+                            " Please specify the correct group with `-G/--group`."
+                        )
                     tracked_names.add(normalized_name)
                 else:
                     matched_req.prerelease = prerelease


Test patch:
diff --git a/tests/cli/test_update.py b/tests/cli/test_update.py
index 1b111aa9..0a5dbfa5 100644
--- a/tests/cli/test_update.py
+++ b/tests/cli/test_update.py
@@ -184,13 +184,21 @@ def test_update_transitive(project, repository, pdm):
 
 
 @pytest.mark.usefixtures("working_set")
-def test_update_transitive_nonexistant_dependencies(project, repository, pdm):
+def test_update_transitive_nonexistant_dependencies(project, pdm):
     pdm(["add", "requests", "--no-sync"], obj=project, strict=True)
-    result = pdm(["update", "numpy"])
+    result = pdm(["update", "numpy"], obj=project)
     assert "ProjectError" in result.stderr
     assert "numpy does not exist in" in result.stderr
 
 
+@pytest.mark.usefixtures("working_set")
+def test_update_package_wrong_group(project, pdm):
+    pdm(["add", "-d", "requests"], obj=project, strict=True)
+    result = pdm(["update", "requests"], obj=project)
+    assert "ProjectError" in result.stderr
+    assert "requests does not exist in default, but exists in dev" in result.stderr
+
+
 @pytest.mark.usefixtures("working_set")
 def test_update_transitive_non_transitive_dependencies(project, repository, pdm):
     pdm(["add", "requests", "pytz", "--no-sync"], obj=project, strict=True)