uv: Lock files don't mesh with dynamic package version info
Since tox-uv just shipped lock files, I took a stab at moving attrs to a fully-locked dev environment.
Here’s the experiment PR: https://github.com/python-attrs/attrs/pull/1349
A problem I’ve run into is that packages often use dynamic packaging data that is based on git metadata (setuptools-scm, hatch-vcs, etc). As such, attrs’s package version changes after every commit, which allows us to upload to test PyPI and continually verify our package. See, for example, https://test.pypi.org/project/attrs/24.2.1.dev19/
Unfortunately the current project gets locked like this:
[[package]]
name = "attrs"
version = "24.2.1.dev20"
source = { editable = "." }
Which means it’s outdated after each commit, because every commit increments the number behind dev.
Is there a way around this that I’ve missed? If not, could there be built one? 😇 AFAICT, this is currently the biggest blocker for FOSS use from uv’s side. I’m also not sure what the point of that version lock is in the first place for the current, editable project? But that’s a topic for another day. 😃
❯ uv --version
uv 0.4.12 (2545bca69 2024-09-18)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 44
- Comments: 65 (32 by maintainers)
Commits related to this issue
- CI: fail CI if lockfile is out of sync with project (#3639) We also want to automate the updating of the lockfile, but I think this also helps make sure that they're always in sync before merging, i... — committed to xdslproject/xdsl by superlopuh 2 years ago
- re-lock `env SETUPTOOLS_SCM_PRETEND_VERSION=99.0 uv lock --upgrade` as per https://github.com/astral-sh/uv/issues/7533#issuecomment-2578130122 — committed to python-attrs/attrs by hynek 2 years ago
- Omit dynamic versions from the lockfile (#10622) ## Summary This PR modifies the lockfile to omit versions for source trees that use `dynamic` versioning, thereby enabling projects to use dynamic... — committed to my1e5/uv by charliermarsh 2 years ago
- chore: remove version for cubi-tk in uv.lock (cf https://github.com/astral-sh/uv/issues/7533) — committed to bihealth/cubi-tk by tedil 2 years ago
I’m working on omitting dynamic versions from the lockfile: https://github.com/astral-sh/uv/pull/10622
I think the solution here will ultimately be to stop storing versions for source trees (either entirely, or when dynamic – I prefer doing so entirely, and not based on whether it’s dynamic). I just want PEP 751 to play out a little more before I make any change.
This problem is not only for dynamic versioning. We have a release-please workflow, which will create a PR for the next release. This will update the pyproject.toml version correctly but of course does not know about the uv.lock version. If there would be an option to omit the project version from the uv.lock it would be great, as I currently also don’t understand why it needs to be kept in the lock-file itself.
(Once we have clarity on what we want to do, I’ll prioritize the change.)
If references or opinion articles exist around this topic, they are few in number. I think a few on this issue would be interested in wider discussion around this. PEP 621 introduced toml metadata, and as of writing the official specification still allows
versionto be set dynamically, with no recommendation to the contrary.It’s a pretty standard paradigm (see hatch-vcs, setuptools-scm, poetry-dynamic-versioning) for single project repos where versioning, point release branches, development builds, etc. are all managed and controlled via VCS change control.
Whilst alternatives exist for separating static versions (such as
0.5.1from a generated identifier ref likef399a5271inuv 0.5.1 (f399a5271 2024-11-08)), there are some compelling reasons to use dynamic (such as dunamai) formed versions:0.5.1for the git tagv0.5.1and publishing builds)My expectation for the project package that has a dynamic version (
source = { editable = "." }specifically) would be simply not defining version inuv.lockfor the local package (or providing a dynamic reference). This by nature isn’t a lockable reference (especially when set by VCS signatures) whereas other uses ofuv locksuch as multi-project mono repos or static versions uphold the current behaviour.It’s worth noting that although version is defined dynamically in source control, builds of the package provide the version statically:
I just discovered this issue with the recent release 0.5.0. Using --locked when syncing the project makes everything fails. I have a dynamic version based on git tags, and when we release we tag our repository. So the lock file can be outdated without any code change.
I think excluding editable dependencies in the lock file could be a good workaround. Or we can also write “dynamic” as a version. By the way I will check what tools like poetry for example are doing in this use case. @charliermarsh Is there any news on this? In the meantime, we will pin our version to the 0.4.30.
By the way I would suggest to list it in the breaking changes of the release
Thanks !!!
Edit: Forgot to mention, I was wondering if you already have some implementation ideas, I totally volunteer to work on this if needed. I will try a draft pull-request on my side for fun
I too have this problem. I generally subscribe to the philosophy that I use a VCS for versioning, thus what is in the VSC repo shouldn’t track it’s own version because that’s what I’m using the VCS for. Said another way, at best a version tracked within repo is imperfect for exactly the reasons described above about how you can’t update a version from a commit without making another commit, i.e., a new version.
I’d love to understand the rationale behind storing this version in the lock file. Can anyone elaborate on that?
I think you could pass
--upgrade-package attrsto ensure thatattrsis rebuilt and that the lockfile is updated. Or even set it in your configuration:But there’s sort of an infinite loop here, right? Since every time you commit the lockfile, it will be outdated. So you then re-lock, and commit again, etc. I don’t know how to solve this holistically. We could omit versions for editables, maybe? Or even, write “dynamic” or something for the version, for packages that have dynamic versions?
Yeah, it seems like fundamentally the only way to make this work with
setuptools-scmis to omit the version or otherwise mark it as dynamic. I don’t think it’s impossible, but it’s not, like, an hour of work. I’m also fairly worried about how we would integrate this if PEP 751 gets approved – the PEP doesn’t make any carveouts for dynamic versions, and there hasn’t been any discussion of it in the PEP thread. If we add a feature like this here, and it doesn’t make it into the PEP, we’ll have to remove it later.Not exactly, I don’t think. The package always DOES have a version. It’s just that the version may change outside of uv’s immediate notice. Perhaps it would be fine if
uv lockanduv syncand similar commands re-assessed the version if it’s markeddynamicin thepyproject.toml. That wouldn’t handle cases where the dynamic version contains the git commit SHA in some form, but I suspect that’s pretty much an intractable problem.That said, I feel like a cleaner solution would be to omit the main package from
uv.lockentirely. However I’m definitely not familiar with the implementation here, so I don’t know what unforeseen consequences that might have.We use a
Taskfile.ymltask definition, so I have a little more flexibility, and I use it to apply an ugly work-around: When we update the lock I force the version to be0.0.0by settingSETUPTOOLS_SCM_PRETEND_VERSION(which works for eitherhatch-vcsorsetuptools-scm):and then, when linting:
This lets us at least avoid recording the current dynamic version in the lock file.
I’m going to ask that if @dsully or anyone else wants to express concern over standardizing lock files they bring it up on the PEP 751 discussion over on discuss.python.org and not here as I don’t think the uv issue tracker is the best place to talk about PEPs that affect the wider Python packaging ecosystem 😉
my use case is that I use
setuptools-scmand the presence of commit-dependent version numbers inuv.lockis a frustrating cause of git merge conflicts preventing what otherwise would be an automated merge.I’ve fixed the issue @RazerM pointed out in https://github.com/astral-sh/uv/pull/8867. I guess I’ll leave this issue open though since it’s more about the inherent incompatibility between lockfiles and VCS-based versioning.
Thank you, this was a real bug. Fix here: https://github.com/astral-sh/uv/pull/11046.
Many thanks for making this change and including it in
v0.15.9! 🎉🎂💖🎆🪩🌌🥦I just recreated
uv.lockfor a project with a dynamic version, and can confirm that the resultinguv.lockno longer contains the dynamic version for the project. I deleted the prioruv.lockbefore runninguv lockwithv0.15.9, just to be safe.Sorry to pipe in on a largely completed debate but I have one idea to put.
My issue here seems the same as many other users:
Changing any one of these links could fix the issue. I think most of the discussion has been about 1 and 2. That is, either moving away from dynamic versioning or changing how lock files work for local packages.
However, a simpler resolution might be via step 3? If uv were to allow users to check that lock files are up to date apart from the versions of any current workspace packages that have dynamic versions then the current workflow would be able to continue. Effectively this returns to the workflow of the pre 0.5.0 uv, while still correctly updating the lock file (and re-installing the package). I would expect this would catch most of the current use cases for
uv lock --frozenThis could work by adding an option to
uv lock --frozenetc to allow versions of local packages with dynamic versioning to change but to error if any other dependencies change.I think https://github.com/astral-sh/uv/pull/8867 released in
uv==0.5.0leadsuvto correctly respect dynamic metadata, which now breakssetuptools-scm+pre-commitusers using theuv-lockhook: https://github.com/astral-sh/uv-pre-commit/issues/25.This is textbook Hyrum’s Law 😆
To share on the SCM versioning debate, I use
setuptools-scmas it’s a nice way for an automated (no human error) workflow of GitHub tagged release to PyPI publish with matching version.This is just my personal opinion! Dynamic metadata makes a number of things more complicated than they need to be. I understand the use-case for scm versioning, but in my opinion using dynamic metadata to read a version from
__init__.pyis a really heavy hammer with the only benefit being that you get to avoid writing out a constant twice – I was responding to that specific case, where I don’t think the tradeoffs are very good.While the setuptools-scm case seems difficult to solve, I think another common reason[^1] a dynamic version is used is when the version is statically defined but not in
pyproject.toml. Typically as a__version__attribute like in this example:Create example project
Configure
pyproject.tomlto use a dynamic versionAdd
__version__attribute to__init__.pyRun
uv lockChange version number
Run
uv lockWhat I expected:
What happened: uv.lock still has
version = "1.0"I’ve also tried
uv lock --no-cache --refreshto no avail.The version doesn’t change until I do an unrelated
uv addor something. This seems like a cache invalidation problem more than anything else, which I expectedcache-keysto solve.[^1]: sometimes for backward compatibility reasons or in my case I can’t move version into pyproject.toml until some other tooling I use catches up.
Yeah that’s what I meant in my last sentence, but I’m always assuming Chesterton’s Fence. 😃 Setting it to
dynamicseems the best way since it maps nicely toproject.dynamic. Just dunno if it would be a problem that there’s a case where there’s an invalid version “number” in the field which would speak for omitting.@charliermarsh Use this repo: https://github.com/apollo13/uv-bug
Follow this to the letter (I am not sure if you can repro if you do anything different):
uv cache cleanuv lockand observe thatgit statusshows no changesuv syncrm -rf uv.lock .venvuv lockgit diffand see the version written into the lock file (shouldn’t be)uv cache cleanrm -rf uv.lock .venvuv lock(No version written -> okay)The solution here is what should be preferred for
hatch-vcs,setuptools-scm, etc. Basically, only temporarily set theSETUPTOOLS_SCM_PRETEND_VERSIONenvironment variable when runninguv lock.Since PEP 751 and therefore a holistic solution within uv looks like it will take some more time, here is my work-around to avoid the
uv sync<->git commitloop ofuv.lock, aimed at local development of an appmyapp:Instead of running
uv syncor e.g.uv run myentrypointwhich callsuv syncimplicitely, I do the following:I take care to only commit the modified
uv.lockwithversion = 0.0+dynamic. I’m sure that this approach could be integrated into a pre-commit rule as well, but haven’t looked into it yet. Of course, this can also be integrated into the task runner of your choice, I use task and myTaskfile.ymlhas something like:For the moment, this serves me well. If there are any unintentional side-effects, I’m eager to learn about them
@thcrt This probably isn’t exactly what you’re looking for, but I’ve found this works:
https://gist.github.com/nathanjmcdougall/42135563c719cfda59a1de317da5116b
Note that this deliberately avoids running
uv sync, to avoid the issue. I’m usinghatch-vcs.For posterity: https://discuss.python.org/t/pep-751-now-with-graphs/69721/86
Can confirm that our workflow (using hatchling as build backend with a dynamic version) breaks when trying to do
uv sync --lockedsince uv0.5.0.4.30is fine. Whether it is actually #8867 that broke it I cannot tell, but sounds plausible.By the way, this can be inverted and made more standard-compliant while doing so by putting something like this into your
__init__.py:You get static metadata with no duplication.
As I mentioned in ttps://github.com/astral-sh/uv/issues/7994 I use
python-semantic-releasewhich behaves the same. The way I work around the infinite build loop is to configure my Jenkins script to skip commits that contains thepython-semantic-releasemessage - these commits include the changedpyproject.tomland__version__.pyfile, along with a generated CHANGELOG.mdAs a workaround, I also added
uv lockat the start of the build script andgit add uv.lockat the end - this ensures thatpython-semantic-releasecommit includes the correctly versioneduv.lockfile (I’m not sure yet if this causes other problems, i.e. updating other packages - ideally there would be a way of only updating the package version inuv.lockand nothing else.I have while searching this bug tracker, but I couldn’t find a way for it to affect
uv.lock? Changing the value tocache-keys = [{ git = true }, { file = "pyproject.toml" }]at least doesn’t do anything when runninguv.lock.From the documentation (and its name 🤓) I’ve gathered it’s mostly about how caching, not about locking? And TBH it sounds to me as if adding git as a git key, I’m actually adding cache busting.
Perhaps I am misunderstanding the fix action, using this improperly, or this is a
hatch-vcsproblem, but are we sure this is working as intended? It seems that the behavior is inconsistent and flip flops depending on the state of uv’s locking/my venv. Please let me know if this is better suited for a separate issue thread.I have a
pyproject.tomllike this:As a caveat, I experienced a huge amount of inconsistency in reproduction (generally almost always ending in a state where the version was included in the
uv.lockfile), so I am not 100% confident these steps will repro for others. But this is what I used to repro my observed behavior.uv.lock+.venvuv lock. This initially works as expected, no dynamic version inuv.lockuv sync. Still fine, no dynamic version inuv.lock. But we see the package has been built according to the logs:uv lockagain. Now we see these new logs:And the dynamic version is present in the lock file:
uv lock, oruv sync, the dynamic version remains. There’s probably other ways, but if I want to reset the process, I can add this to pyproject.toml and rerunuv lock:and
uv lockyields:uv syncanduv lockagain:Here’s a temporary fix for those using
hatch-vcsthat doesn’t require setting environment variables in your development environment, unless you want to override the version, such as when rolling a distribution.Copy the following code block, paste it into a file named
hatch_build.py, and save it next topyproject.toml. Then add[tool.hatch.metadata.hooks.custom]topyproject.toml.This uses the
hatchlinghook system to set theSETUPTOOLS_SCM_PRETEND_VERSIONenvironment variable automatically during the build process, but only if the variable isn’t already set and theCIenvironment variable isn’t set, such as when building with GitHub actions.Of course, feel free to set the default version to something that fits your project.
from @charliermarsh:
Narrowing things down, this specific issue is only when you are trying to pin the version of the repository you are currently working in. No other dependency matters, editable, dynamic, or not, since
uv.lockupdates won’t cause the infinite loop if they don’t effect those repos (so I’ll ignore whether or not its helpful to try and lock a-e).Furthermore, you don’t need to mark the version in
uv.lockof the current repository, theoretically, because theuv.lockwill be checked into git against that version anyway.So I propose something like
version = "self"if theuv.lockis storing metadata for the same project from which apyproject.tomlis in that same directory.Side Note: Further Spooky Behavior (extra notes I’m writing for if I come back to this convo)
nevermind about side note, its just https://github.com/astral-sh/uv/issues/6860 + not knowing when
uv lock --upgraderuns or doesn’t@nathanjmcdougall Incredible. You are a saint (and a fellow Kiwi!)
Genuinely, thank you so much, this has been a major headache for me in two different projects.
I imagine that not running
uv syncin CI may have its downsides, but I’m happy to run it locally and manually if that’s the tradeoff for having builds work properly.Thank you so much again.
Having a universal lockfile seems better than trying to add support for 100 different lock file formats to every other SBOM tool out there 😄
I’ve been deeply involved in the thread and don’t think it’s come up, but I’ll post about it today.
(Edit: @hynek already did.)
@charliermarsh Out of curiosity, why?