Skip to content

Commit ef7a4ef

Browse files
committed
Dont swallow errors when checking existence of a config key
1 parent 2c6647d commit ef7a4ef

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

Diff for: src/cargo/util/config/de.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<'de, 'config> de::Deserializer<'de> for Deserializer<'config> {
8080
where
8181
V: de::Visitor<'de>,
8282
{
83-
if self.config.has_key(&self.key, self.env_prefix_ok) {
83+
if self.config.has_key(&self.key, self.env_prefix_ok)? {
8484
visitor.visit_some(self)
8585
} else {
8686
// Treat missing values as `None`.

Diff for: src/cargo/util/config/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -682,25 +682,25 @@ impl Config {
682682
}
683683
}
684684

685-
fn has_key(&self, key: &ConfigKey, env_prefix_ok: bool) -> bool {
685+
/// Check if the [`Config`] contains a given [`ConfigKey`].
686+
///
687+
/// See `ConfigMapAccess` for a description of `env_prefix_ok`.
688+
fn has_key(&self, key: &ConfigKey, env_prefix_ok: bool) -> CargoResult<bool> {
686689
if self.env.contains_key(key.as_env_key()) {
687-
return true;
690+
return Ok(true);
688691
}
689-
// See ConfigMapAccess for a description of this.
690692
if env_prefix_ok {
691693
let env_prefix = format!("{}_", key.as_env_key());
692694
if self.env.keys().any(|k| k.starts_with(&env_prefix)) {
693-
return true;
695+
return Ok(true);
694696
}
695697
}
696-
if let Ok(o_cv) = self.get_cv(key) {
697-
if o_cv.is_some() {
698-
return true;
699-
}
698+
if self.get_cv(key)?.is_some() {
699+
return Ok(true);
700700
}
701701
self.check_environment_key_case_mismatch(key);
702702

703-
false
703+
Ok(false)
704704
}
705705

706706
fn check_environment_key_case_mismatch(&self, key: &ConfigKey) {

Diff for: tests/testsuite/bad_config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ fn bad1() {
1919
.with_status(101)
2020
.with_stderr(
2121
"\
22-
[ERROR] invalid configuration for key `target.nonexistent-target`
23-
expected a table, but found a string for `[..]` in [..]config
22+
[ERROR] expected table for configuration key `target.nonexistent-target`, \
23+
but found string in [..]config
2424
",
2525
)
2626
.run();

Diff for: tests/testsuite/cargo_alias_config.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn alias_incorrect_config_type() {
2121

2222
p.cargo("b-cargo-test -v")
2323
.with_status(101)
24-
.with_stderr_contains(
24+
.with_stderr(
2525
"\
2626
[ERROR] invalid configuration for key `alias.b-cargo-test`
2727
expected a list, but found a integer for [..]",
@@ -47,9 +47,21 @@ fn alias_malformed_config_string() {
4747
.with_status(101)
4848
.with_stderr(
4949
"\
50-
[ERROR] no such subcommand: `b-cargo-test`
50+
[ERROR] could not load Cargo configuration
51+
52+
Caused by:
53+
could not parse TOML configuration in `[..]/config`
54+
55+
Caused by:
56+
[..]
5157
52-
<tab>View all installed commands with `cargo --list`
58+
Caused by:
59+
TOML parse error at line [..]
60+
|
61+
3 | b-cargo-test = `
62+
| ^
63+
Unexpected ```
64+
Expected quoted string
5365
",
5466
)
5567
.run();
@@ -73,9 +85,19 @@ fn alias_malformed_config_list() {
7385
.with_status(101)
7486
.with_stderr(
7587
"\
76-
[ERROR] no such subcommand: `b-cargo-test`
88+
[ERROR] could not load Cargo configuration
89+
90+
Caused by:
91+
failed to load TOML configuration from `[..]/config`
92+
93+
Caused by:
94+
[..] `alias`
95+
96+
Caused by:
97+
[..] `b-cargo-test`
7798
78-
<tab>View all installed commands with `cargo --list`
99+
Caused by:
100+
expected string but found integer in list
79101
",
80102
)
81103
.run();

Diff for: tests/testsuite/config.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1074,10 +1074,6 @@ Dotted key `ssl-version` attempted to extend non-table type (string)
10741074
10751075
",
10761076
);
1077-
assert!(config
1078-
.get::<Option<SslVersionConfig>>("http.ssl-version")
1079-
.unwrap()
1080-
.is_none());
10811077
}
10821078

10831079
#[cargo_test]

0 commit comments

Comments
 (0)