Skip to content

Commit 24feac2

Browse files
committed
feat(build-analysis): build-started log message
The build-started message ought to be pretty much the same as the header in timings HTML report, but some data is only available after the entire build or dependency resolution. Let's collect these data first.
1 parent 6d1c263 commit 24feac2

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use crate::ops::resolve::{SpecsAndResolvedFeatures, WorkspaceResolve};
5656
use crate::util::BuildLogger;
5757
use crate::util::context::{GlobalContext, WarningHandling};
5858
use crate::util::interning::InternedString;
59+
use crate::util::log_message::LogMessage;
5960
use crate::util::{CargoResult, StableHasher};
6061

6162
mod compile_filter;
@@ -156,9 +157,24 @@ pub fn compile_ws<'a>(
156157
exec: &Arc<dyn Executor>,
157158
) -> CargoResult<Compilation<'a>> {
158159
let interner = UnitInterner::new();
159-
let _logger = BuildLogger::maybe_new(ws)?;
160+
let logger = BuildLogger::maybe_new(ws)?;
161+
162+
if let Some(ref logger) = logger {
163+
let rustc = ws.gctx().load_global_rustc(Some(ws))?;
164+
logger.log(LogMessage::BuildStarted {
165+
cwd: ws.gctx().cwd().to_path_buf(),
166+
host: rustc.host.to_string(),
167+
jobs: options.build_config.jobs,
168+
profile: options.build_config.requested_profile.to_string(),
169+
rustc_version: rustc.version.to_string(),
170+
rustc_version_verbose: rustc.verbose_version.clone(),
171+
target_dir: ws.target_dir().as_path_unlocked().to_path_buf(),
172+
workspace_root: ws.root().to_path_buf(),
173+
});
174+
}
160175

161176
let bcx = create_bcx(ws, options, &interner)?;
177+
162178
if options.build_config.unit_graph {
163179
unit_graph::emit_serialized_unit_graph(&bcx.roots, &bcx.unit_graph, ws.gctx())?;
164180
return Compilation::new(&bcx);

src/cargo/util/log_message.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Messages for logging.
22
33
use std::io::Write;
4+
use std::path::PathBuf;
45

56
use jiff::Timestamp;
67
use serde::Serialize;
@@ -10,7 +11,19 @@ use serde::Serialize;
1011
/// Each variant represents a different type of event.
1112
#[derive(Serialize)]
1213
#[serde(tag = "reason", rename_all = "kebab-case")]
13-
pub enum LogMessage {}
14+
pub enum LogMessage {
15+
/// Emitted when a build starts.
16+
BuildStarted {
17+
cwd: PathBuf,
18+
host: String,
19+
jobs: u32,
20+
profile: String,
21+
rustc_version: String,
22+
rustc_version_verbose: String,
23+
target_dir: PathBuf,
24+
workspace_root: PathBuf,
25+
},
26+
}
1427

1528
impl LogMessage {
1629
/// Serializes this message as a JSON log line directly to the writer.

tests/testsuite/build_analysis.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use crate::prelude::*;
44

55
use cargo_test_support::basic_manifest;
6+
use cargo_test_support::compare::assert_e2e;
7+
use cargo_test_support::paths;
68
use cargo_test_support::project;
79
use cargo_test_support::str;
810

@@ -42,3 +44,51 @@ fn simple() {
4244
"#]])
4345
.run();
4446
}
47+
48+
#[cargo_test]
49+
fn log_msg_build_started() {
50+
let p = project()
51+
.file("Cargo.toml", &basic_manifest("foo", "0.0.0"))
52+
.file("src/lib.rs", "")
53+
.build();
54+
55+
p.cargo("check -Zbuild-analysis")
56+
.env("CARGO_BUILD_ANALYSIS_ENABLED", "true")
57+
.masquerade_as_nightly_cargo(&["build-analysis"])
58+
.run();
59+
60+
let cargo_home = paths::cargo_home();
61+
let log_dir = cargo_home.join("log");
62+
assert!(log_dir.exists());
63+
64+
let entries = std::fs::read_dir(&log_dir).unwrap();
65+
let log_file = entries
66+
.filter_map(Result::ok)
67+
.find(|e| e.path().extension().and_then(|s| s.to_str()) == Some("jsonl"))
68+
.unwrap();
69+
70+
let content = std::fs::read_to_string(log_file.path()).unwrap();
71+
72+
assert_e2e().eq(
73+
&content,
74+
str![[r#"
75+
[
76+
{
77+
"cwd": "[ROOT]/foo",
78+
"host": "[HOST_TARGET]",
79+
"jobs": "{...}",
80+
"profile": "dev",
81+
"reason": "build-started",
82+
"run_id": "[..]T[..]Z-[..]",
83+
"rustc_version": "1.[..]",
84+
"rustc_version_verbose": "{...}",
85+
"target_dir": "[ROOT]/foo/target",
86+
"timestamp": "[..]T[..]Z",
87+
"workspace_root": "[ROOT]/foo"
88+
}
89+
]
90+
"#]]
91+
.is_json()
92+
.against_jsonlines(),
93+
);
94+
}

0 commit comments

Comments
 (0)