-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
When writting and input file is the same as piped output file, jq writes nothing #2152
Comments
So this isn't actually a jq bug, it's a function of how shells work. The stream redirection you did for outputting ( In general, we recommend using a tool like |
That said, re: your comments about streaming- jq "streams" by default in that input files need not be a single JSON value, but could be multiple JSON values (imagine a file with multiple JSON objects in it- structured logging is a common example). So it passes each of those JSON values one-at-a-time to your jq program. The It also has a streaming mode ( |
TIL. Thanks for the clarification!
Thankfully the documentation was pretty good here on explaining its nuances from what I was thinking Thanks again! I'll close this out now. |
I think I'm not understanding -s and my output file isn't a "appended version of the original" as intended, but ends up totally blank. without the file redirect, I'm getting the expected output and I thought -s would address the "input file is the same as piped output file" issue. What have I missed? Thanks in advance!
Surely this would work, but instead I end up with a file containing '[]\n'. Again, I only have to drop the " > package.json" and I get the expected and desired output. Must I use an env-var to do this?
Here's the best I could come up with:
|
Describe the bug
This is possibly related to #1433 and #1110, and peripherally to #105. If
jq
's input file is the same as its output file (via a pipe),jq
will emit nothing, or perhaps even make a partial write (more below). I have only noticed this on write operations though it is possible a filter could produce this as well.To Reproduce
This is a expected scenario, where the input and output are separate:
The output of the expected scenario will be the entire document with
foo
set to2
.Here is the output of breaking scenario, where
jq
reads inin.json
and also pipes its output there:The output of this breaking scenario is that
in.json
is simply an empty file. Nostderr
is seen either.Environment (please complete the following information):
sysctl kern.version
:kern.version: Darwin Kernel Version 18.7.0: Mon Feb 10 21:08:45 PST 2020; root:xnu-4903.278.28~1/RELEASE_X86_64
)Additional context
This was a head scratcher for me. In hindsight, I'm not even sure this should be a bug. If
jq
streams (I didn't think you could get away with that in a JSON document), then I could easily understand how this breaks because the output is being written as it is being read - what should the outcome be in that case? Undefined, I imagine.For #1433 and #1110, this initially appeared in my tests as an issue with
isatty
but that was a poor rabbit hole. I was only flipping between watchingstdout
and writing to my file (which was also my input).I did mention this is peripherally related to the dead horse that is #106. I am not in a position to put together such a pull request, but I can record my findings and hopefully this report will help someone else who tries to get around it as I did. The workarounds listed in #106 are numerous - I went with writing to a temporary file and then moving it. No biggie.
If there were one ask from this ticket (I hesitate to call it a bug), it might be to either allow
jq
to buffer everything with some kind of flag(-s
doesn't seem to do that, or I misunderstand it), or produce an error when the input gets funky. Though such a case seems very niche. I would not press the maintainers to pursue a fix. I will allow the maintainers to do as they wish with though, and close it. It'll be indexed by web crawlers and that's my primary goal here :)Thanks for maintaining such a delightful, self-standing tool! I've enjoyed
jq
for many years, and I've recently come acrossoq
which adds YAML and XML to the mix.The text was updated successfully, but these errors were encountered: