question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Literal block style multilines don't survive round trip; can it be forced?

See original GitHub issue

When using literal multiline block strings inside my YAML file, converting to JS, and then back, sometimes the multiline literal block survives, but not always.

Is there a way to force a particular behavior? Having them be literal block strings inside the YAML is, IMHO, definitely more readable and editable by humans.

An example program:

const yaml = require('js-yaml')

const bad = `
abc:
  - name: xxx
    sample: |
      {
          "abc": 2,
          "def" : {
          "efg" : {
      	"ok" : 6666,
      	"nope" : "./yyy",

`.trim() // make sure the whitespace is not the difference

const good = `
abc:
  - name: xxx
    sample: |
      {
          "abc": 2,
          "def" : {
`.trim() // make sure the whitespace is not the difference

const badDoc = yaml.safeLoad(bad)
const goodDoc = yaml.safeLoad(good)

const goodDumped = yaml.safeDump(goodDoc)
if( goodDumped === good ) {
    console.log( "Round trip was good for GOOD file\n" )
}

const badDumped = yaml.safeDump(badDoc).trim()
if( badDumped !== bad ) {
    console.log( "Round trip was bad for BAD file" )
    console.log( "INPUT: ", bad, "\n\n" )
    console.log( "OUTPUT: ", badDumped,"\n\n" )
}
else {
    console.log( badDumped, bad )
}

The output of this is:

Round trip was good for GOOD file

Round trip was bad for BAD file
INPUT:  abc:
  - name: xxx
    sample: |
      {
          "abc": 2,
          "def" : {
          "efg" : {
      	"ok" : 6666,
      	"nope" : "./yyy", 


OUTPUT:  abc:
  - name: xxx
    sample: "{\n    \"abc\": 2,\n    \"def\" : {\n    \"efg\" : {\n\t\"ok\" : 6666,\n\t\"nope\" : \"./yyy\",\n" 

Note that sample has been turned into a string with newlines encoded, rather than a literal multiline block.

It seems strange that the shorter version (good) does preserve literal block style, but the longer one does not. Am I missing something subtle about using the library?

(IMHO, it would seem more usable to have shorter strings drop the literal multiline block, while longer ones always use the | format, but the library works the opposite way.)

I would really like a way to always have multiline values (any strings with newlines) to be output as literal multiline block strings so that I have the option to edit them by hand easily.

I’m using version "js-yaml": "^3.12.1"

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
RockyYan1994commented, May 20, 2020

Seems like is pass lineWidth to be -1 will set block style to be STYLE_LITERAL https://github.com/nodeca/js-yaml/blob/d6983dd4291849b2854e8d26e1beb302edfd4c76/lib/js-yaml/dumper.js#L274

1reaction
booninitecommented, Oct 4, 2019

// Also prefer folding a super-long line.

This comment does not make functional sense. Anyone using this library to generate YAML containing multi-line blocks with white space sensitive content that goes over an arbitrary line length will get caught on this (which is increasingly common given the pattern of inlining resources in Kubernetes objects such as ConfigMaps). I think it makes more sense for the code to attempt to honor the contents of the string, which would essentially always be |.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Literal block style multilines don't survive round trip; can it be forced?
Is there a way to force a particular behavior? Having them be literal block strings inside the YAML is, IMHO, definitely more readable...
Read more >
YAML Multiline Strings
Block Style Indicator: The block style indicates how newlines inside the block should behave. If you would like them to be kept as...
Read more >
yaml.dump adding unwanted newlines in multiline strings
i.e. each line has two newlines. Now if I read it back into a string I see that all is okay in the...
Read more >
How can I use sed to replace a multi-line string?
You can read multiple lines into the pattern-space and manipulate ... would be (I'd use sed, but that's because I don't know perl...
Read more >
Effective Go - The Go Programming Language
Go provides C-style /* */ block comments and C++-style // line comments. ... (Don't use the import . notation, which can simplify tests...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found