Skip to content

Add option to prefer locked versions of packages in build environments #14944

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

zanieb
Copy link
Member

@zanieb zanieb commented Jul 28, 2025

Adds a new build-dependency-strategy = latest | prefer-locked setting. latest is the current behavior. prefer-locked will prefer to use locked versions of runtime dependencies for build dependencies when they match. This is intended to work with #14735 to allow adding torch as an extra build dependency for flash-attn and prefer using the same version as you would at runtime (that's what flash-attn requires, but it's usually achieved by disabling build isolation).

The name is prefer-locked because I want to add a future option... like require-locked which will error if the versions matching the runtime versions cannot be used. The naming here is actually a little awkward, because it'll be confusing when we introduce locking of build dependencies separately from runtime requirements (which is on the roadmap). We might want to do... latest | try-match-runtime | match-runtime? I'm not sure. I also expect that we'll want a package-level variant of this setting, as match-runtime might be too aggressive for most packages. I also find this setting a little awkward alongside #14735, I wonder if we want a dedicated build dependencies section?

@zanieb zanieb temporarily deployed to uv-test-registries July 28, 2025 19:07 — with GitHub Actions Inactive
@zanieb zanieb force-pushed the zb/lock-extra-build-dependencies branch from afe9742 to 1480f81 Compare July 28, 2025 19:38
@zanieb zanieb temporarily deployed to uv-test-registries July 28, 2025 19:40 — with GitHub Actions Inactive
@zanieb zanieb changed the title Prefer locked versions of packages in build environments Add option to prefer locked versions of packages in build environments Jul 28, 2025
@zanieb zanieb added enhancement New feature or improvement to existing functionality preview Experimental behavior labels Jul 28, 2025
@zanieb zanieb temporarily deployed to uv-test-registries July 28, 2025 21:33 — with GitHub Actions Inactive
@zanieb zanieb temporarily deployed to uv-test-registries July 28, 2025 21:56 — with GitHub Actions Inactive
@zanieb zanieb requested review from konstin and charliermarsh July 29, 2025 02:20
///
/// When set to `prefer-locked`, uv will use the locked versions of packages specified in the
/// lockfile as preferences when resolving build dependencies during source builds. This helps
/// ensure that build environments are consistent with the project's resolved dependencies.
Copy link
Member

Choose a reason for hiding this comment

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

We can make this stronger by saying that the locked version will be used, if a single locked version exists in the lockfile, and if it doesn't cause any conflicts during resolution. (Though the latter isn't about the version being unfulfillable, but about whether it conflicts with higher priority packages in the resolution, which I hope is a detail users won't run into with build deps, and for which require-locked will help)

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to capture this a little in 9a91e91

@konstin
Copy link
Member

konstin commented Jul 29, 2025

I like the prefer-locked name, it captures what it does concisely.

@zanieb zanieb marked this pull request as ready for review July 29, 2025 18:13
@zanieb zanieb temporarily deployed to uv-test-registries July 29, 2025 18:15 — with GitHub Actions Inactive
@zanieb
Copy link
Member Author

zanieb commented Jul 29, 2025

@geofft has pointed out an important concern, that since these preferences are not taken into account in the wheel cache key, you won't automatically get a rebuild of a package when your locked version of a build dependency changes; or, worse, if you have another project with a different locked version, they'll share the same cached wheel :/

@zanieb zanieb temporarily deployed to uv-test-registries July 29, 2025 22:46 — with GitHub Actions Inactive
@zanieb zanieb marked this pull request as draft July 29, 2025 23:26
@zanieb
Copy link
Member Author

zanieb commented Jul 29, 2025

This is still "preferring" the locked version, but it sounds problematic. I'm not sure what the workaround is yet.

@konstin
Copy link
Member

konstin commented Jul 30, 2025

@geofft has pointed out an important concern, that since these preferences are not taken into account in the wheel cache key, you won't automatically get a rebuild of a package when your locked version of a build dependency changes; or, worse, if you have another project with a different locked version, they'll share the same cached wheel :/

This is the same logic we apply today without preferences: We build wheels once, and then cache them forever, even if the resolution of the package's build deps changes. For supporting caching with different version ranges, we need to support multiple builds with the same tag for a package in the cache, attach the build deps versions to it and select only fitting ones installation. A criteria could be whether the versions fit tool.uv.extra-build-dependencies.

@charliermarsh
Copy link
Member

This is the same logic we apply today without preferences: We build wheels once, and then cache them forever, even if the resolution of the package's build deps changes. For supporting caching with different version ranges, we need to support multiple builds with the same tag for a package in the cache, attach the build deps versions to it and select only fitting ones installation.

Yeah, I guess it doesn't seem that much worse than what we have today. Is it worth? We could look at caching separate builds based on build dependencies as a second step, right?

charliermarsh added a commit that referenced this pull request Aug 5, 2025
## Summary

This is an alternative to #14944
that functions a little differently. Rather than adding separate
strategies, you can instead say:

```toml
[tool.uv.extra-build-dependencies]
child = [{ requirement = "anyio", match-runtime = true }]
```

Which will then enforce that `anyio` uses the same version as in the
lockfile.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement to existing functionality preview Experimental behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants