YAML Formatting and LintingΒΆ
IntroductionΒΆ
Our project uses YAML files extensively for configuration, GitHub Actions workflows, and issue templates. To ensure consistent formatting and catch syntax/style issues early, we use yamlfmt for formatting and yamllint for validation.
Key Benefits:
β Separated responsibilities:
yamlfmtformats YAML andyamllintcatches syntax/style issues.π¨ Automatic Formatting: Consistent YAML style across all files
π Fast Performance: Go-based tool for speed and reliability
π§ Highly Configurable: Customizable rules via
.yamlfmtconfigurationπ€ Pre-commit Integration: Runs automatically on every commit
π» No Node.js Required: Pure Go binary, simpler dependency management
π‘οΈ GitHub Actions Compatible: Tested with workflow files
βοΈ Getting StartedΒΆ
PrerequisitesΒΆ
Install Go and yamlfmt:
# Install Go (if not already installed)
just install-go
# Install yamlfmt
just install-yamlfmt
UsageΒΆ
Format all YAML files:ΒΆ
just format-yaml
Automatically fixes formatting in all .yml and .yaml files
Check for YAML lint issues:ΒΆ
just lint-yaml
Shows detailed output of any lint problems
Verify formatting is clean:ΒΆ
just check-yaml
Quick pass/fail lint check with success message
π ConfigurationΒΆ
yamlfmt Configuration (.yamlfmt)ΒΆ
yamllint Configuration (.yamllint)ΒΆ
π Pre-commit IntegrationΒΆ
YAML formatting runs automatically on every commit via pre-commit:
The
yamlfmthook formats.yamland.ymlfiles automatically.The
yamllinthook validates.yamland.ymlfiles automatically.Prevents commits with YAML syntax/style errors.
Ensures codebase consistency and reduces manual reviews.
Example .pre-commit-config.yaml snippet for YAML linting:
repos:
- repo: https://github.com/google/yamlfmt
rev: v0.13.0
hooks:
- id: yamlfmt
name: "Format YAML files"
files: \.ya?ml$
- repo: local
hooks:
- id: yamllint
name: Lint YAML files
entry: uvx yamllint -c .yamllint .
language: system
pass_filenames: false
π Disabling or Skipping YAML LintingΒΆ
To skip linting temporarily, use the
--no-verifyflag ongit commit.To disable formatting rules, edit
.yamlfmt.To disable lint rules, edit
.yamllint.To remove linting completely, remove related pre-commit hooks and
justcommands.