Skip to content

Commit c1a2ac7

Browse files
committed
Make cargo version work even with malformed configurations
1 parent 79439b8 commit c1a2ac7

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/bin/cargo/cli.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
3030
// the [alias] table).
3131
let config = config.get_mut();
3232

33-
// Global args need to be extracted before expanding aliases because the
34-
// clap code for extracting a subcommand discards global options
35-
// (appearing before the subcommand).
3633
let (expanded_args, global_args) = expand_aliases(config, args, vec![])?;
3734

3835
if expanded_args
@@ -222,16 +219,30 @@ fn add_ssl(version_string: &mut String) {
222219
}
223220
}
224221

222+
/// Expands aliases recursively to collect all the command line arguments.
223+
///
224+
/// [`GlobalArgs`] need to be extracted before expanding aliases because the
225+
/// clap code for extracting a subcommand discards global options
226+
/// (appearing before the subcommand).
225227
fn expand_aliases(
226228
config: &mut Config,
227229
args: ArgMatches,
228230
mut already_expanded: Vec<String>,
229231
) -> Result<(ArgMatches, GlobalArgs), CliError> {
230232
if let Some((cmd, args)) = args.subcommand() {
231-
match (
232-
commands::builtin_exec(cmd),
233-
super::aliased_command(config, cmd)?,
234-
) {
233+
let exec = commands::builtin_exec(cmd);
234+
let aliased_cmd = super::aliased_command(config, cmd).or_else(|e| {
235+
// HACK: `cargo version` must always work even with malformed configs.
236+
// Here we treat the error as the aliased command not found,
237+
// so it falls back to call builtin `cargo version`
238+
if cmd == "version" {
239+
Ok(None)
240+
} else {
241+
Err(e)
242+
}
243+
})?;
244+
245+
match (exec, aliased_cmd) {
235246
(Some(_), Some(_)) => {
236247
// User alias conflicts with a built-in subcommand
237248
config.shell().warn(format!(

0 commit comments

Comments
 (0)