Skip to content

Commit 239f2bf

Browse files
committed
Auto merge of #8069 - ehuss:build-finished, r=alexcrichton
Add "build-finished" JSON message. This adds a JSON message when a build is finished. This is useful for tools to know when to stop parsing JSON, which is particularly useful for commands like `cargo test` or `cargo run` where additional output may follow. Closes #7978
2 parents 3a976c1 + c889bbf commit 239f2bf

File tree

7 files changed

+62
-6
lines changed

7 files changed

+62
-6
lines changed

src/cargo/core/compiler/job_queue.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ use super::job::{
7070
use super::timings::Timings;
7171
use super::{BuildContext, BuildPlan, CompileMode, Context, Unit};
7272
use crate::core::{PackageId, TargetKind};
73-
use crate::util;
7473
use crate::util::diagnostic_server::{self, DiagnosticPrinter};
75-
use crate::util::Queue;
76-
use crate::util::{internal, profile, CargoResult, CargoResultExt, ProcessBuilder};
77-
use crate::util::{Config, DependencyQueue};
78-
use crate::util::{Progress, ProgressStyle};
74+
use crate::util::machine_message::{self, Message as _};
75+
use crate::util::{self, internal, profile};
76+
use crate::util::{CargoResult, CargoResultExt, ProcessBuilder};
77+
use crate::util::{Config, DependencyQueue, Progress, ProgressStyle, Queue};
7978

8079
/// This structure is backed by the `DependencyQueue` type and manages the
8180
/// queueing of compilation steps for each package. Packages enqueue units of
@@ -701,6 +700,13 @@ impl<'a, 'cfg> DrainState<'a, 'cfg> {
701700

702701
let time_elapsed = util::elapsed(cx.bcx.config.creation_time().elapsed());
703702
self.timings.finished(cx.bcx, &error)?;
703+
if cx.bcx.build_config.emit_json() {
704+
let msg = machine_message::BuildFinished {
705+
success: error.is_none(),
706+
}
707+
.to_json_string();
708+
cx.bcx.config.shell().stdout_println(msg);
709+
}
704710

705711
if let Some(e) = error {
706712
Err(e)

src/cargo/util/machine_message.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,14 @@ impl<'a> Message for TimingInfo<'a> {
9090
"timing-info"
9191
}
9292
}
93+
94+
#[derive(Serialize)]
95+
pub struct BuildFinished {
96+
pub success: bool,
97+
}
98+
99+
impl Message for BuildFinished {
100+
fn reason(&self) -> &str {
101+
"build-finished"
102+
}
103+
}

src/doc/src/reference/external-tools.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,30 @@ may be found in [the chapter on build scripts](build-scripts.md).
217217
}
218218
```
219219

220+
#### Build finished
221+
222+
The "build-finished" message is emitted at the end of the build.
223+
224+
```javascript
225+
{
226+
/* The "reason" indicates the kind of message. */
227+
"reason": "build-finished",
228+
/* Whether or not the build finished successfully. */
229+
"success": true,
230+
}
231+
````
232+
233+
This message can be helpful for tools to know when to stop reading JSON
234+
messages. Commands such as `cargo test` or `cargo run` can produce additional
235+
output after the build has finished. This message lets a tool know that Cargo
236+
will not produce additional JSON messages, but there may be additional output
237+
that may be generated afterwards (such as the output generated by the program
238+
executed by `cargo run`).
239+
240+
> Note: There is experimental nightly-only support for JSON output for tests,
241+
> so additional test-specific JSON messages may begin arriving after the
242+
> "build-finished" message if that is enabled.
243+
220244
### Custom subcommands
221245

222246
Cargo is designed to be extensible with new subcommands without having to modify

tests/testsuite/bench.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,8 @@ fn json_artifact_includes_executable_for_benchmark() {
16281628
"src_path": "[..]/foo/benches/benchmark.rs"
16291629
}
16301630
}
1631+
1632+
{"reason": "build-finished", "success": true}
16311633
"#,
16321634
)
16331635
.run();

tests/testsuite/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,6 +3200,8 @@ fn compiler_json_error_format() {
32003200
"filenames": "{...}",
32013201
"fresh": $FRESH
32023202
}
3203+
3204+
{"reason": "build-finished", "success": true}
32033205
"#
32043206
.replace("$FRESH", fresh)
32053207
};
@@ -3280,6 +3282,8 @@ fn message_format_json_forward_stderr() {
32803282
"filenames": "{...}",
32813283
"fresh": false
32823284
}
3285+
3286+
{"reason": "build-finished", "success": true}
32833287
"#,
32843288
)
32853289
.run();

tests/testsuite/message_format.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ fn cargo_renders() {
6464

6565
p.cargo("build --message-format json-render-diagnostics")
6666
.with_status(101)
67-
.with_stdout("{\"reason\":\"compiler-artifact\",[..]")
67+
.with_stdout(
68+
"{\"reason\":\"compiler-artifact\",[..]\n\
69+
{\"reason\":\"build-finished\",\"success\":false}",
70+
)
6871
.with_stderr_contains(
6972
"\
7073
[COMPILING] bar [..]

tests/testsuite/test.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,6 +3363,8 @@ fn json_artifact_includes_test_flag() {
33633363
"filenames":["[..]/foo-[..]"],
33643364
"fresh": false
33653365
}
3366+
3367+
{"reason": "build-finished", "success": true}
33663368
"#,
33673369
)
33683370
.run();
@@ -3395,6 +3397,8 @@ fn json_artifact_includes_executable_for_library_tests() {
33953397
"src_path": "[..]/foo/src/lib.rs"
33963398
}
33973399
}
3400+
3401+
{"reason": "build-finished", "success": true}
33983402
"#,
33993403
)
34003404
.run();
@@ -3429,6 +3433,8 @@ fn json_artifact_includes_executable_for_integration_tests() {
34293433
"src_path": "[..]/foo/tests/integration_test.rs"
34303434
}
34313435
}
3436+
3437+
{"reason": "build-finished", "success": true}
34323438
"#,
34333439
)
34343440
.run();

0 commit comments

Comments
 (0)