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
Issues with handling of comments at the start of files #74
Comments
And to add, for these examples my .yamlfmt file was:
|
Thank you for opening an issue @SarahFrench, and thank you for the in-depth detail! I took a cursory look at this in the underlying yaml library. What I was able to determine is that what seems like an obviously consistent situation to the reader, namely the way these comments are placed around document-start tokens, is interpreted very differently by the underlying parser. Of the two problems, I suspect one is an unfortunate result of the YAML spec, but one problem may be a bug in the parser that I can potentially fix. TLDR for the rest of these details: The issue with the comments at the head of the document may be reconcilable, but the one with comments on document starts further down the page may not. It all needs more research to be completely sure. Thank you for the issue and information, and I will keep this issue updated if I get anywhere! Deeper technical stuff that is probably only interesting to my future self: The YAML spec has 3 types of comment parsing patterns: What is happening under the hood in this scenario: # Comment
---
a: 1 Is that the comment is being parsed as a The other annoying thing is this scenario of the following document: a: 1
# Comment
---
a: 2 The comment in this scenario stays where it's supposed to after format. Unfortunately it's not because it's the I will need to dedicate some more time to this next week to see what of these behaviours are "up to spec" so to speak, and what of them are due to bad parsing. |
input yaml:
output yaml:
|
Hi @djgoku, I think what you have there is unrelated to this issue. Would you mind opening a new one? |
Hi there! I came across your project when looking for a yaml formatter to use in a project and I encountered some unexpected behaviour that might be useful to you.
The project I'm contributing to the yaml is parsed using Ruby, so there are tags in the yaml to help Ruby map yaml to classes. The problems below aren't caused by those tags, but they make small problems more painful than they'd be otherwise (problem 2 below in particular).
Problem 1 - inconsistent handling of comments in files containing multiple yaml documents
If a file contains multiple yaml documents starting with
---
then the comments are reordered differently at the start of the file versus elsewhere. This seems to be due to the first---
of the file being prioritised as line 1 of the output.This file has two documents with a consistent format:
(Adapted from https://rhnh.net/2011/01/31/yaml-tutorial/ - example shows yaml output from Ruby code)
The formatter introduces differences between the comment positions in the first and subsequent entries:
Problem 2 - tags being moved above comments at start of file
In the example above you can see that the first
!ruby/object:Set
tag is 'pulled' up to the start of the file, above the comment which was originally line 1. This can be an issue if the yaml file starts with a copyright notice (or any other comment that needs to be at the top of the file) because the tag is separated from the yaml and this obscures how the file works.Original file:
A
yamlfmt
moves the tag away from the yaml:Ideally (blocks of) comments at the start of a file would be given a special status and would still be the first lines of the file after formatting.
I appreciate you're a sole maintainer and the project is in v0.x.x at the moment, so I'm not expecting a fix in response! However I thought these examples and info about reproducing problems would be helpful to you.
Happy new year! 👋
The text was updated successfully, but these errors were encountered: