Skip to content

Commit 2cd583a

Browse files
chiragramanicopybara-github
authored andcommitted
Fix valid json when using jsonproto output in queries with new --output=streamed_jsonproto implementation.
Closes bazelbuild#18701. PiperOrigin-RevId: 555417403 Change-Id: I30eb06f734188f8511884954f43c5f5b3c0091a3
1 parent 138f06f commit 2cd583a

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

src/main/java/com/google/devtools/build/lib/query2/query/output/OutputFormatters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public static ImmutableList<OutputFormatter> getDefaultFormatters() {
3737
new PackageOutputFormatter(),
3838
new LocationOutputFormatter(),
3939
new GraphOutputFormatter(),
40-
new JSONProtoOutputFormatter(),
4140
new XmlOutputFormatter(),
4241
new ProtoOutputFormatter(),
42+
new StreamedJSONProtoOutputFormatter(),
4343
new StreamedProtoOutputFormatter());
4444
}
4545

src/main/java/com/google/devtools/build/lib/query2/query/output/QueryOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public OrderOutputConverter() {
4040
effectTags = {OptionEffectTag.TERMINAL_OUTPUT},
4141
help =
4242
"The format in which the query results should be printed. Allowed values for query are:"
43-
+ " build, graph, jsonproto, label, label_kind, location, maxrank, minrank, package,"
44-
+ " proto, xml.")
43+
+ " build, graph, streamed_jsonproto, label, label_kind, location, maxrank, minrank,"
44+
+ " package, proto, xml.")
4545
public String outputFormat;
4646

4747
@Option(

src/main/java/com/google/devtools/build/lib/query2/query/output/JSONProtoOutputFormatter.java renamed to src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedJSONProtoOutputFormatter.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
import java.nio.charset.StandardCharsets;
2323

2424
/**
25-
* An output formatter that outputs a protocol buffer json representation of a query result and
26-
* outputs the json to the output print stream.
25+
* An output formatter that prints a list of targets according to ndjson spec to the output print
26+
* stream.
2727
*/
28-
public class JSONProtoOutputFormatter extends ProtoOutputFormatter {
28+
public class StreamedJSONProtoOutputFormatter extends ProtoOutputFormatter {
2929
@Override
3030
public String getName() {
31-
return "jsonproto";
31+
return "streamed_jsonproto";
3232
}
3333

3434
private final JsonFormat.Printer jsonPrinter = JsonFormat.printer();
@@ -42,7 +42,11 @@ public void processOutput(Iterable<Target> partialResult)
4242
throws IOException, InterruptedException {
4343
for (Target target : partialResult) {
4444
out.write(
45-
jsonPrinter.print(toTargetProtoBuffer(target)).getBytes(StandardCharsets.UTF_8));
45+
jsonPrinter
46+
.omittingInsignificantWhitespace()
47+
.print(toTargetProtoBuffer(target))
48+
.getBytes(StandardCharsets.UTF_8));
49+
out.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
4650
}
4751
}
4852
};

src/test/shell/integration/bazel_query_test.sh

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ genquery(name='q',
668668
opts = ["--output=blargh"],)
669669
EOF
670670

671-
local expected_error_msg="in genquery rule //starfruit:q: Invalid output format 'blargh'. Valid values are: label, label_kind, build, minrank, maxrank, package, location, graph, jsonproto, xml, proto"
671+
local expected_error_msg="in genquery rule //starfruit:q: Invalid output format 'blargh'. Valid values are: label, label_kind, build, minrank, maxrank, package, location, graph, xml, proto, streamed_jsonproto, "
672672
bazel build //starfruit:q >& $TEST_log && fail "Expected failure"
673673
expect_log "$expected_error_msg"
674674
}
@@ -1098,7 +1098,7 @@ EOF
10981098
expect_log "//pkg3:t4"
10991099
}
11001100

1101-
function test_basic_query_jsonproto() {
1101+
function test_basic_query_streamed_jsonproto() {
11021102
local pkg="${FUNCNAME[0]}"
11031103
mkdir -p "$pkg" || fail "mkdir -p $pkg"
11041104
cat > "$pkg/BUILD" <<'EOF'
@@ -1108,17 +1108,39 @@ genrule(
11081108
outs = ["bar_out.txt"],
11091109
cmd = "echo unused > $(OUTS)",
11101110
)
1111+
genrule(
1112+
name = "foo",
1113+
srcs = ["dummy.txt"],
1114+
outs = ["foo_out.txt"],
1115+
cmd = "echo unused > $(OUTS)",
1116+
)
11111117
EOF
1112-
bazel query --output=jsonproto --noimplicit_deps "//$pkg:bar" > output 2> "$TEST_log" \
1118+
bazel query --output=streamed_jsonproto --noimplicit_deps "//$pkg/..." > output 2> "$TEST_log" \
11131119
|| fail "Expected success"
11141120
cat output >> "$TEST_log"
11151121

11161122
# Verify that the appropriate attributes were included.
1117-
assert_contains "\"ruleClass\": \"genrule\"" output
1118-
assert_contains "\"name\": \"//$pkg:bar\"" output
1119-
assert_contains "\"ruleInput\": \[\"//$pkg:dummy.txt\"\]" output
1120-
assert_contains "\"ruleOutput\": \[\"//$pkg:bar_out.txt\"\]" output
1121-
assert_contains "echo unused" output
1123+
1124+
foo_line_number=$(grep -n "foo" output | cut -d':' -f1)
1125+
bar_line_number=$(grep -n "bar" output | cut -d':' -f1)
1126+
1127+
foo_ndjson_line=$(sed -n "${foo_line_number}p" output)
1128+
bar_ndjson_line=$(sed -n "${bar_line_number}p" output)
1129+
1130+
echo "$foo_ndjson_line" > foo_ndjson_file
1131+
echo "$bar_ndjson_line" > bar_ndjson_file
1132+
1133+
assert_contains "\"ruleClass\":\"genrule\"" foo_ndjson_file
1134+
assert_contains "\"name\":\"//$pkg:foo\"" foo_ndjson_file
1135+
assert_contains "\"ruleInput\":\[\"//$pkg:dummy.txt\"\]" foo_ndjson_file
1136+
assert_contains "\"ruleOutput\":\[\"//$pkg:foo_out.txt\"\]" foo_ndjson_file
1137+
assert_contains "echo unused" foo_ndjson_file
1138+
1139+
assert_contains "\"ruleClass\":\"genrule\"" bar_ndjson_file
1140+
assert_contains "\"name\":\"//$pkg:bar\"" bar_ndjson_file
1141+
assert_contains "\"ruleInput\":\[\"//$pkg:dummy.txt\"\]" bar_ndjson_file
1142+
assert_contains "\"ruleOutput\":\[\"//$pkg:bar_out.txt\"\]" bar_ndjson_file
1143+
assert_contains "echo unused" bar_ndjson_file
11221144
}
11231145

11241146
run_suite "${PRODUCT_NAME} query tests"

0 commit comments

Comments
 (0)