Skip to content

Commit 79439b8

Browse files
committed
Propagate unrecoverable error to users when resolving aliases
1 parent 144f5a4 commit 79439b8

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

Diff for: src/bin/cargo/main.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![warn(rust_2018_idioms)] // while we're getting used to 2018
22
#![allow(clippy::all)]
33

4+
use cargo::util::config::{ConfigError, ConfigErrorKind, Value};
45
use cargo::util::toml::StringOrVec;
56
use cargo::util::CliError;
67
use cargo::util::{self, closest_msg, command_prelude, CargoResult, CliResult, Config};
@@ -52,18 +53,32 @@ fn builtin_aliases_execs(cmd: &str) -> Option<&(&str, &str, &str)> {
5253
BUILTIN_ALIASES.iter().find(|alias| alias.0 == cmd)
5354
}
5455

56+
/// Resolve the aliased command from the [`Config`] with a given command string.
57+
///
58+
/// The search fallback chain is:
59+
///
60+
/// 1. Get the aliased command as a string.
61+
/// 2. If the aliased command is not a string, get it as an array.
62+
/// 3. If cannot find any, finds one insides [`BUILTIN_ALIASES`].
63+
/// 4. If still no command is found, return `None`.
5564
fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<String>>> {
56-
let alias_name = format!("alias.{}", command);
57-
let user_alias = match config.get_string(&alias_name) {
58-
Ok(Some(record)) => Some(
65+
let alias_name = format!("alias.{command}");
66+
let user_alias = match config.get::<Value<String>>(&alias_name) {
67+
Ok(record) => Some(
5968
record
6069
.val
6170
.split_whitespace()
6271
.map(|s| s.to_string())
6372
.collect(),
6473
),
65-
Ok(None) => None,
66-
Err(_) => config.get::<Option<Vec<String>>>(&alias_name)?,
74+
Err(e) => match e.downcast_ref::<ConfigError>().map(|e| e.kind()) {
75+
// Just report we cannot find anything.
76+
Some(ConfigErrorKind::MissingKey) => None,
77+
// Alias might be set as an toml array, fallback and try it.
78+
Some(ConfigErrorKind::UnexpectedValue) => config.get::<Option<_>>(&alias_name)?,
79+
// A unrecoverable error, e.g. TOML parsing error. Relay it to the user.
80+
Some(ConfigErrorKind::Custom) | None => return Err(e),
81+
},
6782
};
6883

6984
let result = user_alias.or_else(|| {

0 commit comments

Comments
 (0)