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

automatic format #62

Closed
Huarong opened this issue Aug 10, 2017 · 19 comments
Closed

automatic format #62

Huarong opened this issue Aug 10, 2017 · 19 comments

Comments

@Huarong
Copy link

Huarong commented Aug 10, 2017

Hope this tool can format the code automatic, just like yapf -i for Python.

@adrienverge
Copy link
Owner

No sorry, it doesn't. yamllint is only a linter: it reports potential problems but doesn't fix them (mainly because there could be ambiguities in fixing).

@adius
Copy link

adius commented Feb 21, 2018

This would be really great. If the warning is that there is a trailing newline missing, it would be really easy to fix ;-)

@adius
Copy link

adius commented Feb 21, 2018

Tools like ESlint also only provide auto fixing for a subset or rules...

@zanona
Copy link

zanona commented Nov 26, 2018

yamlint --fix would indeed be great. Not all errors would have support for fixing, just as eslint but at least a few of them like, adding --- on beginning of files, etc.

@jayvdb
Copy link
Contributor

jayvdb commented Jul 12, 2019

Building a new auto-fixing tool around yamllint would be a good approach for anyone wanting this.

e.g. autoflake wraps pyflakes, and autopep8 wraps pycodestyle.

Where the linter doesnt expose enough information to aide fixing, that is a valid problem with the linter.

@jayvdb
Copy link
Contributor

jayvdb commented Jul 12, 2019

It is probably worthwhile adding a note to the readme that reformatting is out of scope.

Maybe include some recommendations on existing Python yaml reformatters , and non-Python ones (jq?), which support the yamllint default style.

The jq wrappers for python are probably the best options IMO , but yq has a problem with indentation kislyuk/yq#52 .

At https://stackoverflow.com/questions/30642317/how-to-automatically-re-indent-a-yaml-file , @ruamel recommends his ruamel.yaml.cmd which utilises his now well recognised ruamel.yaml library - but then I run into https://bitbucket.org/ruamel/yaml.cmd/issues/4/stack-trace-when-using-save-patch-linked

https://github.com/dbohdan/remarshal is popular, and could be used to convert yaml2toml , and then toml2yaml , however note indentation is an issue there too remarshal-project/remarshal#13 .

And another issue mmlb/yamlfmt#10

Hmm..

@jayvdb
Copy link
Contributor

jayvdb commented Jul 12, 2019

https://github.com/mk-fg/pretty-yaml works for several files I've tried, and has very basic cli, but it does discard comments.

$ python -m pyaml /path/to/some/file.yaml

@jayvdb
Copy link
Contributor

jayvdb commented Jul 15, 2019

@ruamel fixed and released a new version of https://pypi.org/project/ruamel.yaml.cmd/ , and I've packaged it for openSUSE https://build.opensuse.org/project/show/home:jayvdb:ruamel

It prefers single quotes over double quotes, and likes \ line continuations within quoted strings, but it does do indents nicely.

@dudicoco
Copy link

I've been breaking my head over finding a formatter that would provide more or less the same features as yamllint via cli or via VSS, i've already tried all of the suggestions mentioned here.

Can someone suggest a tool that would provide formatting similar to the following rules in yamllint:

rules:
  colons: enable
  empty-lines:
    max: 1
  empty-values: enable
  hyphens: enable
  indentation:
    spaces: 2
    indent-sequences: false
  key-duplicates: enable
  new-line-at-end-of-file: enable
  new-lines:
    type: unix
  trailing-spaces: enable

Thanks!

@yvele
Copy link

yvele commented Aug 22, 2020

No sorry, it doesn't. yamllint is only a linter: it reports potential problems but doesn't fix them (mainly because there could be ambiguities in fixing).

@adrienverge can the issue be reconsidered? Most of linters supports auto fixing today, for example:

Not to mention prettier and go fmt that are more code formatters.

I don't want to find an alternative to yamllint, because I find it awesome.. but the lack of autofixing feature will make us find another tool or fork.. I'm using yamllint at an enterprise scale and it's very painful to ask developers manually fixing problems that can "easily" be automated 😓

yamlint --fix would indeed be great. Not all errors would have support for fixing, just as eslint

@adrienverge if you main concern is "mainly because there could be ambiguities in fixing" we can do like ESLint and styleling and ONLY fix errors that are fixable without ambiguities 🤷

This should be more than enough

@adrienverge
Copy link
Owner

Hey @yvele,

I agree such a feature would be great. It could be in a separated command like yamlfix.

Would you want to implement and maintain this?

Here are the 4 main issues I see:

  1. The fix feature would apply unquestionably for some rules (ones about whitespaces).
    But as you point out, for other rules there would be ambiguity. Still, users will want us to fix them anyway (with they would reasonably be right).
  2. There would also be inter-rules ambiguities. For example, a badly-indented key-value pair would be reindented to the next level, while the user wanted it at the previous (parent) level.
  3. Fixing code while respecting rules options (like ignore-shebangs, indent-sequences: consistent, etc.) is a real challenge.
  4. Based on my experience, creating + maintaining such a tool would be much more work than for yamllint itself.
    Moreover, if there's a bug in yamllint, it's OK. But if there's a bug in a tool that modifies users files, you have to be extra careful and reactive to fix bugs, because you could be blamed for ruining users work.

@dudicoco
Copy link

dudicoco commented Aug 25, 2020

My solution was to use yamllint as a pre-commit hook along with the yamlfmt hook: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt

@solarmosaic-kflorence
Copy link

Just FYI https://github.com/lyz-code/yamlfix

@tamere-allo-peter
Copy link
Contributor

FYI another fixer with a different approach : https://github.com/opt-nc/yamlfixer

@paul-civitas
Copy link

Not every rule can be fixed, but some can be trivially fixed without ambiguities for example trailing-spaces

@oussjarrousse
Copy link

Why not combine efforts with one of the yamlfix projects to provide both checking and fixing functionality?

@RukshanJS
Copy link

Just FYI https://github.com/lyz-code/yamlfix

Tested, works perfectly. Thanks a bunch!

@socherbyc
Copy link

Man, this yamlfix broke many of my yaml files. For example, it changes

  example: 'foo #bar'

to

  example: 'foo # bar'

I just can't use it :/

@andrewimeson
Copy link
Contributor

@socherbyc - wrong repo, old closed issue. This is yamllint, not yamlfix.

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

No branches or pull requests