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

scan_folded_as_literal not behaving plausible with trailing whitespace #153

Open
bewee-i opened this issue Dec 22, 2023 · 4 comments
Open
Labels
yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix.

Comments

@bewee-i
Copy link

bewee-i commented Dec 22, 2023

Config:

formatter:
    type: basic
    scan_folded_as_literal: true

Minimal reproducible example:

foo: >
  lorem ipsum 

(note there is a trailing whitespace after "lorem ipsum ")

is being formatted to

foo: "lorem ipsum \n"

It took me quite a while to figure out that this is the underlying reason why our 20+ line folded strings got turned into single lines. So I guess that this behaviour is for the very least misleading and probably also not desired.

@bewee-i
Copy link
Author

bewee-i commented Dec 22, 2023

This is not limited to trailing whitespaces in the last line of a folded string:

foo: >
  lorem 
  ipsum

is being formated to

foo: "lorem \nipsum\n"

@braydonk
Copy link
Collaborator

braydonk commented Jan 5, 2024

Thanks for opening an issue. This looks like it's probably caused by the same thing as I've been tracking in #86, which I haven't been able to resolve yet due to its complexity.

@mikelorant
Copy link

Can also confirmed this is happening with normal block scalars.

Before

block: |
  key: value (single whitespace appended)

After

block: "key: value \n"

Expected

block: |
  key: value (whitespace removed)

If there is no trailing whitespace, everything words as expected. As per @bewee-i any line with a trailing whitespace will cause this occur, it is not limited to the last line.

@braydonk braydonk added the yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix. label Jan 18, 2024
@braydonk
Copy link
Collaborator

Thank you for the additional example.

On the root issue #86 I added an explanation for why this bug occurs, but I'll copy that explanation here for visibility:

This is caused by the hack around the fact that yaml.v3 doesn't retain plain line-break information. yamlfmt will insert a magic string before being serialized into yaml.v3's node structure. Then after the new output is produced, we use the magic strings to put them back in. However, one place that newline information will actually be retained is in block scalars. So the magic line string being thrown on there messes with the serialization. I can't think of a way around this without somehow getting rid of the hack. I've tried in the past to fix this in yaml.v3 but I came up short. I've been wanting to build my own yaml parser instead to rid myself of yaml.v3 in general, and in that case this and many other fixes/features I've wanted to implement would be possible. Haven't had the time to invest to make that happen though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix.
Projects
None yet
Development

No branches or pull requests

3 participants