Skip to content

Commit aca513a

Browse files
committed
Preserve order of build scripts
1 parent 8be51ef commit aca513a

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ use crate::core::dependency::{Artifact, ArtifactKind, ArtifactTarget, DepKind};
3030
use crate::core::profiles::{Profile, Profiles, UnitFor};
3131
use crate::core::resolver::Resolve;
3232
use crate::core::resolver::features::{FeaturesFor, ResolvedFeatures};
33-
use crate::core::{Dependency, Package, PackageId, PackageSet, Target, TargetKind, Workspace};
33+
use crate::core::{
34+
Dependency, Feature, Package, PackageId, PackageSet, Target, TargetKind, Workspace,
35+
};
3436
use crate::ops::resolve_all_features;
3537
use crate::util::GlobalContext;
3638
use crate::util::interning::InternedString;
@@ -142,8 +144,27 @@ pub fn build_unit_dependencies<'a, 'gctx>(
142144
// which affect the determinism of the build itself. As a result be sure
143145
// that dependency lists are always sorted to ensure we've always got a
144146
// deterministic output.
145-
for list in state.unit_dependencies.values_mut() {
146-
list.sort();
147+
for (unit, list) in &mut state.unit_dependencies {
148+
let is_multiple_build_scripts_enabled = unit
149+
.pkg
150+
.manifest()
151+
.unstable_features()
152+
.require(Feature::multiple_build_scripts())
153+
.is_ok();
154+
155+
if is_multiple_build_scripts_enabled {
156+
list.sort_by_key(|unit_dep| {
157+
if unit_dep.unit.target.is_custom_build() {
158+
// We do not sort build scripts to preserve the user-defined order.
159+
// In terms of determinism, we are assuming nothing interferes with order from when the user set it in `Cargo.toml` to here
160+
(0, None)
161+
} else {
162+
(1, Some(unit_dep.clone()))
163+
}
164+
});
165+
} else {
166+
list.sort();
167+
}
147168
}
148169
trace!("ALL UNIT DEPENDENCIES {:#?}", state.unit_dependencies);
149170

tests/testsuite/build_scripts_multiple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ fn build_script_with_conflicts_reverse_sorted() {
681681
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
682682
.with_status(0)
683683
.with_stdout_data(str![[r#"
684-
Hello, from Build Script 2!
684+
Hello, from Build Script 1!
685685
686686
"#]])
687687
.run();

0 commit comments

Comments
 (0)