Skip to content

Commit 5392da8

Browse files
fix(providers/uv): treat uv.lock as optional
Closes #1383. `UvProvider.set_lock_version()` unconditionally read `uv.lock`, which made `cz bump` crash with `FileNotFoundError` whenever the lock had not been written yet. The two reproducers in #1383 are: - a freshly initialised uv project (`uv init`) before `uv sync` has run, - a uv workspace member where the lock lives at the workspace root, not alongside the package's own `pyproject.toml`. Add an `if not self.lock_file.exists(): return` early-out so updating `pyproject.toml` is enough; the lock is rewritten only when present. A regression test creates a `pyproject.toml` without a sibling `uv.lock` and asserts that `set_version` updates the project file and does not write a lock. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4d99415 commit 5392da8

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

commitizen/providers/uv_provider.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ def set_version(self, version: str) -> None:
2626
self.set_lock_version(version)
2727

2828
def set_lock_version(self, version: str) -> None:
29+
if not self.lock_file.exists():
30+
# `uv.lock` is optional: a freshly initialised project (or a uv
31+
# workspace member, since the lock lives at the workspace root)
32+
# may not have one yet. Updating `pyproject.toml` is enough.
33+
return
34+
2935
pyproject_toml_content = tomlkit.parse(
3036
self.file.read_text(encoding=self._get_encoding())
3137
)

tests/providers/test_uv_provider.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,24 @@ def test_uv_provider(
119119

120120
file_regression.check(updated_pyproject_toml_content, extension=".toml")
121121
file_regression.check(updated_uv_lock_content, extension=".lock")
122+
123+
124+
def test_uv_provider_without_lock_file(config: BaseConfig, tmp_path, monkeypatch):
125+
"""Regression for #1383: a freshly initialised uv project (or a uv
126+
workspace member) has no `uv.lock` yet; bumping must still update
127+
`pyproject.toml` instead of raising `FileNotFoundError`."""
128+
monkeypatch.chdir(tmp_path)
129+
pyproject_toml_file = tmp_path / UvProvider.filename
130+
pyproject_toml_file.write_text(PYPROJECT_TOML, encoding="utf-8")
131+
assert not (tmp_path / UvProvider.lock_filename).exists()
132+
133+
config.settings["version_provider"] = "uv"
134+
135+
provider = get_provider(config)
136+
assert isinstance(provider, UvProvider)
137+
assert provider.get_version() == "4.2.1"
138+
139+
provider.set_version("100.100.100")
140+
assert provider.get_version() == "100.100.100"
141+
assert "100.100.100" in pyproject_toml_file.read_text(encoding="utf-8")
142+
assert not (tmp_path / UvProvider.lock_filename).exists()

0 commit comments

Comments
 (0)