From a0e8d3e4bc5f751a02526ab0dfc28d361b62e2e4 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 10 Sep 2024 13:22:00 -0400 Subject: [PATCH] Avoid erroneous updates --- crates/uv/src/commands/project/add.rs | 25 +++++++------- crates/uv/tests/edit.rs | 50 +++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index c841b8973b0c..84ed9b4fd762 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -411,21 +411,20 @@ pub(crate) async fn add( } }; - // Keep track of the exact location of the edit. - let index = edit.index(); - // If the edit was inserted before the end of the list, update the existing edits. - for edit in &mut edits { - if *edit.dependency_type == dependency_type { - match &mut edit.edit { - ArrayEdit::Add(existing) => { - if *existing >= index { - *existing += 1; + if let ArrayEdit::Add(index) = &edit { + for edit in &mut edits { + if *edit.dependency_type == dependency_type { + match &mut edit.edit { + ArrayEdit::Add(existing) => { + if *existing >= *index { + *existing += 1; + } } - } - ArrayEdit::Update(existing) => { - if *existing >= index { - *existing += 1; + ArrayEdit::Update(existing) => { + if *existing >= *index { + *existing += 1; + } } } } diff --git a/crates/uv/tests/edit.rs b/crates/uv/tests/edit.rs index 44fe9c524f9a..9de2f1783189 100644 --- a/crates/uv/tests/edit.rs +++ b/crates/uv/tests/edit.rs @@ -4539,3 +4539,53 @@ fn custom_dependencies() -> Result<()> { }); Ok(()) } + +/// Regression test for: +#[test] +fn update_offset() -> Result<()> { + let context = TestContext::new("3.12"); + + let pyproject_toml = context.temp_dir.child("pyproject.toml"); + pyproject_toml.write_str(indoc! {r#" + [project] + name = "project" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = [ + "iniconfig", + ] + "#})?; + + uv_snapshot!(context.filters(), context.add().args(["typing-extensions", "iniconfig"]), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 3 packages in [TIME] + Prepared 2 packages in [TIME] + Installed 2 packages in [TIME] + + iniconfig==2.0.0 + + typing-extensions==4.10.0 + "###); + + let pyproject_toml = fs_err::read_to_string(context.temp_dir.join("pyproject.toml"))?; + + insta::with_settings!({ + filters => context.filters(), + }, { + assert_snapshot!( + pyproject_toml, @r###" + [project] + name = "project" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = [ + "iniconfig", + "typing-extensions>=4.10.0", + ] + "### + ); + }); + Ok(()) +}