Skip to content

Commit 3457ec3

Browse files
Alpenjclaude
andcommitted
fix(init): read pyproject.toml as UTF-8
`get_default_version_provider` read pyproject.toml with the locale encoding. TOML files are UTF-8, so on Windows a pyproject.toml with non-ASCII text (for example an author name) made `cz init` fail with a UnicodeDecodeError. Assisted By: AI Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c6a9bdf commit 3457ec3

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

‎commitizen/project_info.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def get_default_version_provider() -> Literal[
2727
]:
2828
pyproject_path = Path("pyproject.toml")
2929
if pyproject_path.is_file():
30-
if "[tool.poetry]" in pyproject_path.read_text():
30+
# TOML files are UTF-8 by specification, whatever the locale encoding
31+
if "[tool.poetry]" in pyproject_path.read_text(encoding="utf-8"):
3132
return "poetry"
3233
if Path("uv.lock").is_file():
3334
return "uv"

‎tests/test_project_info.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def _create_project_files(files: dict[str, str | None]) -> None:
1515
if content is None:
1616
path.touch()
1717
else:
18-
path.write_text(content)
18+
path.write_text(content, encoding="utf-8")
1919

2020

2121
@pytest.mark.parametrize(
@@ -48,6 +48,11 @@ def test_available_hook_installers(mocker, which_map, expected):
4848
{"pyproject.toml": '[tool.poetry]\nname = "test"\nversion = "0.1.0"'},
4949
"poetry",
5050
),
51+
(
52+
# Non-ASCII text that the cp949 and cp1252 code pages cannot decode
53+
{"pyproject.toml": '[tool.poetry]\nname = "test"\nauthors = ["Łukasz ✨"]'},
54+
"poetry",
55+
),
5156
({"pyproject.toml": "", "uv.lock": ""}, "uv"),
5257
(
5358
{"pyproject.toml": '[tool.commitizen]\nversion = "0.1.0"'},

0 commit comments

Comments
 (0)