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

--write mode has issues with block indentation indicators #2941

Closed
MarkusTeufelberger opened this issue Jan 25, 2023 · 3 comments · Fixed by #3700
Closed

--write mode has issues with block indentation indicators #2941

MarkusTeufelberger opened this issue Jan 25, 2023 · 3 comments · Fixed by #3700
Assignees
Labels
AAP Ansible Automation Platform bug critical upstream

Comments

@MarkusTeufelberger
Copy link
Contributor

Summary

If blocks are indented using indentation indicators (https://yaml.org/spec/1.2.2/#8111-block-indentation-indicator), the --write mode seems to have a hard time and constantly re-formats the file until it outputs invalid YAML

Issue Type
  • Bug Report
OS / ENVIRONMENT
ansible-lint --version
ansible-lint 6.11.0 using ansible 2.14.1
  • ansible installation method: pip
  • ansible-lint installation method: pip
STEPS TO REPRODUCE

When trying to automatically prettify a larger set of Ansible roles, I detected some strange diffs in files with multiline blocks. I fixed some other stuff in the meantime and after 2-3 ansible-lint runs, it suddenly started crashing.

Here is a reproducing case:

foo.yml:
---
- name: Demo
  hosts: all
  tasks:
    - name: Demo
      ansible.builtin.debug:
        msg: |3
               multi
             line
             message
Desired Behavior

Either leave this as-is or at least don't break it over time.

Actual Behavior

Please give some details of what is happening.
Include a minimum complete verifiable example with:

  • minimized playbook to reproduce the error
  • the output of running ansible-lint including the command line used
  • if you're getting a stack trace, also the output of
    ansible-playbook --syntax-check playbook
# First run:
ansible-lint --write foo.yml

---
- name: Demo
  hosts: all
  tasks:
    - name: Demo
      ansible.builtin.debug:
        msg: |4
              multi
            line
            message

# Running again:
ansible-lint --write foo.yml

---
- name: Demo
  hosts: all
  tasks:
    - name: Demo
      ansible.builtin.debug:
        msg: |4
            multi
          line
          message

# Running _again_:
ansible-lint --write foo.yml
# Now it crashes:
ruamel.yaml.parser.ParserError: while parsing a block mapping
  in "<unicode string>", line 7, column 9:
            msg: |4
            ^ (line: 7)
expected <block end>, but found '<scalar>'
  in "<unicode string>", line 9, column 11:
              line
              ^ (line: 9)

@MarkusTeufelberger MarkusTeufelberger added bug new Triage required labels Jan 25, 2023
@ssbarnea
Copy link
Member

ssbarnea commented Mar 4, 2023

To be honest, I am not surprised it chokes on this feature. I am aware of reformatting bugs with far more popular YAML options, that one being one that I never used myself.

@ssbarnea ssbarnea self-assigned this Mar 4, 2023
@ssbarnea
Copy link
Member

ssbarnea commented Mar 4, 2023

@MarkusTeufelberger This is likely related to the upstream bug https://sourceforge.net/p/ruamel-yaml/tickets/416/ which I was able to reproduce. I will try to find a workaround as I do not know how to fix it in ruamel.

Update: that one is quite problematic because the workaround is to use same indentation for sequences as for hashes, which is not the Ansible formatting, basically ruamel.yaml formatting works only with classic formatting which does not add extra indentation for lists.

@ssbarnea ssbarnea added upstream and removed new Triage required labels Mar 4, 2023
@MarkusTeufelberger
Copy link
Contributor Author

Luckily I could avoid this specific YAML feature in the one case that I encountered it, but in general it is something to be aware of - especially since it did not crash after the first reformat and it took 3 runs in total to get a crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AAP Ansible Automation Platform bug critical upstream
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants