fix(log): add `dependencies` field to `UnitRegistered` (#16448)
### What does this PR try to resolve?
Track unit dependency graph in `LogMessage::UnitRegistered`.
This gives the unit-graph information before running the build, which
will help enable root cause analysis for cascading rebuilds.
Part of <https://github.com/rust-lang/cargo/issues/15844>
### How to test and review this PR?
diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs
index d9aedc6..0640db9 100644
--- a/src/cargo/ops/cargo_compile/mod.rs
+++ b/src/cargo/ops/cargo_compile/mod.rs
@@ -535,6 +535,14 @@
for (index, unit) in units.into_iter().enumerate() {
let index = index as u64;
+ let dependencies = unit_graph
+ .get(unit)
+ .map(|deps| {
+ deps.iter()
+ .filter_map(|dep| unit_to_index.get(&dep.unit).copied())
+ .collect()
+ })
+ .unwrap_or_default();
logger.log(LogMessage::UnitRegistered {
package_id: unit.pkg.package_id().to_spec(),
target: (&unit.target).into(),
@@ -547,6 +555,7 @@
.map(|s| s.as_str().to_owned())
.collect(),
requested: root_unit_indexes.contains(&index),
+ dependencies,
});
}
let elapsed = ws.gctx().creation_time().elapsed().as_secs_f64();
diff --git a/src/cargo/ops/cargo_report/timings.rs b/src/cargo/ops/cargo_report/timings.rs
index c12ef48..5d64918 100644
--- a/src/cargo/ops/cargo_report/timings.rs
+++ b/src/cargo/ops/cargo_report/timings.rs
@@ -177,6 +177,7 @@
index,
features,
requested,
+ dependencies: _,
} => {
if requested {
requested_units.insert(index);
diff --git a/src/cargo/util/log_message.rs b/src/cargo/util/log_message.rs
index 77a24b3..d742de0 100644
--- a/src/cargo/util/log_message.rs
+++ b/src/cargo/util/log_message.rs
@@ -86,6 +86,9 @@
/// like via the `-p` flag or the default workspace members.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
requested: bool,
+ /// Unit indices that this unit depends on.
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ dependencies: Vec<u64>,
},
/// Emitted when a compilation unit starts.
UnitStarted {
diff --git a/tests/testsuite/build_analysis.rs b/tests/testsuite/build_analysis.rs
index cbaa68e..1b5fa06 100644
--- a/tests/testsuite/build_analysis.rs
+++ b/tests/testsuite/build_analysis.rs
@@ -606,6 +606,11 @@
"timestamp": "[..]T[..]Z"
},
{
+ "dependencies": [
+ 0,
+ 1,
+ 4
+ ],
"index": 2,
"mode": "doc",
"package_id": "path+[ROOTURL]/foo#0.0.0",
@@ -633,6 +638,9 @@
"timestamp": "[..]T[..]Z"
},
{
+ "dependencies": [
+ 3
+ ],
"index": 4,
"mode": "run-custom-build",
"package_id": "path+[ROOTURL]/foo#0.0.0",