yamlfix icon indicating copy to clipboard operation
yamlfix copied to clipboard

yamlfix crashing when found "duplicate key"

Open KingBain opened this issue 3 years ago • 3 comments

Description

yamlfix crashes when run.

Steps to reproduce

  • pip install yamlfix
  • git clone https://github.com/GoogleCloudPlatform/pubsec-declarative-toolkit
  • cd pubsec-declarative-toolkit
  • yamlfix .

Current behavior

Crashes.

[+] Fixed solutions/guardrails-policies/12-cloud-market-place/constraint.yaml
Traceback (most recent call last):
  File "/home/bain/.local/bin/yamlfix", line 8, in <module>
    sys.exit(cli())
  File "/home/bain/.local/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/bain/.local/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/bain/.local/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/bain/.local/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/bain/.local/lib/python3.10/site-packages/yamlfix/entrypoints/cli.py", line 86, in cli
    fixed_code, changed = services.fix_files(files_to_fix, check, config)
  File "/home/bain/.local/lib/python3.10/site-packages/yamlfix/services.py", line 82, in fix_files
    fixed_source = fix_code(source, config)
  File "/home/bain/.local/lib/python3.10/site-packages/yamlfix/services.py", line 154, in fix_code
    source_code = fixer.fix(source_code=source_code)
  File "/home/bain/.local/lib/python3.10/site-packages/yamlfix/adapters.py", line 362, in fix
    source_code = fixer(source_code)
  File "/home/bain/.local/lib/python3.10/site-packages/yamlfix/adapters.py", line 380, in _ruamel_yaml_fixer
    for source_dict in source_dicts:
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/main.py", line 477, in load_all
    yield constructor.get_data()
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 134, in get_data
    return self.construct_document(self.composer.get_node())
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 151, in construct_document
    for _dummy in generator:
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 1627, in construct_yaml_map
    self.construct_mapping(node, data, deep=True)
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 1506, in construct_mapping
    value = self.construct_object(value_node, deep=deep)
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 174, in construct_object
    data = self.construct_non_recursive_object(node)
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 216, in construct_non_recursive_object
    for _dummy in generator:
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 1627, in construct_yaml_map
    self.construct_mapping(node, data, deep=True)
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 1506, in construct_mapping
    value = self.construct_object(value_node, deep=deep)
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 174, in construct_object
    data = self.construct_non_recursive_object(node)
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 216, in construct_non_recursive_object
    for _dummy in generator:
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 1627, in construct_yaml_map
    self.construct_mapping(node, data, deep=True)
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 1507, in construct_mapping
    if self.check_mapping_key(node, key_node, maptyp, key, value):
  File "/home/bain/.local/lib/python3.10/site-packages/ruyaml/constructor.py", line 314, in check_mapping_key
    raise DuplicateKeyError(*args)
ruyaml.constructor.DuplicateKeyError: while constructing a mapping
  in "<unicode string>", line 20, column 5:
        config.kubernetes.io/local-confi ... 
        ^ (line: 20)
found duplicate key "config.kubernetes.io/local-config" with value "true" (original value: "true")
  in "<unicode string>", line 21, column 5:
        config.kubernetes.io/local-confi ... 
        ^ (line: 21)

To suppress this check see:
    http://yaml.readthedocs.io/en/latest/api.html#duplicate-keys

bain@DESKTOP-N2H6GLJ:~/pubsec-declarative-toolkit$ 

Desired behavior

either autofix the issue or present user with a simple error code image

Screenshots If applicable, add screenshots to help explain your problem. -->

Environment

image -->

KingBain avatar Feb 07 '23 18:02 KingBain

Hi @KingBain thank you for taking the time to open the issue. I agree with you that the error should be handled more gracefully. Please remember that if you don't mind having duplicate keys you can always set the yamlfix configuration allow_duplicate_keys to True.

I would not autofix the issue as we may delete information that we should not

lyz-code avatar Feb 08 '23 10:02 lyz-code

I agree the issue should not be autofixed, and I actually don't mind if the error presents a stack trace, but at the bare minimum can yamlfix at least print out the path of the file where the duplicate keys are found? When running yamlfix on a directory, you are given no helpful information guiding you to where the problem is.

dkbarn avatar Jan 31 '24 03:01 dkbarn

Hi @dkbarn showing information as a warning is a good idea, would you be able to make a PR?

lyz-code avatar Jan 31 '24 08:01 lyz-code