@@ -30,7 +30,9 @@ use crate::core::dependency::{Artifact, ArtifactKind, ArtifactTarget, DepKind};
3030use crate :: core:: profiles:: { Profile , Profiles , UnitFor } ;
3131use crate :: core:: resolver:: Resolve ;
3232use 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+ } ;
3436use crate :: ops:: resolve_all_features;
3537use crate :: util:: GlobalContext ;
3638use 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
0 commit comments