Skip to content

Commit 1505019

Browse files
committed
feat(log): emit build-started JSON message with run_id
When both `-Zbuild-analysis` and `--message-format=json` are enabled, emit a `build-started` JSON message to stdout containing the `run_id`. This allows external tools to associate the JSON output stream with the corresponding log file in `~/.cargo/log/{run_id}.jsonl`. While a better design of JSON message and structured logging is needed, this provides a temporary workaround for those needing the old `--timings=json` flag Closes #16576
1 parent 060402c commit 1505019

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ use crate::util::BuildLogger;
5858
use crate::util::context::{GlobalContext, WarningHandling};
5959
use crate::util::interning::InternedString;
6060
use crate::util::log_message::LogMessage;
61+
use crate::util::machine_message;
62+
use crate::util::machine_message::Message as _;
6163
use crate::util::{CargoResult, StableHasher};
6264

6365
mod compile_filter;
@@ -181,6 +183,12 @@ pub fn compile_ws<'a>(
181183
target_dir: ws.target_dir().as_path_unlocked().to_path_buf(),
182184
workspace_root: ws.root().to_path_buf(),
183185
});
186+
187+
if options.build_config.emit_json() {
188+
let run_id = logger.run_id().to_string();
189+
let msg = machine_message::BuildStarted { run_id: &run_id }.to_json_string();
190+
writeln!(ws.gctx().shell().out(), "{msg}")?;
191+
}
184192
}
185193

186194
let bcx = create_bcx(ws, options, &interner, logger.as_ref())?;

src/cargo/util/machine_message.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,14 @@ impl Message for BuildFinished {
103103
"build-finished"
104104
}
105105
}
106+
107+
#[derive(Serialize)]
108+
pub struct BuildStarted<'a> {
109+
pub run_id: &'a str,
110+
}
111+
112+
impl Message for BuildStarted<'_> {
113+
fn reason(&self) -> &str {
114+
"build-started"
115+
}
116+
}

tests/testsuite/build_analysis.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,10 @@ fn json_message_build_started_with_run_id() {
732732
.with_stdout_data(
733733
str![[r#"
734734
[
735+
{
736+
"reason": "build-started",
737+
"run_id": "[..]T[..]Z-[..]"
738+
},
735739
{
736740
"reason": "compiler-artifact",
737741
"...": "{...}"

0 commit comments

Comments
 (0)