Skip to content

Commit

Permalink
Propagate unrecoverable error to users when resolving aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Sep 14, 2022
1 parent 144f5a4 commit 79439b8
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![warn(rust_2018_idioms)] // while we're getting used to 2018
#![allow(clippy::all)]

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

/// Resolve the aliased command from the [`Config`] with a given command string.
///
/// The search fallback chain is:
///
/// 1. Get the aliased command as a string.
/// 2. If the aliased command is not a string, get it as an array.
/// 3. If cannot find any, finds one insides [`BUILTIN_ALIASES`].
/// 4. If still no command is found, return `None`.
fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<String>>> {
let alias_name = format!("alias.{}", command);
let user_alias = match config.get_string(&alias_name) {
Ok(Some(record)) => Some(
let alias_name = format!("alias.{command}");
let user_alias = match config.get::<Value<String>>(&alias_name) {
Ok(record) => Some(
record
.val
.split_whitespace()
.map(|s| s.to_string())
.collect(),
),
Ok(None) => None,
Err(_) => config.get::<Option<Vec<String>>>(&alias_name)?,
Err(e) => match e.downcast_ref::<ConfigError>().map(|e| e.kind()) {
// Just report we cannot find anything.
Some(ConfigErrorKind::MissingKey) => None,
// Alias might be set as an toml array, fallback and try it.
Some(ConfigErrorKind::UnexpectedValue) => config.get::<Option<_>>(&alias_name)?,
// A unrecoverable error, e.g. TOML parsing error. Relay it to the user.
Some(ConfigErrorKind::Custom) | None => return Err(e),
},
};

let result = user_alias.or_else(|| {
Expand Down

0 comments on commit 79439b8

Please sign in to comment.