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

query --output jsonproto produces invalid json when multiple objects are returned #19583

Closed
novas0x2a opened this issue Sep 21, 2023 · 4 comments
Assignees
Labels
query bugs Bugs related to "bazel (c)query" team-Performance Issues for Performance teams type: bug type: documentation (cleanup) untriaged

Comments

@novas0x2a
Copy link

Description of the bug:

bazel query --output jsonproto //pkg1:target+//pkg2:target

produces json that looks like:

{
  "type": "RULE",
  "rule": {...}
}{
  "type": "RULE",
  "rule": {...}
}

i.e. it produces a stream of json objects without the necessary [] wrapper to make it an array. Some tools handle this (e.g. jq has no problem with it) but other parsers (like python's json) fail with

json.decoder.JSONDecodeError: Extra data: line 240 column 2 (char 6270)

I suspect this is the same problem as #14209

Which category does this issue belong to?

CLI

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

git clone https://github.com/bazelbuild/bazel repro
cd repro
git checkout 7.0.0-pre.20230906.2
bazel query --output jsonproto //tools/aquery_differ:aquery_differ+//tools/aquery_differ:aquery_differ_test | python -m json.tool

produces

Extra data: line 241 column 2 (char 6533)

Which operating system are you running Bazel on?

linux

What is the output of bazel info release?

release 6.3.2

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

@brentleyjones
Copy link
Contributor

Can you try 6.4.0rc1, which has this a8b2543?

@brentleyjones
Copy link
Contributor

brentleyjones commented Sep 21, 2023

Key thing: #18701 (comment)

It's now called streamed_jsonproto and it's in NDJSON format.

@novas0x2a
Copy link
Author

novas0x2a commented Sep 21, 2023

It's sort of a minor nit, but maybe it should be called streamed_ndjsonproto? I'm not sure I would have recognized the output as ndjson if it hadn't been mentioned above. Loading it with a json parser requires a bit of additional work, and googling "ndjson python" led me to --json-lines. Knowing the name of the format made it a lot easier to figure it out. It does work, though:

$ bazel info release
release 6.4.0rc1
$ bazel query --output streamed_jsonproto //tools/aquery_differ:aquery_differ+//tools/aquery_differ:aquery_differ_test | python -mjson.tool --json-lines | head -n5
Loading: 0 packages loaded
Loading: 0 packages loaded
{
    "type": "RULE",
    "rule": {
        "name": "//tools/aquery_differ:aquery_differ",
        "ruleClass": "py_binary",

@iancha1992 iancha1992 added the team-CLI Console UI label Sep 21, 2023
@joeleba joeleba removed the untriaged label Sep 26, 2023
@meisterT meisterT added untriaged query bugs Bugs related to "bazel (c)query" team-Performance Issues for Performance teams and removed team-CLI Console UI labels Sep 26, 2023
@joeleba joeleba removed the untriaged label Sep 26, 2023
@zhengwei143
Copy link
Contributor

I'm leaning for this to be a documentation fix (https://bazel.build/query/language#output-formats).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
query bugs Bugs related to "bazel (c)query" team-Performance Issues for Performance teams type: bug type: documentation (cleanup) untriaged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants