Skip to content

Only remove orphaned dependencies on uv remove - #20192

Draft
zanieb wants to merge 1 commit into
mainfrom
zb/remove-orphaned-dependencies
Draft

zanieb wants to merge 1 commit into
mainfrom
zb/remove-orphaned-dependencies

Conversation

@zanieb

@zanieb zanieb commented Jul 7, 2026 •

Copy link
Copy Markdown
Member

uv remove performs an exact sync, so removing a dependency can also uninstall unrelated packages from previously selected groups or extras.

Limit removal to the removed dependencies and their transitive dependencies that are no longer required by the project or another installed package. Determine removal candidates from the previous lockfile and installed metadata, while retaining packages needed by any remaining project group or extra.

Closes #9012
Closes #12827

@zanieb zanieb changed the title Prune orphaned dependencies on uv remove Prune orphaned dependencies on uv remove Jul 7, 2026
@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from e8621a2 to 355b08d Compare July 7, 2026 16:47
@zanieb zanieb changed the title Prune orphaned dependencies on uv remove Only remove orphaned dependencies on uv remove Jul 7, 2026
@astral-sh-bot

This comment has been minimized.

@charliermarsh

Copy link
Copy Markdown
Member

I found this sort of helpful as the calculation of "which candidates to uninstall":

candidates = packages reachable from the removed package in the old lock
candidates += packages reachable from it in the installed environment
candidates -= packages still reachable from remaining project declarations
candidates -= packages required by unrelated installed packages

@charliermarsh

Copy link
Copy Markdown
Member

I think the idea here is reasonable although there are still cases that it would get "wrong" in practice, e.g.:

  • User installs a package B out-of-band that A depends on; we remove A and it also removes B, because we didn't know that it was explicitly installed.
  • ...

These are, of course, rare, especially in projects.

I do wonder if the complexity is worth it as compared to just disabling the sync step in uv remove.

@zanieb

zanieb commented Jul 7, 2026

Copy link
Copy Markdown
Member Author

I think it's valuable that uv remove removes the target package from the environment.

User installs a package B out-of-band that A depends on; we remove A and it also removes B, because we didn't know that it was explicitly installed.

I think this is an acceptable trade-off for me. Though we could record if a package was installed via a sync to track and account for this. That might be desirable metadata anyway?

@zanieb

zanieb commented Jul 7, 2026

Copy link
Copy Markdown
Member Author

Oh I think we can make that work if we fix #13278

Comment thread crates/uv-cli/src/lib.rs Resolved
@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from a757fb1 to 2b65306 Compare July 9, 2026 14:10
Comment on lines +1063 to +1081
fn remove_prune_graph() {
let context = uv_test::test_context!("3.12");
let server = PackseServer::new("extras/remove-prune-extra.toml");

uv_snapshot!(context.filters(), command(&context, &server)
, @"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
error: the following required arguments were not provided:
<PACKAGE|--requirements <REQUIREMENTS>|--editable <EDITABLE>|--group <GROUP>>

Usage: uv pip install --cache-dir [CACHE_DIR] --index-url <INDEX_URL> <PACKAGE|--requirements <REQUIREMENTS>|--editable <EDITABLE>|--group <GROUP>>

For more information, try '--help'.
");
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clearly problematic?

I think we might need a precursor pull request that let's us mark a scenario as "no autogen test"

@zsol

zsol commented Jul 10, 2026 •

Copy link
Copy Markdown
Member

There's some quadratic behavior here that the security scanner found, but I don't think it's an actual security issue - worth fixing though:

The new reachability walk deduplicates states only after dequeue, then scans the full Requires-Dist vector and appends dependency extras for every first-seen state. A publisher can therefore produce quadratic work and duplicate queue growth from valid installed metadata. Ireproduced this at 221e529a: a 60,730-byte inert wheel took 20.940s to remove, while the exact base removed the same metadata in 0.138s; doubling extras from 4,096 to 8,192 increased head CPU by 3.98x.

Edit: oh I realize #20289 is about addressing this, but I don't think it does so fully

@zanieb

zanieb commented Jul 10, 2026

Copy link
Copy Markdown
Member Author

Oh #20289 wasn't an attempt to resolve performance issues, it was just a sketch of combining implementations. I can look into both though.

@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from 221e529 to a404861 Compare July 16, 2026 12:47
@codspeed

codspeed Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 25 untouched benchmarks
⏩ 12 skipped benchmarks1


Comparing zb/remove-orphaned-dependencies (bc90934) with zb/dependency-traversal-adoption (97edf0c)

Open in CodSpeed

Footnotes

  1. 12 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment thread crates/uv/tests/project/edit.rs Outdated
Comment on lines +4610 to +4611
let mut filters = context.filters();
filters.push((r"(?m)^WARN Range requests not supported[^\n]*\n", ""));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also seems like it needs a precursor pull. We should fix this in the packse server?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A downside of this approach is we don't get the nice packse rendering of the dependency tree which is generally really helpful for understanding what's going on. Maybe we can render it in markdown and reference it? This should be a separate pull stacked on the above but not a fresh base for the uv remove changes.

@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from 31f41ba to beef924 Compare July 16, 2026 13:52
@zanieb
zanieb changed the base branch from main to zb/scenario-no-autogen July 16, 2026 13:52
@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from beef924 to 363cc61 Compare July 16, 2026 14:06
@zanieb
zanieb changed the base branch from zb/scenario-no-autogen to zb/packse-range-requests July 16, 2026 14:08
@zanieb
zanieb force-pushed the zb/packse-range-requests branch from 7ddf72e to 3477a91 Compare July 16, 2026 14:31
@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from 363cc61 to c352d06 Compare July 16, 2026 14:31
Base automatically changed from zb/packse-range-requests to zb/scenario-no-autogen July 16, 2026 14:34
@zanieb
zanieb force-pushed the zb/scenario-no-autogen branch from 56d4166 to d3fad3f Compare July 16, 2026 14:39
@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from c352d06 to 68daee0 Compare July 16, 2026 14:39
@zanieb
zanieb force-pushed the zb/scenario-no-autogen branch from d3fad3f to 5c3b43f Compare July 17, 2026 15:32
Base automatically changed from zb/scenario-no-autogen to main July 17, 2026 15:38
@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from 68daee0 to ac76f18 Compare August 29, 2026 14:16
@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from 5ff5ea4 to 55ca934 Compare August 31, 2026 18:30
@zanieb
zanieb changed the base branch from main to zb/dependency-traversal-adoption August 31, 2026 18:31
@zanieb
zanieb force-pushed the zb/dependency-traversal-adoption branch from 589134a to 97edf0c Compare August 31, 2026 19:06
@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from 55ca934 to 77464f5 Compare August 31, 2026 19:06
@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from 77464f5 to bc90934 Compare August 31, 2026 19:12
@zanieb
zanieb force-pushed the zb/remove-orphaned-dependencies branch from bc90934 to 0db46ce Compare October 8, 2026 03:21
@zanieb
zanieb changed the base branch from zb/dependency-traversal-adoption to main October 8, 2026 03:21

This branch was previously deployed

1 inactive (outdated) deployment
automations — bc909347 Deployed Aug 31, 2026 by zanieb via review / security review #45570
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 remove should support inexact uv remove --group any_group any_package. All deps are removed.

4 participants