Yamllint: Difference between revisions

explain block scalars a bit
link to github action workflow for yamllint in the Meza repo
 
(One intermediate revision by the same user not shown)
Line 3: Line 3:
Use an online validator like https://www.yamllint.com/ or https://jsonformatter.org/yaml-validator  
Use an online validator like https://www.yamllint.com/ or https://jsonformatter.org/yaml-validator  


Of course, you should have one in your local tools and CI pipeline to ensure that your [[YAML]] is always correct. With <code>yamllint</code>, there is both a script and a Python module; meaning you can write your own linting tool in Python by invoking (importing) the yamllint module<ref>https://yamllint.readthedocs.io/en/stable/development.html</ref>. See the caveat section below about "using the right tool for the job" - meaning use the right linter for the language/project you are linting.
Of course, you should have one in your local tools and [https://github.com/freephile/meza/blob/e4ba295c4705ba982ae3b5c100ce212d366c1330/.github/workflows/yamllint.yml CI pipeline to ensure] that your [[YAML]] is always correct. With <code>yamllint</code>, there is both a script and a Python module; meaning you can write your own linting tool in Python by invoking (importing) the yamllint module<ref>https://yamllint.readthedocs.io/en/stable/development.html</ref>. See the caveat section below about "using the right tool for the job" - meaning use the right linter for the language/project you are linting.


{{Notice|If you have a <tt>.yamllint</tt> file in your working directory, it will be automatically loaded as configuration by yamllint.}}
{{Notice|If you have a <tt>.yamllint</tt> file in your working directory, it will be automatically loaded as configuration by yamllint.}}
Line 10: Line 10:
==Manually fix errors==
==Manually fix errors==
Unfortunately, <tt>yamllint</tt> does not fix your file for you. There could be ambiguities about the proper fix, so you need to do the fix(es) yourself.
Unfortunately, <tt>yamllint</tt> does not fix your file for you. There could be ambiguities about the proper fix, so you need to do the fix(es) yourself.
== VSCode settings ==
I had a problem with VSCode automatically re-introducing spaces around curly braces - causing the very same yamllint errors I was trying to fix. Despite trying various linters (aka formatters), I ultimately found that the only way to be able to save the correct formatting was to '''disable''' <code>formatOnSave</code> in my user settings. This was because the "default settings" (<code>/defaultSettings.json</code>) was read-only so I could not change it, AND it was configured to <code>formatOnSave</code> - using the kennylong linter which is apparently bad for yaml outside kubernetes contexts.
In VSCode, use <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> to open the preferences dialog and type 'Preferences: Open User Settings' then open User Settings (JSON) settings. Ensure you have lines like this (especially the editor.formatOnSave line because I tried other linters like "redhat.vscode-yaml" without success):<syntaxhighlight lang="json">
"[yaml]": {
"editor.defaultFormatter": "kennylong.kubernetes-yaml-formatter",
"editor.autoIndent": "advanced",
"editor.formatOnSave": false
},
</syntaxhighlight>


==Cheatsheet==
==Cheatsheet==