Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid updating incorrect dependencies for sorted uv add #6939

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/uv-workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ impl Source {
}

/// The type of a dependency in a `pyproject.toml`.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DependencyType {
/// A dependency in `project.dependencies`.
Production,
Expand Down
8 changes: 8 additions & 0 deletions crates/uv-workspace/src/pyproject_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ pub enum ArrayEdit {
Add(usize),
}

impl ArrayEdit {
pub fn index(&self) -> usize {
match self {
Self::Update(i) | Self::Add(i) => *i,
}
}
}

/// Specifies whether dependencies are added to a script file or a `pyproject.toml` file.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DependencyTarget {
Expand Down
22 changes: 21 additions & 1 deletion crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ pub(crate) async fn add(
DependencyTarget::PyProjectToml,
),
}?;
let mut edits = Vec::with_capacity(requirements.len());
let mut edits = Vec::<DependencyEdit>::with_capacity(requirements.len());
for mut requirement in requirements {
// Add the specified extras.
requirement.extras.extend(extras.iter().cloned());
Expand Down Expand Up @@ -407,6 +407,26 @@ 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;
}
}
ArrayEdit::Update(existing) => {
if *existing >= index {
*existing += 1;
}
}
}
}
}

edits.push(DependencyEdit {
dependency_type: &dependency_type,
requirement,
Expand Down
34 changes: 22 additions & 12 deletions crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4411,8 +4411,8 @@ fn fail_to_add_revert_project() -> Result<()> {
Ok(())
}

/// Ensure that the added dependencies are sorted
/// if the dependency list was already sorted prior to adding the new one.
/// Ensure that the added dependencies are sorted if the dependency list was already sorted prior
/// to the operation.
#[test]
fn sorted_dependencies() -> Result<()> {
let context = TestContext::new("3.12");
Expand All @@ -4426,19 +4426,31 @@ fn sorted_dependencies() -> Result<()> {
requires-python = ">=3.12"
dependencies = [
"CacheControl[filecache]>=0.14,<0.15",
"mwparserfromhell",
"pywikibot",
"sentry-sdk",
"yarl",
"iniconfig",
]
"#})?;

uv_snapshot!(context.filters(), context.add(&["pydantic"]).arg("--frozen"), @r###"
uv_snapshot!(context.filters(), context.add(&["typing-extensions", "anyio"]), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 13 packages in [TIME]
Prepared 12 packages in [TIME]
Installed 12 packages in [TIME]
+ anyio==4.3.0
+ cachecontrol==0.14.0
+ certifi==2024.2.2
+ charset-normalizer==3.3.2
+ filelock==3.13.1
+ idna==3.6
+ iniconfig==2.0.0
+ msgpack==1.0.8
+ requests==2.31.0
+ sniffio==1.3.1
+ typing-extensions==4.10.0
+ urllib3==2.2.1
"###);

let pyproject_toml = fs_err::read_to_string(context.temp_dir.join("pyproject.toml"))?;
Expand All @@ -4455,11 +4467,9 @@ fn sorted_dependencies() -> Result<()> {
requires-python = ">=3.12"
dependencies = [
"CacheControl[filecache]>=0.14,<0.15",
"mwparserfromhell",
"pydantic",
"pywikibot",
"sentry-sdk",
"yarl",
"anyio>=4.3.0",
"iniconfig",
"typing-extensions>=4.10.0",
]
"###
);
Expand Down
Loading