Skip to content
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

yamlfix crashing when found "duplicate key" #223

Open
KingBain opened this issue Feb 7, 2023 · 3 comments
Open

yamlfix crashing when found "duplicate key" #223

KingBain opened this issue Feb 7, 2023 · 3 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@KingBain
Copy link

KingBain commented Feb 7, 2023

Description

yamlfix crashes when run.

Steps to reproduce

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 KingBain added the bug Something isn't working label Feb 7, 2023
@KingBain KingBain changed the title yamlfix crashing yamlfix crashing when found "duplicate key" Feb 7, 2023
@lyz-code
Copy link
Owner

lyz-code commented Feb 8, 2023

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 lyz-code added help wanted Extra attention is needed good first issue Good for newcomers labels Feb 8, 2023
@dkbarn
Copy link

dkbarn commented Jan 31, 2024

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.

@lyz-code
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants