Skip to content

Commit

Permalink
Fix JSON output of targets --streaming --keep-going when there is an …
Browse files Browse the repository at this point in the history
…error

Summary:
Right now, if the output of `stream_packages` for one of the packages produces only an error, it will add an unnecessary trailing separator, causing malformed JSON.

Instead, just add the separator if we have something else to show.

Reviewed By: zertosh

Differential Revision: D68892303

fbshipit-source-id: 5b957c60eb86e146a29593847691d3bb0688d090
  • Loading branch information
Ian Childs authored and facebook-github-bot committed Jan 30, 2025
1 parent 622f910 commit 31796d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
10 changes: 6 additions & 4 deletions app/buck2_server_commands/src/commands/targets/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,14 @@ pub(crate) async fn targets_streaming(
};
match targets {
Ok((eval_result, targets, err)) => {
if let Some(err) = err {
show_err(&err.into());
formatter.separator(&mut res.stdout);
if let Some(ref err) = err {
show_err(err.into());
}
res.stats.success += 1;
if imports {
if err.is_some() {
formatter.separator(&mut res.stdout);
}
let eval_imports = eval_result.imports();
formatter.imports(
&eval_result.buildfile_path().path(),
Expand All @@ -135,7 +137,7 @@ pub(crate) async fn targets_streaming(
}
for (i, node) in targets.iter().enumerate() {
res.stats.targets += 1;
if imports || i != 0 {
if err.is_some() || imports || i != 0 {
formatter.separator(&mut res.stdout);
}
formatter.target(
Expand Down
36 changes: 24 additions & 12 deletions tests/core/targets_command/test_targets_keep_going.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ async def test_streaming_keep_going_with_single_failure(buck: Buck) -> None:
"//a:does_not_exist",
]
result = await buck.targets(*targets, "--json", "--streaming", "--keep-going")
try:
# TODO(T213880451) we shouldn't emit malformed json on an error
json.loads(result.stdout)
raise AssertionError("Expected json to fail to parse")
except json.decoder.JSONDecodeError:
pass
xs = json.loads(result.stdout)
assert len(xs) == 1
assert xs[0]["buck.package"] == "root//a"
assert (
xs[0]["buck.error"]
== "Unknown targets `does_not_exist` from package `root//a`."
)


@buck_test()
Expand All @@ -99,9 +100,20 @@ async def test_streaming_keep_going_with_single_failing_target_and_one_other_tar
"--keep-going",
)

try:
# TODO(T213880451) we shouldn't emit malformed json on an error
json.loads(result.stdout)
raise AssertionError("Expected json to fail to parse")
except json.decoder.JSONDecodeError:
pass
xs = json.loads(result.stdout)
assert len(xs) == 2

if "buck.error" in xs[0]:
good_target = xs[1]
bad_target = xs[0]
else:
good_target = xs[0]
bad_target = xs[1]

assert good_target["buck.type"] == "prelude//prelude.bzl:a_target"

assert bad_target["buck.package"] == "root//c"
assert (
bad_target["buck.error"]
== "Unknown targets `does_not_exist` from package `root//c`."
)

0 comments on commit 31796d0

Please sign in to comment.