Skip to content
Draft
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
45 changes: 44 additions & 1 deletion crates/uv-workspace/src/pyproject_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use itertools::Itertools;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use toml_edit::{
Array, ArrayOfTables, DocumentMut, Formatted, Item, RawString, Table, TomlError, Value,
Array, ArrayOfTables, DocumentMut, Formatted, InlineTable, Item, RawString, Table, TomlError,
Value,
};

use uv_cache_key::CanonicalUrl;
Expand Down Expand Up @@ -496,6 +497,11 @@ impl PyProjectTomlMut {
.cloned()
.unwrap_or_default();

let previous_name = table
.get("name")
.and_then(Item::as_str)
.map(ToString::to_string);

// If necessary, update the name.
if let Some(index) = index.name.as_deref()
&& table
Expand Down Expand Up @@ -636,6 +642,30 @@ impl PyProjectTomlMut {
// Push the item to the table.
existing.push(table);

// Keep source references valid when an equivalent index is renamed.
if let Some(previous_name) = previous_name
&& let Some(name) = index.name.as_deref()
&& previous_name != name
&& let Some(sources) = self
.doc
.get_mut("tool")
.and_then(Item::as_table_mut)
.and_then(|tool| tool.get_mut("uv"))
.and_then(Item::as_table_mut)
.and_then(|uv| uv.get_mut("sources"))
.and_then(Item::as_table_mut)
{
for (_, source) in sources.iter_mut() {
if let Some(source) = source.as_inline_table_mut() {
rename_index_source(source, &previous_name, name);
} else if let Some(source) = source.as_array_mut() {
for source in source.iter_mut().filter_map(Value::as_inline_table_mut) {
rename_index_source(source, &previous_name, name);
}
}
}
}

Ok(())
}

Expand Down Expand Up @@ -1706,6 +1736,19 @@ fn find_source(name: &PackageName, sources: &Table) -> Option<String> {
None
}

fn rename_index_source(source: &mut InlineTable, previous_name: &str, name: &str) {
let Some(index) = source.get_mut("index") else {
return;
};
if index.as_str() != Some(previous_name) {
return;
}

let decor = index.decor().clone();
*index = Value::from(name);
*index.decor_mut() = decor;
}

// Add a source to `tool.uv.sources`.
fn add_source(req: &PackageName, source: &Source, sources: &mut Table) -> Result<(), Error> {
// Serialize as an inline table.
Expand Down
68 changes: 68 additions & 0 deletions crates/uv/tests/project/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11745,6 +11745,74 @@ fn add_index_comments() -> Result<()> {
Ok(())
}

/// Renaming an equivalent index must update every existing source reference.
#[test]
fn add_index_renames_existing_sources() -> Result<()> {
let context = uv_test::test_context!("3.12");
let server = uv_test::packse::PackseServer::new("simple/dependency-groups.toml");
let index_url = server.index_url();

context
.temp_dir
.child("pyproject.toml")
.write_str(&formatdoc! {r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["sniffio==1.3.1"]

[[tool.uv.index]]
name = "old"
url = "{index_url}"
explicit = true

[tool.uv.sources]
sniffio = [{{ index = "old", marker = "sys_platform == 'linux'" }}, {{ index = "old", marker = "sys_platform != 'linux'" }}]
"#})?;

uv_snapshot!(context.filters(), context.add().arg("iniconfig==2.0.0").arg("--index").arg(format!("new={}", index_url.trim_end_matches('/'))).arg("--frozen"), @"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
");

insta::with_settings!({filters => context.filters()}, {
assert_snapshot!(context.read("pyproject.toml"), @r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"iniconfig==2.0.0",
"sniffio==1.3.1",
]

[[tool.uv.index]]
name = "new"
url = "http://[LOCALHOST]/simple"
explicit = true

[tool.uv.sources]
sniffio = [{ index = "new", marker = "sys_platform == 'linux'" }, { index = "new", marker = "sys_platform != 'linux'" }]
iniconfig = { index = "new" }
"#);
});

uv_snapshot!(context.filters(), context.lock(), @"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 3 packages in [TIME]
");

Ok(())
}

/// Accidentally add a dependency on the project itself.
#[test]
fn add_self() -> Result<()> {
Expand Down
Loading