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

yq -i does not work on empty files with comments #869

Closed
Yann-J opened this issue Jun 25, 2021 · 7 comments
Closed

yq -i does not work on empty files with comments #869

Yann-J opened this issue Jun 25, 2021 · 7 comments
Labels

Comments

@Yann-J
Copy link

Yann-J commented Jun 25, 2021

Describe the bug

yq -i seems to return an empty file when there are no values in the original file, even losing any original comments.

Version of yq: 4.9.6
Operating system: mac
Installed via: homebrew

Input Yaml

test.yaml:

# comment

Command
The command you ran:

yq eval '.key = "value"' test.yaml -i

Actual behavior

File is empty

Expected behavior

# comment
key: value

Additional context

It works when an empty object is specified in JSON format:

$ echo "{}" > test.yaml
$ yq eval '.key = "value"' test.yaml -i
$ cat test.yaml
{key: value}
@mikefarah
Copy link
Owner

Yeah that's a known issue - though I hadn't considered empty yaml docs with comments. That is annoying.

@mikefarah
Copy link
Owner

Unfortunately there's not much I can do in yq to solve this - the underlying go-yaml parser returns io.EOF when reading a yaml file with no yaml contents (even if that file has valid yaml comments)

@mikefarah mikefarah changed the title yq -i does not work on empty files yq -i does not work on empty files with comments Jul 11, 2021
@mikefarah
Copy link
Owner

mikefarah commented Jul 11, 2021

as a workaround, you could put in an empty object {} like you had along with the comments

# interesting comment
{}

@Yann-J
Copy link
Author

Yann-J commented Jul 15, 2021

OK understood...

I don't think that's a huge issue of course, but just to be clear my issue is not about the comments, but rather that yq -i will essentially be a noop on an empty file. Comments or not, any new value you're trying to insert will not be present in the output. The use case I provided in example will also fail on an empty file without comments:

touch test.yaml
yq eval '.key = "value"' test.yaml -i
cat test.yaml

The (minor) problem I have with starting with an {} empty object is that:

  • We need to know about this workaround :)
  • It will cause the output file to use the compact JSON format (in my script I have a final step to reformat it)

@mikefarah
Copy link
Owner

Good call, I've added it to 'Known Issues' - I'm going to have another look when I get more time and see if I can fix it so it just works.

@mikefarah
Copy link
Owner

I've made some changes in yq in 4.11.0 to workaround go-yaml and to process empty files with comments as you'd expect, let me know how it works for you :)

@Yann-J
Copy link
Author

Yann-J commented Jul 20, 2021

Working fine for me!
Thanks!

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

No branches or pull requests

2 participants