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
Preserve comments for a file with only comments #183
Comments
This is a big issue, comments were deleted out of dozens of YAML files that explained the reasoning behind various YAML entries. This was not caught for quite some time until someone opened up a reformatted yaml file a month ago later and could not understand why documentation was missing. Comments within yaml should be preserved, as they are often as important as the actual data in the yaml. |
Comments are kept when the file is not empty, for example, the next snippet is left unchanged by ---
# this is a comment
a: 1 I understand your case, but I feel it's a corner case, as you're using a yaml file for documenting instead of holding data. I'd either suggest you to summit a PR with the fix or add a |
The ruyaml maintainer made a comment on StackOverflow how this could be fixed: https://stackoverflow.com/a/65001415 |
I'm closing this as |
This is an interesting bug because sometimes 'yamlfix' gets added to GitHub workflows that are shared broadly. The "owner" of the YAML files may never even be able to make a decision whether or when yamlfix gets applied...and the workflow owner has no idea whether or not all YAML files that may be processed by their change will have comments. This is a "destructive" bug IMO since it can destroy important information in YAML files, even if those aren't yaml fields. |
EVEN further, the very title in GitHub of 'yamlfix' is: "A simple opinionated yaml formatter that KEEPS YOUR COMMENTS!" Someone adding yamlfix to a workflow or running it across sets of files would assume that the yaml files output would be syntactically identical AND preserve all comments, giving them no pause to run the tool. In reality, this is not the case. |
In six months you've been the only user that has shown this need. As a maintainer I feel it's a corner case that don't reflect the general need of the program. That being said the fix is not difficult. If you're willing to do a pull request I'm more than open to review it. Else, this will remain closed. I'm sorry |
I think this issue is a special case of #123 The current implementation is only handling the case for shebang, but I could see a general implementation that even would not rely on string manipulation an also not need to rely on parsing the buffer. We could add a top-level node with a uuid before parsing, run it through everything, and then just remove the line with the uuid-node -> that would prevent ruyaml from removing the comments, it would be more general as it could handle all comment-only files and multi-document yaml files, the only special case for shebang would be to prevent it from adding the document-start indicator to the first yaml document. @lyz-code I can implement this as well, but only if you want to have it in the codebase / want to maintain such a feature. |
As an additional data point - we would also like to keep comments, even if it's an empty yaml document. Our use case is similar to others - entries are added as a way to document how to override some defaults. I believe the user creating this one had some similar expectations: #255 @lyz-code Could you please reopen this one? I understand not wanting to spend time on it, but seeing that it's requested by a few people it may attract a PR contribution. 🙂 |
Tthe comments can be as valuable...or in some cases more valuable than the actual entries. |
Sure @kbakk , I will only accept a PR to solve this issue as long as it doesn't increase too much the complexity of maintenance |
@nbari your contribution is available since |
Description
When a file has only comments, the comments are removed
Steps to reproduce
Run
fix_code
over:--- # Keep comments!
Current behavior
The file is transformed to:
Desired behavior
The file is unchanged
Additional context
@rsnodgrass created the unit test in this PR which was closed due to inactivity, if you want to implement this, you can follow in that branch
The text was updated successfully, but these errors were encountered: