Show ambiguous requirements when `uv add` failed (#12106)

Improves the error message.

Fixes #12105

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
This commit is contained in:
konsti 2025-03-11 04:00:09 +01:00 committed by GitHub
parent e843433b07
commit 82212bb439
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 3 deletions

View File

@ -44,8 +44,11 @@ pub enum Error {
MalformedWorkspace,
#[error("Expected a dependency at index {0}")]
MissingDependency(usize),
#[error("Cannot perform ambiguous update; found multiple entries with matching package names")]
Ambiguous,
#[error("Cannot perform ambiguous update; found multiple entries for `{}`:\n{}", package_name, requirements.iter().map(|requirement| format!("- `{requirement}`")).join("\n"))]
Ambiguous {
package_name: PackageName,
requirements: Vec<Requirement>,
},
}
/// The result of editing an array in a TOML document.
@ -1124,7 +1127,13 @@ pub fn add_dependency(
Ok(ArrayEdit::Update(i))
}
// Cannot perform ambiguous updates.
_ => Err(Error::Ambiguous),
_ => Err(Error::Ambiguous {
package_name: req.name.clone(),
requirements: to_replace
.into_iter()
.map(|(_, requirement)| requirement)
.collect(),
}),
}
}

View File

@ -10389,3 +10389,50 @@ fn add_auth_policy_never_without_credentials() -> Result<()> {
context.assert_command("import anyio").success();
Ok(())
}
/// Test the error message when adding a package with multiple existing references in
/// `pyproject.toml`.
#[test]
fn add_ambiguous() -> Result<()> {
let context = TestContext::new("3.12");
context
.temp_dir
.child("pyproject.toml")
.write_str(indoc! {r#"
[project]
name = "foo"
version = "0.1.0"
requires-python = ">=3.12.0"
dependencies = [
"anyio>=4.0.0",
"anyio>=4.1.0",
]
[dependency-groups]
bar = ["anyio>=4.1.0", "anyio>=4.2.0"]
"#})?;
uv_snapshot!(context.filters(), context.add().arg("anyio"), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
error: Cannot perform ambiguous update; found multiple entries for `anyio`:
- `anyio>=4.0.0`
- `anyio>=4.1.0`
"###);
uv_snapshot!(context.filters(), context.add().arg("--group").arg("bar").arg("anyio"), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
error: Cannot perform ambiguous update; found multiple entries for `anyio`:
- `anyio>=4.1.0`
- `anyio>=4.2.0`
"###);
Ok(())
}