Skip to content

Commit b1522d7

Browse files
committed
high priority config first
1 parent 7c3904d commit b1522d7

File tree

6 files changed

+68
-43
lines changed

6 files changed

+68
-43
lines changed

src/cargo/util/config/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ impl Config {
938938
.map(|s| (s.to_string(), def.clone())),
939939
);
940940
}
941+
output.sort_by(|a, b| a.1.cmp(&b.1));
941942
Ok(())
942943
}
943944

@@ -2106,16 +2107,22 @@ impl ConfigValue {
21062107

21072108
/// Merge the given value into self.
21082109
///
2109-
/// If `force` is true, primitive (non-container) types will override existing values.
2110-
/// If false, the original will be kept and the new value ignored.
2110+
/// If `force` is true, primitive (non-container) types will override existing values
2111+
/// of equal priority. For arrays, incoming values of equal priority will be placed first.
21112112
///
21122113
/// Container types (tables and arrays) are merged with existing values.
21132114
///
21142115
/// Container and non-container types cannot be mixed.
21152116
fn merge(&mut self, from: ConfigValue, force: bool) -> CargoResult<()> {
21162117
match (self, from) {
21172118
(&mut CV::List(ref mut old, _), CV::List(ref mut new, _)) => {
2118-
old.extend(mem::take(new).into_iter());
2119+
if force {
2120+
new.append(old);
2121+
mem::swap(new, old);
2122+
} else {
2123+
old.append(new);
2124+
}
2125+
old.sort_by(|a, b| a.1.cmp(&b.1));
21192126
}
21202127
(&mut CV::Table(ref mut old, _), CV::Table(ref mut new, _)) => {
21212128
for (key, value) in mem::take(new) {

src/cargo/util/config/value.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
use crate::util::config::Config;
1212
use serde::de;
13+
use std::cmp::Ordering;
1314
use std::fmt;
1415
use std::marker;
1516
use std::mem;
@@ -63,6 +64,24 @@ pub enum Definition {
6364
Cli(Option<PathBuf>),
6465
}
6566

67+
impl PartialOrd for Definition {
68+
fn partial_cmp(&self, other: &Definition) -> Option<Ordering> {
69+
Some(self.cmp(other))
70+
}
71+
}
72+
73+
impl Ord for Definition {
74+
fn cmp(&self, other: &Definition) -> Ordering {
75+
if mem::discriminant(self) == mem::discriminant(other) {
76+
Ordering::Equal
77+
} else if self.is_higher_priority(other) {
78+
Ordering::Less
79+
} else {
80+
Ordering::Greater
81+
}
82+
}
83+
}
84+
6685
impl Definition {
6786
/// Root directory where this is defined.
6887
///

tests/testsuite/cargo_config/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,10 @@ target.\"cfg(target_os = \\\"linux\\\")\".runner = \"runme\" # [ROOT]/home/.carg
280280
.with_stdout(
281281
"\
282282
build.rustflags = [
283-
\"--flag-directory\", # [ROOT]/foo/.cargo/config.toml
284-
\"--flag-global\", # [ROOT]/home/.cargo/config.toml
285283
\"env1\", # environment variable `CARGO_BUILD_RUSTFLAGS`
286284
\"env2\", # environment variable `CARGO_BUILD_RUSTFLAGS`
285+
\"--flag-directory\", # [ROOT]/foo/.cargo/config.toml
286+
\"--flag-global\", # [ROOT]/home/.cargo/config.toml
287287
]
288288
",
289289
)
@@ -310,12 +310,12 @@ fn show_origin_toml_cli() {
310310
.with_stdout(
311311
"\
312312
build.rustflags = [
313-
\"--flag-directory\", # [ROOT]/foo/.cargo/config.toml
314-
\"--flag-global\", # [ROOT]/home/.cargo/config.toml
315313
\"cli1\", # --config cli option
316314
\"cli2\", # --config cli option
317315
\"env1\", # environment variable `CARGO_BUILD_RUSTFLAGS`
318316
\"env2\", # environment variable `CARGO_BUILD_RUSTFLAGS`
317+
\"--flag-directory\", # [ROOT]/foo/.cargo/config.toml
318+
\"--flag-global\", # [ROOT]/home/.cargo/config.toml
319319
]
320320
",
321321
)
@@ -471,7 +471,7 @@ fn includes() {
471471
cargo_process("config get build.rustflags -Zunstable-options -Zconfig-include")
472472
.cwd(&sub_folder.parent().unwrap())
473473
.masquerade_as_nightly_cargo(&["cargo-config", "config-include"])
474-
.with_stdout(r#"build.rustflags = ["--flag-other", "--flag-directory", "--flag-global"]"#)
474+
.with_stdout(r#"build.rustflags = ["--flag-directory", "--flag-other", "--flag-global"]"#)
475475
.with_stderr("")
476476
.run();
477477

@@ -481,8 +481,8 @@ fn includes() {
481481
.with_stdout(
482482
"\
483483
build.rustflags = [
484-
\"--flag-other\", # [ROOT]/foo/.cargo/other.toml
485484
\"--flag-directory\", # [ROOT]/foo/.cargo/config.toml
485+
\"--flag-other\", # [ROOT]/foo/.cargo/other.toml
486486
\"--flag-global\", # [ROOT]/home/.cargo/config.toml
487487
]
488488
",

tests/testsuite/config.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ c = ['c']
481481
);
482482
assert_eq!(
483483
config.get::<VSOB>("c").unwrap(),
484-
VSOB::VecString(vec!["c".to_string(), "d".to_string()])
484+
VSOB::VecString(vec!["d".to_string(), "c".to_string()])
485485
);
486486
assert_eq!(config.get::<VSOB>("envb").unwrap(), VSOB::Bool(false));
487487
assert_eq!(
@@ -508,11 +508,11 @@ Caused by:
508508
.build();
509509
assert_eq!(
510510
config.get::<VSOB>("b").unwrap(),
511-
VSOB::VecString(vec!["b".to_string(), "d".to_string(), "e".to_string()])
511+
VSOB::VecString(vec!["d".to_string(), "e".to_string(), "b".to_string()])
512512
);
513513
assert_eq!(
514514
config.get::<VSOB>("c").unwrap(),
515-
VSOB::VecString(vec!["c".to_string(), "f".to_string(), "g".to_string()])
515+
VSOB::VecString(vec!["f".to_string(), "g".to_string(), "c".to_string()])
516516
);
517517

518518
// config-cli
@@ -540,19 +540,19 @@ expected boolean, but found array",
540540
assert_eq!(
541541
config.get::<VSOB>("b").unwrap(),
542542
VSOB::VecString(vec![
543-
"b".to_string(),
544543
"clib".to_string(),
545544
"env1".to_string(),
546-
"env2".to_string()
545+
"env2".to_string(),
546+
"b".to_string(),
547547
])
548548
);
549549
assert_eq!(
550550
config.get::<VSOB>("c").unwrap(),
551551
VSOB::VecString(vec![
552-
"c".to_string(),
553552
"clic".to_string(),
554553
"e1".to_string(),
555-
"e2".to_string()
554+
"e2".to_string(),
555+
"c".to_string(),
556556
])
557557
);
558558
}
@@ -780,7 +780,7 @@ expected a list, but found a integer for `l3` in [..]/.cargo/config",
780780
);
781781
assert_eq!(
782782
config.get::<L>("l4").unwrap(),
783-
vec!["one", "two", "three", "four"]
783+
vec!["three", "four", "one", "two"]
784784
);
785785
assert_eq!(config.get::<L>("l5").unwrap(), vec!["a"]);
786786
assert_eq!(config.get::<L>("env-empty").unwrap(), vec![] as Vec<String>);
@@ -814,10 +814,10 @@ expected `]`
814814
.get::<(String, String, String, String)>("l4")
815815
.unwrap(),
816816
(
817-
"one".to_string(),
818-
"two".to_string(),
819817
"three".to_string(),
820-
"four".to_string()
818+
"four".to_string(),
819+
"one".to_string(),
820+
"two".to_string()
821821
)
822822
);
823823
assert_eq!(config.get::<(String,)>("l5").unwrap(), ("a".to_string(),));
@@ -845,7 +845,7 @@ expected `]`
845845
assert_eq!(
846846
config.get::<S>("nested2").unwrap(),
847847
S {
848-
l: Some(vec!["y".to_string(), "z".to_string()]),
848+
l: Some(vec!["z".to_string(), "y".to_string()]),
849849
}
850850
);
851851
assert_eq!(
@@ -1582,17 +1582,17 @@ known-hosts = [
15821582
.as_ref()
15831583
.unwrap();
15841584
assert_eq!(kh.len(), 4);
1585-
assert_eq!(kh[0].val, "example.org ...");
1586-
assert_eq!(kh[0].definition, Definition::Path(foo_path.clone()));
1587-
assert_eq!(kh[1].val, "example.com ...");
1588-
assert_eq!(kh[1].definition, Definition::Path(root_path.clone()));
1589-
assert_eq!(kh[2].val, "example.net ...");
1590-
assert_eq!(kh[2].definition, Definition::Path(root_path.clone()));
1591-
assert_eq!(kh[3].val, "env-example");
1585+
assert_eq!(kh[0].val, "env-example");
15921586
assert_eq!(
1593-
kh[3].definition,
1587+
kh[0].definition,
15941588
Definition::Environment("CARGO_NET_SSH_KNOWN_HOSTS".to_string())
15951589
);
1590+
assert_eq!(kh[1].val, "example.org ...");
1591+
assert_eq!(kh[1].definition, Definition::Path(foo_path.clone()));
1592+
assert_eq!(kh[2].val, "example.com ...");
1593+
assert_eq!(kh[2].definition, Definition::Path(root_path.clone()));
1594+
assert_eq!(kh[3].val, "example.net ...");
1595+
assert_eq!(kh[3].definition, Definition::Path(root_path.clone()));
15961596
}
15971597

15981598
#[cargo_test]

tests/testsuite/config_cli.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,18 @@ fn merges_array() {
135135
.build();
136136
assert_eq!(
137137
config.get::<Vec<String>>("build.rustflags").unwrap(),
138-
["--file", "--cli"]
138+
["--cli", "--file"]
139139
);
140140

141141
// With normal env.
142142
let config = ConfigBuilder::new()
143143
.env("CARGO_BUILD_RUSTFLAGS", "--env1 --env2")
144144
.config_arg("build.rustflags = ['--cli']")
145145
.build();
146-
// The order of cli/env is a little questionable here, but would require
147-
// much more complex merging logic.
146+
148147
assert_eq!(
149148
config.get::<Vec<String>>("build.rustflags").unwrap(),
150-
["--file", "--cli", "--env1", "--env2"]
149+
["--cli", "--env1", "--env2", "--file"]
151150
);
152151

153152
// With advanced-env.
@@ -158,7 +157,7 @@ fn merges_array() {
158157
.build();
159158
assert_eq!(
160159
config.get::<Vec<String>>("build.rustflags").unwrap(),
161-
["--file", "--cli", "--env"]
160+
["--cli", "--env", "--file"]
162161
);
163162

164163
// Merges multiple instances.
@@ -168,7 +167,7 @@ fn merges_array() {
168167
.build();
169168
assert_eq!(
170169
config.get::<Vec<String>>("build.rustflags").unwrap(),
171-
["--file", "--one", "--two"]
170+
["--two", "--one", "--file"]
172171
);
173172
}
174173

@@ -189,7 +188,7 @@ fn string_list_array() {
189188
.get::<cargo::util::config::StringList>("build.rustflags")
190189
.unwrap()
191190
.as_slice(),
192-
["--file", "--cli"]
191+
["--cli", "--file"]
193192
);
194193

195194
// With normal env.
@@ -202,7 +201,7 @@ fn string_list_array() {
202201
.get::<cargo::util::config::StringList>("build.rustflags")
203202
.unwrap()
204203
.as_slice(),
205-
["--file", "--cli", "--env1", "--env2"]
204+
["--cli", "--env1", "--env2", "--file"]
206205
);
207206

208207
// With advanced-env.
@@ -216,7 +215,7 @@ fn string_list_array() {
216215
.get::<cargo::util::config::StringList>("build.rustflags")
217216
.unwrap()
218217
.as_slice(),
219-
["--file", "--cli", "--env"]
218+
["--cli", "--env", "--file"]
220219
);
221220
}
222221

@@ -278,10 +277,10 @@ fn merge_array_mixed_def_paths() {
278277
// The definition for the root value is somewhat arbitrary, but currently starts with the file because that is what is loaded first.
279278
assert_eq!(paths.definition, Definition::Path(paths::root()));
280279
assert_eq!(paths.val.len(), 2);
281-
assert_eq!(paths.val[0].0, "file");
282-
assert_eq!(paths.val[0].1.root(&config), paths::root());
283-
assert_eq!(paths.val[1].0, "cli");
284-
assert_eq!(paths.val[1].1.root(&config), somedir);
280+
assert_eq!(paths.val[0].0, "cli");
281+
assert_eq!(paths.val[0].1.root(&config), somedir);
282+
assert_eq!(paths.val[1].0, "file");
283+
assert_eq!(paths.val[1].1.root(&config), paths::root());
285284
}
286285

287286
#[cargo_test]

tests/testsuite/config_include.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn works_with_cli() {
7676
"\
7777
[DIRTY] foo v0.0.1 ([..]): the rustflags changed
7878
[CHECKING] foo v0.0.1 [..]
79-
[RUNNING] `rustc [..]-W unsafe-code -W unused`
79+
[RUNNING] `rustc [..]-W unused -W unsafe-code`
8080
[FINISHED] [..]
8181
",
8282
)

0 commit comments

Comments
 (0)