-
-
Notifications
You must be signed in to change notification settings - Fork 616
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
yq creates a file it cannot parse by moving comments #427
Comments
Yeah that's def dodgey - I noticed the comment placing before, but didn't realise it could result in what seems to be bad yaml. Unfortunately this is handled by the underlying yaml parser yaml.v3 (https://github.com/go-yaml/yaml/tree/v3) so a fix will have to happen there, then I can simply bump the version and it will be fine. |
Looks like this is related to go-yaml/yaml#587 |
and this go-yaml/yaml#459 |
In the mean-time a workaround would be a mechanism to optionally strip the comments before modifying. |
Ah good one - I'll look into that |
This seems to be a workaround for stripping all the comments first but it isn't fun. for doc in $(seq 0 "$(grep -c '^---' a.yaml)")
do
printf -- '---\n'
yq read --stripComments -d "${doc}" a.yaml
done >a2.yaml Would be better if this worked without removing all the document headers ( yq read --stripComments --separateDocuments -d '*' a.yaml >a2.yaml |
go-yaml doesn't promise to preserve bit by bit the textual representation, so if that's the requirement we indeed won't be able to handle it in the short term. We may end up having a flag to strip comments, but meanwhile it should be quite straightforward to have a function that just zeros them out in the node structure. |
Definitely not a requirement for my scenario. I'm not concerned if the comments move or get deleted, as long as the output yaml is still valid. I reported this bug only because the changing comments led to unparseable yaml. |
Can you please provide an example in a new issue outlining that case? The existing ones in go-yaml are reorderings that preserve the data as-is, so if there's a case that breaks I want to look into it and fix it. Thanks for your help. |
This takes a valid yaml and produces invalid yaml: |
Describe the bug
Begin with yaml file with a comment placed within an array. Use
yq
to modify an unrelated document in the same file several times and watch the comment move each time untilyq
eventually fails withError reading document...
Input Yaml
Concise yaml document(s) (as simple as possible to show the bug)
a.yaml
:Command
The command you ran:
Actual behavior
b.yaml
c.yaml
and
d.yaml
fails to be created.Expected behavior
d.yaml
Additional context
Using: https://github.com/mikefarah/yq/releases/download/3.3.0/yq_linux_amd64
The text was updated successfully, but these errors were encountered: