Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Haskely opened this issue Jan 20, 2025 · 0 comments

Comments

@Haskely
Copy link

Haskely commented Jan 20, 2025

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:

    @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

# 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant