commitizen icon indicating copy to clipboard operation
commitizen copied to clipboard

`cz` commands failed if there is already a config file but in wrong format

Open Bogay opened this issue 3 years ago • 1 comments

Description

Run cz commands, e.g. cz init, cz version will fail if there is already a config file with default name but in wrong format.

Although this might not be a common case, IMO it would be better to tell users there is a wrong config (if cannot find others).

Steps to reproduce

mkdir /tmp/cz-init
cd /tmp/cz-init
echo "I am not json" > .cz.json
cz init

Current behavior

cz init failed caused by decode error. Outputs attached below.

Traceback (most recent call last):
  File "/home/bogay/.local/bin/cz", line 8, in <module>
    sys.exit(main())
  File "/home/bogay/.local/pipx/venvs/commitizen/lib/python3.8/site-packages/commitizen/cli.py", line 354, in main
    conf = config.read_cfg()
  File "/home/bogay/.local/pipx/venvs/commitizen/lib/python3.8/site-packages/commitizen/config/__init__.py", line 37, in read_cfg
    _conf = JsonConfig(data=data, path=filename)
  File "/home/bogay/.local/pipx/venvs/commitizen/lib/python3.8/site-packages/commitizen/config/json_config.py", line 14, in __init__
    self._parse_setting(data)
  File "/home/bogay/.local/pipx/venvs/commitizen/lib/python3.8/site-packages/commitizen/config/json_config.py", line 46, in _parse_setting
    doc = json.loads(data)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Desired behavior

Maybe try to search the next possible config? If none of them are found. Report an error to user. Tell them there is a config with wrong format.

Screenshots

No response

Environment

Commitizen Version: 2.32.3 Python Version: 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0] Operating System: Linux

Bogay avatar Sep 07 '22 20:09 Bogay

Thanks for reporting! I think we can solve it by catching such exception in read_cfg

Lee-W avatar Sep 10 '22 09:09 Lee-W