Skip to content

cz init failed if there is already a config with identical filename (json format) #576

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

Closed
Bogay opened this issue Sep 7, 2022 · 2 comments

Comments

@Bogay
Copy link
Contributor

Bogay commented Sep 7, 2022

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

@Bogay Bogay added the type: bug label Sep 7, 2022
@woile
Copy link
Member

woile commented Sep 8, 2022

I think we should show a better error message to guide the user.

@Lee-W
Copy link
Member

Lee-W commented Sep 10, 2022

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.

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

No branches or pull requests

3 participants