adrienverge / yamllint

A linter for YAML files.
GNU General Public License v3.0
2.9k stars 278 forks source link

BUG: `indent-sequences: whatever` not working as intended #602

Closed phantinuss closed 1 year ago

phantinuss commented 1 year ago

From the docs https://yamllint.readthedocs.io/en/latest/rules.html?highlight=indent-sequences#id14 the 5th test is not working as intended: image

yamlconfig:

rules:
    indentation:
        spaces: 2
        indent-sequences: whatever

example.yml

list:
- flying:
  - spaghetti
  - monster
- not flying:
    - spaghetti
    - sauce

results in:

$ yamllint yamlconfig example.yml 
example.yml
  6:5       error    wrong indentation: expected 6 but found 4  (indentation)

I know there is some discussion about this topic already:

But whatever seems to be broken, given the test in the docs doesn't produce the output it should.

phantinuss commented 1 year ago

It seems like whatever is just the same as consistent: https://github.com/adrienverge/yamllint/blob/master/yamllint/rules/indentation.py#L505-L519

phantinuss commented 1 year ago

Also setting the above example to indent-sequences: false will result in a "wrong indentation" error.

phantinuss commented 1 year ago

I know that setting to "whatever" would mean some issues with non-clear meaning in some special cases. But we don't have this level of nesting in the project we are using yamllint in.

If yamllint offers the setting "whatever" it should do just what it claims to do. Otherwise better remove the option altogether. But I like yamllint and want to continue using it and "whatever" would be just what we need.

adrienverge commented 1 year ago

Hello, I just tested with latest yamllint and I have a correct result (no bug):

yamllint -d "rules: {indentation: {indent-sequences: whatever}}" - <<<"
list:
- flying:
  - spaghetti
  - monster
- not flying:
    - spaghetti
    - sauce
"""
# no error reported

(whereas replacing whatever with consistent yields an error: wrong indentation: expected 2 but found 4)

Can you give more information about the conditions in which you experience this problem?

nasbench commented 1 year ago

Hi,

I'm from the same team as @phantinuss. Here is an example of an issue we faced during the CI. Using the whatever setting. This produces an error [error] wrong indentation: expected 14 but found 12 (indentation)

detection:
    selection:
        Field: 'Something'
    filter:
        - Field2:
            - 'Value1'
            - 'Value2'
        - Field3:
            - 'Value3'
            - 'Value4'

Whereas this will not

detection:
    selection:
        Field: 'Something'
    filter:
        - Field2:
              - 'Value1'
              - 'Value2'
        - Field3:
              - 'Value3'
              - 'Value4'

In our understanding and from the docs the whatever setting should allow the first example as the indentation is respected in the parent bloc.

Link to the actual CI error is here

adrienverge commented 1 year ago

Hello again,

This new example is different from the first one. The first one showed a difference in indent-sequences (whether a list is has 0 or 1+ spaces of indentation), the latest one is about spaces (whether a child element has N or ≠N spaces of indentation).

It's hard to understand your problem and what you expect (again, you should post a minimal repro with your exact configuration, yamllint version, etc. if you expect help from volunteers here), but it looks like you want spaces: consistent. The documentation of indentation which has helpful explanation of each option.

nasbench commented 1 year ago

The issue we're trying to describe is practically the same as https://github.com/adrienverge/yamllint/issues/80

The whatever confing states the following whatever means either indenting or not indenting individual block sequences is OK. From my understanding this means when I enforce indentation say to 4 space and apply whatever like this indentation: {spaces: 4, indent-sequences: whatever} The indentation count isn't starting from - but from the key name.

From https://github.com/adrienverge/yamllint/issues/80 and https://github.com/adrienverge/yamllint/issues/97 and the docs. The whatever setting should allow such a case where the linter shouldn't care as its suggested in both issue.

For example, this works because indentation is set to 4 and everything is good

something:
    selection:
        Element:
            - 'data1'
            - 'data2'

But this example doesn't work and states that indentation needs 14 instead of 12 (the counting starts from the -)

something:
    selection:
        - Element1:
            - 'data1'
            - 'data2'
        - Element2:
            - 'data1'
            - 'data2'

The issue with this is that every major editor out there starts counting the tabs from the - so for a user writing a yaml document every time he needs to add 2 spaces manually which is not ideal because as far as I know the settings cannot be changed in the editor (such as vscode) and its not optimal to have the indentation counting by the linter different than the editors. And yamllint doesn't allow for this change. At least the whatever setting should in theory but its not. Hence the claim of the bug

We using the yamllint action ibiqlik/action-yamllint@v3

nasbench commented 1 year ago

Hi,

Any updates on this @adrienverge?

Regards.

adrienverge commented 1 year ago

Hello,

Please answer https://github.com/adrienverge/yamllint/issues/602#issuecomment-1771103491. In particular:

But whatever seems to be broken, given the test in the docs doesn't produce the output it should.

Is it true?

About your latest message (which seems different from the initial issue), how is it different from #80 and #97?

phantinuss commented 1 year ago

That's embarrassing. Using yamllint v1.32.0 works.

If you see my opening post I used

$ yamllint yamlconfig example.yml 

therefore not using the config file as a config file. It should have been

$ yamllint -c yamlconfig example.yml 

Sorry for the noise. It works as intended, indeed.