-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Valid json when using jsonproto output in queries #18701
Valid json when using jsonproto output in queries #18701
Conversation
86478e4
to
6eb7a29
Compare
cc: @zhengwei143 |
The current output follows the NDJSON (see http://ndjson.org/) format where each line a separate JSON value. The advantage of that is that you can start parsing each line individually |
Rather, the current output is concatenated json instead of ndjson - there isn't any newline separating each target proto. I also just realized a slight discrepancy: currently EDIT: I would suggest renaming it to Back to bazel/src/main/java/com/google/devtools/build/lib/query2/query/output/JSONProtoOutputFormatter.java Lines 39 to 49 in 0efaf54
It is for this same reason that we can't wrap the |
} | ||
out.write("[".getBytes(StandardCharsets.UTF_8)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in #18701 (comment), instead of wrapping it in square brackets, follow the ndjson format and append a newline after the json message instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an add on, I would suggest renaming this to --output=streamed_jsonproto
if you follow my other comment to output it in ndjson format.
cquery --output=jsonproto
actually outputs only a single CqueryResult
(the behavior was wrongly changed recently in 607d0f7 and reverted in dba9e43). So renaming it as mentioned above would ensure that it isn't inconsistent --output=jsonproto
and consistent with --output=streamed_proto
of the other cquery/aquery output formats.
Otherwise, if you wish to stick with --output=jsonproto
and output a single QueryResult
proto, you'll need to find a workaround to first aggregate the targets and wrapping them in the QueryResult
before writing (especially in the case of --order_output=no
).
Thank you @zhengwei143 , @meisterT - updated the diff to update |
The changes look good, thanks! Just a few minor comments and we should be good to go. |
let me know once you submit the comments and I will be happy to look into them, thanks. |
@@ -20,15 +20,17 @@ | |||
import java.io.IOException; | |||
import java.io.OutputStream; | |||
import java.nio.charset.StandardCharsets; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
|
|||
/** | |||
* An output formatter that outputs a protocol buffer json representation of a query result and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is not accurate anymore since we aren't outputting a query result, but a list of targets in ndjson format. Could you update the comment here w.r.t the new output format?
@@ -1109,15 +1109,15 @@ genrule( | |||
cmd = "echo unused > $(OUTS)", | |||
) | |||
EOF | |||
bazel query --output=jsonproto --noimplicit_deps "//$pkg:bar" > output 2> "$TEST_log" \ | |||
bazel query --output=streamed_jsonproto --noimplicit_deps "//$pkg:bar" > output 2> "$TEST_log" \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a few more targets to the $pkg/BUILD
file, query here for //$pkg:*
so that we can test that the overall output format is in ndjson, instead of a QueryResult
.
Of course, I forgot to submit the comments :) |
b6923cc
to
a62d39e
Compare
Hey @zhengwei143 , I have updated the PR however, some of the CI jobs failed and I think |
Thanks for the update! Let me check to see whether it is possible to use |
a62d39e
to
2697336
Compare
2697336
to
73bce66
Compare
I removed jq and tests are all successful. I would like your help with this PR as the changes that went live in 6.3 have a bug where the output isn't in NDJSON format and isn't valid JSON either. I plan to make this change available in 6.4, please let me know if you have any other suggestions for me. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes, just some minor changes requested and it should be good to go (sorry for not catching that one earlier).
Internally, it seems that there have been some usages of --output=jsonproto
that I'll need to migrate. That might take a while but I'll get started while this review is ongoing.
...java/com/google/devtools/build/lib/query2/query/output/StreamedJSONProtoOutputFormatter.java
Show resolved
Hide resolved
@bazel-io flag |
@bazel-io fork 6.4.0 |
…put=streamed_jsonproto` implementation. Closes bazelbuild#18701. PiperOrigin-RevId: 555417403 Change-Id: I30eb06f734188f8511884954f43c5f5b3c0091a3
…put=streamed_jsonproto` implementation. Closes bazelbuild#18701. PiperOrigin-RevId: 555417403 Change-Id: I30eb06f734188f8511884954f43c5f5b3c0091a3
…put=streamed_jsonproto` implementation. Closes bazelbuild#18701. PiperOrigin-RevId: 555417403 Change-Id: I30eb06f734188f8511884954f43c5f5b3c0091a3
…put=streamed_jsonproto` implementation. Closes bazelbuild#18701. PiperOrigin-RevId: 555417403 Change-Id: I30eb06f734188f8511884954f43c5f5b3c0091a3
…w `--ouput=streamed_jsonproto` implementation. (#19226) Fix valid json when using jsonproto output in queries with new `--ouput=streamed_jsonproto` implementation. Closes #18701. Commit 2cd583a PiperOrigin-RevId: 555417403 Change-Id: I30eb06f734188f8511884954f43c5f5b3c0091a3 Co-authored-by: Chirag Ramani <[email protected]>
The changes in this PR have been included in Bazel 6.4.0 RC1. Please test out the release candidate and report any issues as soon as possible. If you're using Bazelisk, you can point to the latest RC by setting USE_BAZEL_VERSION=last_rc. |
When using
bazel query ... --output=jsonproto
, the output JSON is not valid asThis PR fixes the above issue.