Skip to content

stringify() of multiline string with leading space on first line does not round-trip through parse() #692

Description

@Noethix55555

Description

stringify() of a multiline string whose first content line starts with a space produces a block scalar whose declared indentation indicator does not match the body indentation. The result either silently drops a leading space on every line, or is not parseable by the library's own parse().

At the document root ctx.indent is '', but the block indentation indicator is emitted as 1. The body lines, however, are indented with ctx.indent (empty at root), so the declared indicator and the actual body indentation disagree.

Reproduction

import { parse, stringify } from 'yaml'

stringify('  a\n  b')
// => "|1-\n  a\n  b\n"
parse(stringify('  a\n  b'))
// => " a\n b"   ❌ a leading space was lost (expected "  a\n  b")

stringify('  indented\nlines')
// => "|1-\n  indented\nlines\n"
parse(stringify('  indented\nlines'))
// ❌ throws YAMLParseError: Unexpected scalar at node end

More cases that currently fail to round-trip:

parse(stringify(' a\nb'))          // throws
parse(stringify('   x\n y\nz'))    // throws
parse(stringify('    deep\nshallow')) // throws

Expected

parse(stringify(x)) should equal x for any string x. Each of the cases above should round-trip exactly.

Environment

  • yaml 2.9.0 and current main (3.0.0 prerelease)
  • Node.js 22

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