Skip to content

Commit

Permalink
Avoid updating pyproject.toml offsets on non-add edits (#7262)
Browse files Browse the repository at this point in the history
## Summary

Closes #7259.
  • Loading branch information
charliermarsh authored Sep 10, 2024
1 parent bbccee8 commit cfa9299
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
25 changes: 12 additions & 13 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4539,3 +4539,53 @@ fn custom_dependencies() -> Result<()> {
});
Ok(())
}

/// Regression test for: <https://github.com/astral-sh/uv/issues/7259>
#[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(())
}

0 comments on commit cfa9299

Please sign in to comment.