`cz init` failed if there is already a config with identical filename (json format)
Description
Run cz init failed if there is already a config with identical filename (json format).
Steps to reproduce
mkdir /tmp/cz-init
cd /tmp/cz-init
echo '{"foo": {}}' > .cz.json
cz init
# Select .cz.json as config file, other questions leave defaults
Current behavior
Initialization failed. Error outputs are 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 389, in main
args.func(conf, vars(args))()
File "/home/bogay/.local/pipx/venvs/commitizen/lib/python3.8/site-packages/commitizen/commands/init.py", line 40, in __call__
self._update_config_file(values_to_add)
File "/home/bogay/.local/pipx/venvs/commitizen/lib/python3.8/site-packages/commitizen/commands/init.py", line 157, in _update_config_file
self.config.set_key(key, value)
File "/home/bogay/.local/pipx/venvs/commitizen/lib/python3.8/site-packages/commitizen/config/json_config.py", line 28, in set_key
parser = json.load(f)
File "/usr/lib/python3.8/json/__init__.py", line 293, in load
return loads(fp.read(),
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 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 12)
Desired behavior
Correctly detected there is already a config file. But I am not sure what should cz init do next. It seems to be a undefined behavior currently.
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
I think we should show a better error message to guide the user.
Hi @Bogay , thanks for reporting the issue. I just traced the code a bit.
Due the detection here, commitizen finds no valid config file and assume such file does not exist while .cz.json does exists. It seems we could change the code here to fix this issue.
The original design supposes to be throwing error whenever the file with commitizen config exists 👉 https://github.com/commitizen-tools/commitizen/blob/master/commitizen/commands/init.py#L49. But I seems to miss the case that such file exists without commitizen config.