Skip to content

pretty-format-yaml removes necessary quotes in flow sequences causing invalid YAML #284

@philipp-horstenkamp

Description

@philipp-horstenkamp

Description

The pretty-format-yaml hook removes quotes from scalar values inside flow-style sequences ([...]), even when those values contain special characters (like colons :) that must be quoted to produce valid YAML. This formatting breakage leads to syntax errors when parsing output (e.g., with Docker Compose or YAML linters).


Steps to Reproduce

  1. Create a docker-compose.yaml with the following content:
    services:
      nc:
        image: nextcloud:31.0.7
        restart: unless-stopped
        healthcheck:
          test: [CMD, curl, -f, "http://localhost/status.php"]
  2. Run pretty-format-yaml via pre-commit.
  3. Formatter outputs:
    services:
      nc:
        image: nextcloud:31.0.7
        restart: unless-stopped
        healthcheck:
          test: [CMD, curl, -f, http://localhost/status.php]
  4. YAML parser reports:
    while scanning a plain scalar
    found unexpected ':'
    

Expected Behavior

Quotes around scalars with special characters inside flow sequences must be preserved per YAML specification. The formatter should:

  • Detect when quoting is necessary and preserve it automatically.
  • Or offer a --preserve-quotes/--safe-syntax flag to avoid invalidating valid YAML.

Context

  • YAML spec (1.1/1.2) mandates quotes for ambiguous scalars in flow sequences.
  • Tools like Docker Compose rely on this, making the formatter’s behavior disruptive.

Proposal

Enhance pretty-format-yaml to:

  • Automatically preserve necessary quotes in flow sequences.
  • Or introduce a configuration option to enforce safe syntax preservation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions