Skip to content

toYaml produces invalid or unquoted YAML for empty objects in arrays, leading "-", YAML 1.2 numbers, and indented block strings #237

Open

Description

@trackoor

toYaml (used by the default yamlTableConsoleLogger and yamlConsoleLogger) emits YAML that fails to parse or parses back to a different value for four kinds of input, unlike the YAML.parse(toYaml(value)) checks in test/yaml.test.ts.

// npm i [email protected] [email protected] && node repro.mjs
import {inspect} from "node:util"
import {toYaml} from "trpc-cli"
import YAML from "yaml"

for (const v of [[{}], {a: [{}]}, "- a", {"- a": 1}, "-", "---", "0x1F", ".inf", "1.", {k: "  indented\nnext"}]) {
  const y = toYaml(v)
  let parsed
  try {
    parsed = inspect(YAML.parse(y), {breakLength: Infinity})
  } catch (e) {
    parsed = "throws: " + e.message.split(" at line")[0]
  }
  console.log(`${inspect(v, {breakLength: Infinity}).padEnd(26)} -> ${JSON.stringify(y).padEnd(30)} -> ${parsed}`)
}

Actual:

[ {} ]                     -> "{}"                           -> {}
{ a: [ {} ] }              -> "a:\n{}"                       -> throws: Implicit map keys need to be followed by map values
'- a'                      -> "- a"                          -> [ 'a' ]
{ '- a': 1 }               -> "- a: 1"                       -> [ { a: 1 } ]
'-'                        -> "-"                            -> [ null ]
'---'                      -> "---"                          -> null
'0x1F'                     -> "0x1F"                         -> 31
'.inf'                     -> ".inf"                         -> Infinity
'1.'                       -> "1."                           -> 1
{ k: '  indented\nnext' }  -> "k: |-\n    indented\n  next"  -> throws: All mapping items must start at the same column

Expected: each toYaml(v) string is valid YAML that YAML.parse reads back as v.

In src/yaml.ts, renderObjectEntries returns bare emptyValue ({}) without firstPrefix when an object is empty, isPlainString leaves leading -/---/... and YAML 1.2 core-schema numbers (0x1F, 0o17, .inf, .nan, 1.) unquoted, and getBlockHeader omits an explicit indentation indicator (|2-, see https://yaml.org/spec/1.2.2/#8111-block-indentation-indicator) when the first line starts with spaces.

Versions: [email protected] and main (9fe8aef), [email protected], Node.js v26.13.0.

Credit: https://ucsbplse.github.io/

Activity

  1. pullfrog commented on Nov 15, 2026

    @pullfrog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions