Improve error messages for `uv remove` (#9959)

Closes https://github.com/astral-sh/uv/issues/9958
This commit is contained in:
Zanie Blue 2024-12-17 08:16:01 -06:00 committed by GitHub
parent 686f383fa4
commit 2288905d46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 15 deletions

View File

@ -112,7 +112,7 @@ pub(crate) async fn remove(
if deps.is_empty() {
warn_if_present(&package, &toml);
anyhow::bail!(
"The dependency `{package}` could not be found in `dependencies`"
"The dependency `{package}` could not be found in `project.dependencies`"
);
}
}
@ -123,7 +123,7 @@ pub(crate) async fn remove(
if dev_deps.is_empty() && group_deps.is_empty() {
warn_if_present(&package, &toml);
anyhow::bail!(
"The dependency `{package}` could not be found in `dev-dependencies` or `dependency-groups.dev`"
"The dependency `{package}` could not be found in `tool.uv.dev-dependencies` or `tool.uv.dependency-groups.dev`"
);
}
}
@ -132,7 +132,7 @@ pub(crate) async fn remove(
if deps.is_empty() {
warn_if_present(&package, &toml);
anyhow::bail!(
"The dependency `{package}` could not be found in `optional-dependencies`"
"The dependency `{package}` could not be found in `project.optional-dependencies.{extra}`"
);
}
}
@ -144,7 +144,7 @@ pub(crate) async fn remove(
if dev_deps.is_empty() && group_deps.is_empty() {
warn_if_present(&package, &toml);
anyhow::bail!(
"The dependency `{package}` could not be found in `dev-dependencies` or `dependency-groups.dev`"
"The dependency `{package}` could not be found in `tool.uv.dev-dependencies` or `tool.uv.dependency-groups.dev`"
);
}
} else {
@ -152,7 +152,7 @@ pub(crate) async fn remove(
if deps.is_empty() {
warn_if_present(&package, &toml);
anyhow::bail!(
"The dependency `{package}` could not be found in `dependency-groups`"
"The dependency `{package}` could not be found in `dependency-groups.{group}`"
);
}
}
@ -323,16 +323,21 @@ fn warn_if_present(name: &PackageName, pyproject: &PyProjectTomlMut) {
warn_user!("`{name}` is a production dependency");
}
DependencyType::Dev => {
warn_user!("`{name}` is a development dependency; try calling `uv remove --dev`");
warn_user!(
"`{name}` is a development dependency (try: `{}`)",
format!("uv remove {name} --dev`").bold()
);
}
DependencyType::Optional(group) => {
warn_user!(
"`{name}` is an optional dependency; try calling `uv remove --optional {group}`",
"`{name}` is an optional dependency (try: `{}`)",
format!("uv remove {name} --optional {group}").bold()
);
}
DependencyType::Group(group) => {
warn_user!(
"`{name}` is in the `{group}` group; try calling `uv remove --group {group}`",
"`{name}` is in the `{group}` group (try: `{}`)",
format!("uv remove {name} --group {group}").bold()
);
}
}

View File

@ -1113,8 +1113,8 @@ fn add_remove_dev() -> Result<()> {
----- stdout -----
----- stderr -----
warning: `anyio` is in the `dev` group; try calling `uv remove --group dev`
error: The dependency `anyio` could not be found in `dependencies`
warning: `anyio` is in the `dev` group (try: `uv remove anyio --group dev`)
error: The dependency `anyio` could not be found in `project.dependencies`
"###);
// Remove the dependency.
@ -1336,8 +1336,8 @@ fn add_remove_optional() -> Result<()> {
----- stdout -----
----- stderr -----
warning: `anyio` is an optional dependency; try calling `uv remove --optional io`
error: The dependency `anyio` could not be found in `dependencies`
warning: `anyio` is an optional dependency (try: `uv remove anyio --optional io`)
error: The dependency `anyio` could not be found in `project.dependencies`
"###);
// Remove the dependency.
@ -4817,7 +4817,7 @@ fn remove_group() -> Result<()> {
----- stdout -----
----- stderr -----
error: The dependency `anyio` could not be found in `dependency-groups`
error: The dependency `anyio` could not be found in `dependency-groups.test`
"###);
let pyproject_toml = context.read("pyproject.toml");
@ -4845,7 +4845,7 @@ fn remove_group() -> Result<()> {
----- stdout -----
----- stderr -----
error: The dependency `anyio` could not be found in `dependency-groups`
error: The dependency `anyio` could not be found in `dependency-groups.test`
"###);
let pyproject_toml = context.temp_dir.child("pyproject.toml");
@ -4864,7 +4864,7 @@ fn remove_group() -> Result<()> {
----- stderr -----
warning: `anyio` is a production dependency
error: The dependency `anyio` could not be found in `dependency-groups`
error: The dependency `anyio` could not be found in `dependency-groups.test`
"###);
Ok(())