yaml icon indicating copy to clipboard operation
yaml copied to clipboard

Empty line added for end hierarchy key after #590 fix

Open Shruti157 opened this issue 3 months ago • 4 comments

Describe the bug #590 fix seems to have broken spacing behavior for end hierarchy keys with no values. In this case spaceBefore attribute becomes true and adds an empty line.

To Reproduce

#comment:  This is comment in YAML file
userName: <--- this is correct

#comment: This is comment in YAML file (end hierarchy)
password:
+(extra empty line added) <---- unexpected

Expected behaviour Keys at the end of hierarchy should be parsed without adding extra line, maintaining the original spacing behavior.

Additional context I traced this issue and found that if we remove
indicator !== 'seq-item-ind' which was added as a part of #590 fix then extra line issue was resolved in my use case.

Please provide your thoughts on this

Shruti157 avatar Sep 17 '25 17:09 Shruti157

Could you provide an actual reproduction here? As in, a sequence of JS calls that produce the output that you think is wrong.

eemeli avatar Sep 18 '25 08:09 eemeli

Hey there,

I just used below code to reproduce the issue with YAML.


const YAML = require('./dist')
const fs = require('fs')

const src = `app-secrets:
  ingress:
    # Description: Base64-encoded TLS certificate in one line without any carriage returns or line wrap.
    tlsCertificate:

    # Description: Keystore password for enabling backend TLS communication
    keystorePassword:

  clients:
    # Description: Service account password for connecting to external reporting services.
    reportingServicePassword: example_password_123

    # Description: Application preference settings password.
    applicationPreferencePassword: "SamplePassword@2024"
`

const doc = YAML.parseDocument(src)

const out = String(doc)

fs.writeFileSync('output.yaml', doc.toString(), 'utf8')

When you run this code, you will see a new line is added just after empty end hierarchy.

For your reference, I added screenshot as well.

Image

Additionally, I already mentioned that to resolve this issue I have removed condition

indicator !== 'seq-item-ind' (newline case)

from resolveProps.js which was added as a fix for #590

Shruti157 avatar Sep 29 '25 13:09 Shruti157

Hi @eemeli , any update on this?

Shruti11124 avatar Oct 08 '25 09:10 Shruti11124

No updates here; have been focused on other work. It looks like these are minimal examples of the bug:

parseDocument('a:\n  b:\n\nc:').toString() // 'a:\n  b:\n\n\nc:\n'
parseDocument('- a:\n\n- b').toString() // '- a:\n\n\n- b\n'

where an empty line after a mapping inside a collection with an empty last value gets serialised as two empty lines.

It's probably a matter of the wrong node getting assigned spaceBefore: true, not sure why.

A fix to this would be welcome; not sure when I'm likely to have the time to look into this.

eemeli avatar Oct 08 '25 13:10 eemeli