Skip to content

Commit d9907f6

Browse files
authored
Update resolver internals docs (#11098)
Since the resolver internals docs were written, we added a lot more features to the resolver, which should be documented. As usual, these docs are not targeted at regular users, but should give interested readers an insight into the internals of uv and help advanced users with especially hard resolver problems.
1 parent 1d9db68 commit d9907f6

File tree

1 file changed

+90
-14
lines changed

1 file changed

+90
-14
lines changed

docs/reference/resolver-internals.md

+90-14
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ there are no general, fast algorithms. In practice, this is misleading for a num
2525
- When resolution fails, more information is needed than a message that there is no solution (as is
2626
seen in SAT solvers). Instead, the resolver should produce an understandable error trace that
2727
states which packages are involved in away to allows a user to remove the conflict.
28+
- The most important heuristic for performance and user experience is determining the order in which
29+
decisions are made through prioritization.
2830

2931
uv uses [pubgrub-rs](https://github.com/pubgrub-rs/pubgrub), the Rust implementation of
3032
[PubGrub](https://nex3.medium.com/pubgrub-2fb6470504f), an incremental version solver. PubGrub in uv
3133
works in the following steps:
3234

3335
- Start with a partial solution that declares which packages versions have been selected and which
3436
are undecided. Initially, only a virtual root package is decided.
35-
- The highest priority package is selected from the undecided packages. Package with URLs (including
36-
file, git, etc.) have the highest priority, then those with more exact specifiers (such as `==`),
37-
then those with less strict specifiers. Inside each category, packages are ordered by when they
38-
were first seen (i.e. order in a file), making the resolution deterministic.
37+
- The highest priority package is selected from the undecided packages. Roughly, packages with URLs
38+
(including file, git, etc.) have the highest priority, then those with more exact specifiers (such
39+
as `==`), then those with less strict specifiers. Inside each category, packages are ordered by
40+
when they were first seen (i.e. order in a file), making the resolution deterministic.
3941
- A version is picked for the selected package. The version must works with all specifiers from the
4042
requirements in the partial solution and must not be previously marked as incompatible. The
4143
resolver prefers versions from a lockfile (`uv.lock` or `-o requirements.txt`) and those installed
@@ -115,16 +117,6 @@ between forks is written to the lockfile. When performing a new resolution, the
115117
lockfile are used to ensure the resolution is stable. When requirements change, new forks may be
116118
added to the saved forks.
117119

118-
## Requires-python
119-
120-
To ensure that a resolution with `requires-python = ">=3.9"` can actually be installed for the
121-
included Python versions, uv requires that all dependencies have the same minimum Python version.
122-
Package versions that declare a higher minimum Python version, e.g., `requires-python = ">=3.10"`,
123-
are rejected, because a resolution with that version can't be installed on Python 3.9. For
124-
simplicity and forward compatibility, only lower bounds in `requires-python` are respected. For
125-
example, if a package declares `requires-python = ">=3.8,<4"`, the `<4` marker is not propagated to
126-
the entire resolution.
127-
128120
## Wheel tags
129121

130122
While uv's resolution is universal with respect to environment markers, this doesn't extend to wheel
@@ -136,3 +128,87 @@ system and architecture. Most projects have a universally compatible source dist
136128
used when attempted to install a package that has no compatible wheel, but some packages, such as
137129
`torch`, don't publish a source distribution. In this case an installation on, e.g., Python 3.13, an
138130
uncommon operating system, or architecture, will fail and complain that there is no matching wheel.
131+
132+
## Marker and wheel tag filtering
133+
134+
In every fork, we know what markers are possible. In non-universal resolution, we know their exact
135+
values. In universal mode, we know at least a constraint for the python requirement, e.g.,
136+
`requires-python = ">=3.12"` means that `importlib_metadata; python_version < "3.10"` can be
137+
discarded because it can never be installed. If additionally `tool.uv.environments` is set, we can
138+
filter out requirements with markers disjoint with those environments. Inside each fork, we can
139+
additionally filter by the fork markers.
140+
141+
There is some redundancy in the marker expressions, where the value of one marker field implies the
142+
value of another field. Internally, we normalize `python_version` and `python_full_version` as well
143+
as known values of `platform_system` and `sys_platform` to a shared canonical representation, so
144+
they can match against each other.
145+
146+
When we selected a version with a local tag (e.g.,`1.2.3+localtag`) and the wheels don't cover
147+
support for Windows, Linux and macOS, and there is a base version without tag (e.g.,`1.2.3`) with
148+
support for a missing platform, we fork trying to extend the platform support by using both the
149+
version with local tag and without local tag depending on the platform. This helps with packages
150+
that use the local tag for different hardware accelerators such as torch. While there is no 1:1
151+
mapping between wheel tags and markers, we can do a mapping for well-known platforms, including
152+
Windows, Linux and macOS.
153+
154+
## Requires-python
155+
156+
To ensure that a resolution with `requires-python = ">=3.9"` can actually be installed for the
157+
included Python versions, uv requires that all dependencies have the same minimum Python version.
158+
Package versions that declare a higher minimum Python version, e.g., `requires-python = ">=3.10"`,
159+
are rejected, because a resolution with that version can't be installed on Python 3.9. For
160+
simplicity and forward compatibility, only lower bounds in `requires-python` are respected. For
161+
example, if a package declares `requires-python = ">=3.8,<4"`, the `<4` marker is not propagated to
162+
the entire resolution.
163+
164+
This default is a problem for packages that use the version-dependent C API of CPython, such as
165+
numpy. Each numpy release support 4 Python minor versions, e.g., numpy 2.0.0 has wheels for CPython
166+
3.9 through 3.12 and declares `requires-python = ">=3.9"`, while numpy 2.1.0 has wheels for CPython
167+
3.10 through 3.13 and declares `requires-python = ">=3.10"`. The means that when we resolve a
168+
`numpy>=2,<3` requirement in a project with `requires-python = ">=3.9"`, we resolve numpy 2.0.0 and
169+
the lockfile doesn't install on Python 3.13 or newer. To alleviate this, whenever we reject a
170+
version due to a too high Python requirement, we fork on that Python version. This behavior is
171+
controlled by `--fork-strategy`. In the example case, upon encountering numpy 2.1.0 we fork into
172+
Python versions `>=3.9,<3.10` and `>=3.10` and resolve two different numpy versions:
173+
174+
```
175+
numpy==2.0.0; python_version >= "3.9" and python_version < "3.10"
176+
numpy==2.1.0; python_version >= "3.10"
177+
```
178+
179+
## Prioritization
180+
181+
Prioritization is important for both performance and for better resolutions.
182+
183+
If we try many versions we have to later discard, resolution is slow, both because we have to read
184+
metadata we didn't need and because we have to track a lot of (conflict) information for this
185+
discarded subtree.
186+
187+
There are expectations about which solution uv should choose, even if the version constraints allow
188+
multiple solutions. Generally, a desirable solution prioritizes use the highest versions for direct
189+
dependencies over those for indirect dependencies, it avoids backtracking to very old versions and
190+
can be installed on a target machine.
191+
192+
Internally, uv represent each package with a given package name as a number of virtual packages, for
193+
example, one package for each activated extra, for dependency groups, or for having a marker. While
194+
PubGrub needs to choose a version for each virtual package, uv's prioritization works on the package
195+
name level.
196+
197+
Whenever we encounter a requirement on a package, we match it to a priority. The root package and
198+
URL requirements have the highest priority, then singleton requirements with the `==` operator, as
199+
their version can be directly determined, then highly conflicting packages (next paragraph), and
200+
finally all other packages. Inside each category, packages are sorted by when they were first
201+
encountered, creating a breadth first search that prioritizes direct dependencies including
202+
workspace dependencies over transitive dependencies.
203+
204+
A common problem is that we have a package A with a higher priority than package B, and B is only
205+
compatible with older versions of A. We decide the latest version for package A. Each time we decide
206+
a version for B, it is immediately discarded due to the conflict with A. We have to try all possible
207+
versions of B, until we have either exhausted the possible range (slow), pick a very old version
208+
that doesn't depend on A, but most likely isn't compatible with the project either (bad) or fail to
209+
build a very old version (bad). Once we see such conflict happen five time, we set A and B to
210+
special highly-conflicting priority levels, and set them so that B is decided before A. We then
211+
manually backtrack to a state before deciding A, in the next iteration now deciding B instead of A.
212+
See [#8157](https://github.com/astral-sh/uv/issues/8157) and
213+
[#9843](https://github.com/astral-sh/uv/pull/9843) for a more detailed description with real world
214+
examples.

0 commit comments

Comments
 (0)