diff --git a/commitizen/providers/uv_provider.py b/commitizen/providers/uv_provider.py index 0eb19e51a..f6291d6df 100644 --- a/commitizen/providers/uv_provider.py +++ b/commitizen/providers/uv_provider.py @@ -23,7 +23,14 @@ def lock_file(self) -> Path: def set_version(self, version: str) -> None: super().set_version(version) - self.set_lock_version(version) + # `uv.lock` is optional: a freshly initialised project (or a uv + # workspace member, since the lock lives at the workspace root) + # may not have one yet. Updating `pyproject.toml` is enough. + # Use `is_file()` (not `exists()`) to skip directories or other + # path-shaped artefacts as well, matching the convention in + # `cargo_provider.py` / `npm_provider.py`. + if self.lock_file.is_file(): + self.set_lock_version(version) def set_lock_version(self, version: str) -> None: pyproject_toml_content = tomlkit.parse( diff --git a/tests/providers/test_uv_provider.py b/tests/providers/test_uv_provider.py index e0da97916..f47d7a6c2 100644 --- a/tests/providers/test_uv_provider.py +++ b/tests/providers/test_uv_provider.py @@ -119,3 +119,24 @@ def test_uv_provider( file_regression.check(updated_pyproject_toml_content, extension=".toml") file_regression.check(updated_uv_lock_content, extension=".lock") + + +def test_uv_provider_without_lock_file(config: BaseConfig, tmp_path, monkeypatch): + """Regression for #1383: a freshly initialised uv project (or a uv + workspace member) has no `uv.lock` yet; bumping must still update + `pyproject.toml` instead of raising `FileNotFoundError`.""" + monkeypatch.chdir(tmp_path) + pyproject_toml_file = tmp_path / UvProvider.filename + pyproject_toml_file.write_text(PYPROJECT_TOML, encoding="utf-8") + assert not (tmp_path / UvProvider.lock_filename).exists() + + config.settings["version_provider"] = "uv" + + provider = get_provider(config) + assert isinstance(provider, UvProvider) + assert provider.get_version() == "4.2.1" + + provider.set_version("100.100.100") + assert provider.get_version() == "100.100.100" + assert "100.100.100" in pyproject_toml_file.read_text(encoding="utf-8") + assert not (tmp_path / UvProvider.lock_filename).exists()