From 40b9fec2550161c3ca257e04b9e6a2b13cef09c8 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 31 May 2024 09:18:57 -0500 Subject: [PATCH] fix(toml): Ensure targets are in a deterministic order With #13713, we enumerate all targets in `Cargo.toml` on `cargo publish` and `cargo vendor`. However, the order of the targets is non-determistic. This can be annoying for comparing the published `Cargo.toml` across releases but even worse is the churn it causes for `cargo vendor`. So we sort all the targets during publish. This keeps costs minimal with the following risks - If the non-determinism shows up in a way that affects developers during development - If there is a reason the user wants to control target order for explicit targets Fixes #13988 --- src/cargo/util/toml/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 088dd0196ad..9313a0ddd2c 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -2777,6 +2777,11 @@ fn prepare_targets_for_publish( }; prepared.push(target); } + // Ensure target order is deterministic, particularly for `cargo vendor` where re-vendoring + // shuld not cause changes. + // + // `unstable` should be deterministic because we enforce that `t.name` is unique + prepared.sort_unstable_by_key(|t| t.name.clone()); if prepared.is_empty() { Ok(None)