Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#magic___^_^___line when retain_line_breaks: true #84

Closed
mnil opened this issue Jan 10, 2023 · 2 comments
Closed

#magic___^_^___line when retain_line_breaks: true #84

mnil opened this issue Jan 10, 2023 · 2 comments

Comments

@mnil
Copy link

mnil commented Jan 10, 2023

Hello, thanks for a really neat and fast tool! Seems to work fantastic in our mono-repo.

I noticed something strange though when formatting this minimal example:

Foobar:
  baz: >
    Lorem Ipsum is simply dummy text of the printing_and_typesetting industry.

Foobaz:
  baz: "foobar"

which will get formatted into:

Foobar:
  baz: >
    Lorem Ipsum is simply dummy text of the printing_and_typesetting industry. #magic___^_^___line
Foobaz:
  baz: "foobar"

with the following config:

formatter:
  type: basic
  indent: 2
  retain_line_breaks: true

I guess there is some problem in internal/hotfix/retain_line_break.go but could not see where.

@braydonk
Copy link
Collaborator

Thanks for making an issue and the kind words!

This appears to be a strange edge case of scanning folded block scalars, something that's been a hassle. There's two bits of weirdness, one of which yamlfmt has a workaround for, and one that probably needs investigation in the library.

The one with a workaround is that the underlying yaml library cuts out lines for folded block scalars. This actually kind of makes sense, since that's a correct interpretation of folded block scalars from the spec, however it's really uncool for a tool like this where we want to maintain that form. For that, I've implemented a feature in the yaml library that forces re-encoding folded block scalars to retain the line breaks. I added a boolean option in the formatter config called scan_folded_as_literal to enable this. See the config docs for a bit more info there. With scan_folded_as_literal enabled, the first example in the issue remains the same after format.

The second weirdness is something I actually discovered just now while reproducing the issue. If a line in a folded block scalar ends with space characters, the encoder throws a fit and dumps the raw string value instead of producing a folded block scalar as intended. If whitespace is left at the end of a folded block scalar, you get output like this:

Foobar:
  baz: "Lorem Ipsum is simply dummy text of the printing_and_typesetting industry.
    \n#magic___^_^___line\n"
Foobaz:
  baz: "foobar"

This is a really annoying consequence, and I'll start tackling it soon.

That being said, hopefully scan_folded_as_literal will work for your issue! If it does, I will close this issue; I'll be starting the new investigation for folded block scalar lines ending with whitespace in a new issue dedicated to it.

@mnil
Copy link
Author

mnil commented Jan 10, 2023

You are indeed correct! With scan_folded_as_literal: true in my real example seems to give the correct output. Thanks a lot for the quick help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants