commitizen / commitizen/cz-cli

fix: UnicodeDecodeError when running 'cz init' on Windows with non-English locale

オープン
#1,003 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
JavaScript
スター
17.5k
フォーク
566
平均マージ
8時間 16分
マージ済み PR(30日)
1

説明

## Description
When running `cz init` on Windows, the command fails with a UnicodeDecodeError due to the default file encoding being 'gbk' instead of 'utf-8'.

### Steps to Reproduce
1. Create a new Python project on Windows with Chinese system locale
2. Install commitizen
3. Run `cz init`
4. Select `pyproject.toml` as config file
5. Select commit rule

### Error Message
```
UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 52: illegal multibyte sequence
```

### Technical Details
The error occurs in `commitizen/commands/init.py` when checking for poetry configuration:
```python
@property
def is_python_poetry(self) -> bool:
if not self.has_pyproject:
return False
with open("pyproject.toml") as f:
return "[tool.poetry]" in f.read()) # Here the file is opened without specifying encoding
```

### Expected Behavior
The file should be opened with UTF-8 encoding explicitly to ensure consistent behavior across different system locales.

### Suggested Fix
```python
# In commitizen/commands/init.py
@property
def is_python_poetry(self) -> bool:
if not self.has_pyproject:
return False
with open("pyproject.toml", encoding=self.settings["encoding"]) as f: # self.settings["encoding"] default is utf-8
return "[tool.poetry]" in f.read())
```

### Environment
- OS: Windows
- Python Version: 3.12
- Commitizen Version: latest
- System Locale: Chinese (Simplified)

### ScreenShot
![Image](https://github.com/user-attachments/assets/993cb587-d7ae-4159-a605-3c96a28f7fe8)

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。