Skip to content

Commit

Permalink
add message when compile starts for crate
Browse files Browse the repository at this point in the history
  • Loading branch information
bend-n committed Oct 22, 2023
1 parent d2f6a04 commit 7d188fa
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/cargo/core/compiler/job_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,13 @@ impl<'cfg> DrainState<'cfg> {
// NOTE: An error here will drop the job without starting it.
// That should be OK, since we want to exit as soon as
// possible during an error.
self.note_working_on(cx.bcx.config, cx.bcx.ws.root(), &unit, job.freshness())?;
self.note_working_on(
cx.bcx.config,
cx.bcx.ws.root(),
&unit,
job.freshness(),
cx.bcx.build_config.emit_json(),
)?;
}
self.run(&unit, job, cx, scope);
}
Expand Down Expand Up @@ -1096,6 +1102,7 @@ impl<'cfg> DrainState<'cfg> {
ws_root: &Path,
unit: &Unit,
fresh: &Freshness,
json: bool,
) -> CargoResult<()> {
if (self.compiled.contains(&unit.pkg.package_id())
&& !unit.mode.is_doc()
Expand Down Expand Up @@ -1129,6 +1136,13 @@ impl<'cfg> DrainState<'cfg> {
if unit.mode.is_check() {
config.shell().status("Checking", &unit.pkg)?;
} else {
if json {
let msg = machine_message::CompileStarted {
package_id: unit.pkg.package_id(),
}
.to_json_string();
_ = writeln!(config.shell().out(), "{msg}");
}
config.shell().status("Compiling", &unit.pkg)?;
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/cargo/util/machine_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,14 @@ impl Message for BuildFinished {
"build-finished"
}
}

#[derive(Serialize)]
pub struct CompileStarted {
pub package_id: PackageId,
}

impl Message for CompileStarted {
fn reason(&self) -> &str {
"compile-started"
}
}
15 changes: 14 additions & 1 deletion src/doc/src/reference/external-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ The "build-finished" message is emitted at the end of the build.
/* Whether or not the build finished successfully. */
"success": true,
}
````
```

This message can be helpful for tools to know when to stop reading JSON
messages. Commands such as `cargo test` or `cargo run` can produce additional
Expand All @@ -257,6 +257,19 @@ executed by `cargo run`).
> so additional test-specific JSON messages may begin arriving after the
> "build-finished" message if that is enabled.
### Compile started

The "compile-started" message is emitted when a crate begins compiling.
This corresponds to the ui's "Compiling <crate>" message.

```javascript
{
"reason": "compile-started",
/* The package getting compiled */
"package-id": "cargo 0.74.0"
}
```

## Custom subcommands

Cargo is designed to be extensible with new subcommands without having to modify
Expand Down
2 changes: 2 additions & 0 deletions tests/testsuite/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,8 @@ fn json_artifact_includes_executable_for_benchmark() {
p.cargo("bench --no-run --message-format=json")
.with_json(
r#"
{"reason":"compile-started","package_id":"foo 0.0.1 [..]"}
{
"executable": "[..]/foo/target/release/deps/benchmark-[..][EXE]",
"features": [],
Expand Down
2 changes: 2 additions & 0 deletions tests/testsuite/binary_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ fn check_msg_format_json() {
.build();

let output = r#"
{"reason":"compile-started","package_id":"foo 0.0.1 [..]"}
{
"reason": "compiler-artifact",
"package_id": "foo 0.0.1 [..]",
Expand Down
6 changes: 6 additions & 0 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3881,6 +3881,8 @@ fn json_artifact_includes_test_flag() {
p.cargo("test --lib -v --message-format=json")
.with_json(
r#"
{"reason":"compile-started","package_id":"foo 0.0.1 [..]"}
{
"reason":"compiler-artifact",
"profile": {
Expand Down Expand Up @@ -3924,6 +3926,8 @@ fn json_artifact_includes_executable_for_library_tests() {
p.cargo("test --lib -v --no-run --message-format=json")
.with_json(
r#"
{"reason":"compile-started","package_id":"foo 0.0.1 [..]"}
{
"executable": "[..]/foo/target/debug/deps/foo-[..][EXE]",
"features": [],
Expand Down Expand Up @@ -3963,6 +3967,8 @@ fn json_artifact_includes_executable_for_integration_tests() {
p.cargo("test -v --no-run --message-format=json --test integration_test")
.with_json(
r#"
{"reason":"compile-started","package_id":"foo 0.0.1 [..]"}
{
"executable": "[..]/foo/target/debug/deps/integration_test-[..][EXE]",
"features": [],
Expand Down

0 comments on commit 7d188fa

Please sign in to comment.