Skip to content

Sync index format when uv add --index updates an existing index URL#19818

Open
BitWeaverDev wants to merge 1 commit into
astral-sh:mainfrom
BitWeaverDev:fix-add-index-format-overwrite
Open

Sync index format when uv add --index updates an existing index URL#19818
BitWeaverDev wants to merge 1 commit into
astral-sh:mainfrom
BitWeaverDev:fix-add-index-format-overwrite

Conversation

@BitWeaverDev

Copy link
Copy Markdown

Summary

uv add --index <name>=<url> matches an existing index by name and overwrites its url, but it previously left the format key in place. An index declared as format = "flat" keeps that format even after being repointed at a Simple-API URL, silently producing an inconsistent configuration.

The add itself succeeds (the CLI-supplied index carries the correct Simple format), so the breakage only surfaces later — a subsequent uv lock reads the stale format = "flat" and fails to resolve.

Reproduction from #19759:

$ uv init --bare --name test
$ cat >> pyproject.toml <<EOF
[[tool.uv.index]]
name = "index"
url = "https://example.com/flat"
format = "flat"
EOF
$ uv add iniconfig --index index=https://pypi.org/simple
$ grep -A3 'tool.uv.index' pyproject.toml
[[tool.uv.index]]
name = "index"
url = "https://pypi.org/simple"
format = "flat"   # <-- stale, breaks `uv lock`

Changes

In PyProjectTomlMut::add_index, capture whether the URL is actually changing. When it is, sync the format to match the incoming index:

  • write format = "flat" for flat indexes, and
  • drop the format key entirely for Simple indexes (the default).

When the URL is unchanged, the existing format is left untouched, so entries that aren't being repointed keep their configuration.

Test

Two unit tests in pyproject_mut.rs, matching the module's existing test style:

  • add_index_clears_format_on_url_update — repointing a flat index at a Simple URL removes the stale format key.
  • add_index_preserves_format_when_url_unchanged — re-adding the same URL leaves format = "flat" intact.

Closes #19759


I used AI as a coding assistant for this contribution, in accordance with the project's AI Policy.

When `uv add --index <name>=<url>` matches an existing index by name, it
overwrites the `url` but previously left the `format` key untouched. An
index declared with `format = "flat"` would keep that format after being
repointed at a Simple-API URL, silently producing an inconsistent
configuration that breaks a subsequent `uv lock`.

Track whether the URL actually changes and, when it does, sync the
`format` to match the incoming index: write `format = "flat"` for flat
indexes, and drop the key entirely for Simple indexes (the default). The
format is left alone when the URL is unchanged, so existing entries keep
their configuration.

Closes astral-sh#19759
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

uv add --index <name>=<url> <package> can overwrite an index of the wrong format

1 participant