commitizen icon indicating copy to clipboard operation
commitizen copied to clipboard

Run `cz init` will raise `UnicodeDecodeError`

Open Bogay opened this issue 3 years ago • 5 comments

Description

The cz init command may fail unexpectedly under some condition.
In this case, the error may be caused by my command output is not encoding by UTF-8. I have tried to decode the output using CP950 and after that I can see a error message: 'pre-commit' 不是內部或外部命令、可執行的程式或批次檔。, which means that PowerShell can not find pre-commit to execute, because I forget to install it before run cz init.

Steps to reproduce

  1. Create a empty directory
  2. Enter the directory
  3. execute cz init and use the default setttings

Current behavior

It will raise UnicodeDecodeError, logs are attached below:

? Please choose a supported config file: (default: pyproject.toml) pyproject.toml
? Please choose a cz (commit rule): (default: cz_conventional_commits) cz_conventional_commits
No Existing Tag. Set tag to v0.0.1
? Please enter the correct version format: (default: "$version")
? Do you want to install pre-commit hook? Yes
Traceback (most recent call last):
  File "c:\users\aa\appdata\local\programs\python\python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\aa\appdata\local\programs\python\python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\AA\AppData\Local\Programs\Python\Python38\Scripts\cz.exe\__main__.py", line 7, in <module>
  File "c:\users\aa\appdata\local\programs\python\python38\lib\site-packages\commitizen\cli.py", line 301, in main
    args.func(conf, vars(args))()
  File "c:\users\aa\appdata\local\programs\python\python38\lib\site-packages\commitizen\commands\init.py", line 43, in __call__
    self._install_pre_commit_hook()
  File "c:\users\aa\appdata\local\programs\python\python38\lib\site-packages\commitizen\commands\init.py", line 144, in _install_pre_commit_hook
    c = cmd.run("pre-commit install --hook-type commit-msg")
  File "c:\users\aa\appdata\local\programs\python\python38\lib\site-packages\commitizen\cmd.py", line 23, in run
    return Command(stdout.decode(), stderr.decode(), stdout, stderr, return_code)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa4 in position 13: invalid start byte

Desired behavior

Successfully initialize commitizen project.

Environment

  • commitizen version: 2.18.1
  • python version: 3.8.7
  • operating system: Windows (OS buid: 19043.1237)
  • shell: PowerShell 7.1.3

Bogay avatar Sep 26 '21 06:09 Bogay

Hi @Bogay Did you find any solution for this?

I'm getting below error

UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 806: character maps to <undefined>

souravjamwal77 avatar Aug 02 '22 06:08 souravjamwal77

Can you open a new issue @souravjamwal77 ? I think it's unrelated to the original ticket. I think it was introduced here recently: https://github.com/commitizen-tools/commitizen/pull/522#discussion_r905883408

woile avatar Aug 02 '22 08:08 woile

@Woile Currently cz can recognize the output of powershell, but it exit normally even if pre-commit is not found. Will it be better if the output message explicitly tell users the pre-commit hook installation is fail and return an non-zero exit code? If that's not an issue, this can be closed.

Command outputs are here:

❯ cz init
? Please choose a supported config file: (default: pyproject.toml) pyproject.toml
? Please choose a cz (commit rule): (default: cz_conventional_commits) cz_conventional_commits
No Existing Tag. Set tag to v0.0.1
? Please enter the correct version format: (default: "$version")
? Do you want to install pre-commit hook? Yes
'pre-commit' 不是內部或外部命令、可執行的程式或批次檔。

You can bump the version and create changelog running:

cz bump --changelog
The configuration are all set.

Bogay avatar Aug 07 '22 06:08 Bogay

That makes sense. If you say "Yes I want pre-commit" and cz fails to install/configure it, then it shouldn't pass silently. The user must fix pre-commit first and then continue with cz. Do you think you can submit a PR?

woile avatar Aug 07 '22 07:08 woile

No problem.

Bogay avatar Aug 07 '22 07:08 Bogay

Is this issue still happening?

woile avatar Apr 28 '23 10:04 woile

It's fixed, at least on my end. I think we can close it now.

Bogay avatar Apr 29 '23 13:04 Bogay