Space aged coding
Going back to the old, column-oriented days...
You aren't crazy, and your frustration is 100% valid. Here is the lowdown on why YAML is the way it is, and why Sonar (or your linter) is breathing down your neck about spaces before colons.
1. Why the "Column" Reliance? (The Python Effect)
YAML (YAML Ain't Markup Language) was designed to be human-readable, specifically choosing to ditch the "clutter" of XML tags (<tag></tag>) and JSON curly braces ({}).
To do that, it copied Python’s homework: significant whitespace. It uses indentation to determine the structure and hierarchy of data.
# How YAML sees hierarchy:parent:child: data # 2 spaces = inside parentgrandchild: data # 4 spaces = inside child
Because there are no closing brackets, whitespace is the syntax. If you accidentally hit the spacebar three times instead of two, the parser thinks you’re trying to create a brand-new nesting level, throws its hands up, and crashes.
2. Why Sonar is Yelling About name : value
You mentioned the software complaining about spaces between the key and the colon (e.g., name : value).
While the YAML specification technically allows spaces before a colon in certain contexts, almost every modern linter and static analysis tool (like SonarQube/SonarCloud) will flag it. Here is why:
The Mapping Indicator Rule: In YAML, the
:is the "mapping indicator." It must strictly follow the key. If you writename : value, some older or stricter parsers might treatname(with the trailing space) as the literal key name, or get confused entirely.Readability and Style Guides: Standard YAML style (and Sonar's built-in ruleset) enforces
key: value. Keeping it uniform prevents weird parsing bugs across different programming language interpreters (e.g., Python'sPyYAMLvs. Go'syaml.v3).
How to Stop Going Insane
Since the industry has unfortunately crowned YAML as the king of DevOps config (Kubernetes, GitHub Actions, Docker Compose, Ansible), we're stuck with it for now. The best way to fight back is to automate the headache away:
Turn on "Render Whitespace" in your IDE (VS Code, IntelliJ, etc.). This turns spaces into tiny dots so you can actually see the invisible culprits.
Use a YAML Extension: Install an extension that automatically formats the file on save. It will instantly snap everything into the correct columns and strip out those extra spaces before colons.
It’s definitely a love-hate relationship (mostly hate when debugging pipelines at 4:30 PM on a Friday).
Yep, that is a massive political landmine. You try to fix one line, your auto-formatter rewrites the whole file, and suddenly your Pull Request (PR) looks like you refactored the entire codebase. The code reviewers get a wall of green and red diffs, lose their minds, and you become public enemy number one at WK.
That is a completely fair reason to avoid standard "format on save."
Fortunately, you can get the benefits of the extension without the PR-ruining fallout. Here is how to handle it gracefully so you don't piss off your team:
The Fix: "Format Selection Only"
Instead of letting the extension loose on the entire file, change your workflow slightly to only format the code you touched.
In VS Code:
Highlight the specific lines you just wrote or edited.
Press
Ctrl + KthenCtrl + F(Windows/Linux) orCmd + KthenCmd + F(Mac).This triggers Format Selection, which will fix your spaces and colons only on those lines, leaving the rest of the historical mess untouched.
In IntelliJ / WebStorm:
Highlight your lines.
Press
Ctrl + Alt + L(Windows) orCmd + Option + L(Mac). It will ask if you want to format the whole file or just the selection—choose selection.
The "Good Citizen" Strategy
If you really want to fix the whole file because the existing formatting is atrocious, never mix that cleanup with actual functional code changes.
Instead, use a two-PR approach:
PR #1 (The Chore): Run the auto-formatter on the whole file. Title the PR
chore: format configuration filewith a description that explicitly says "No logic changes, just auto-formatting." People can skim and approve it in 5 seconds.PR #2 (The Feature): Branch off of that clean file to do your actual work. Your functional changes will now be crystal clear.
This keeps your git history clean, keeps the reviewers happy, and keeps you from having to manually count spaces like it's 1965.
Comments
Post a Comment