Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if built-in subcommand aliases shadow user-defined aliases #6222

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use clap::{AppSettings, Arg, ArgMatches};

use cargo::{self, CliResult, Config};

use super::commands::{self, BuiltinExec};
use super::list_commands;
use super::commands;
use command_prelude::*;

pub fn main(config: &mut Config) -> CliResult {
Expand Down Expand Up @@ -118,11 +118,19 @@ fn expand_aliases(
.get_matches_from_safe(alias)?;
return expand_aliases(config, args);
}
(Some(_), Some(_)) => {
config.shell().warn(format!(
"alias `{}` is ignored, because it is shadowed by a built in command",
(Some(BuiltinExec { alias_for, .. }), Some(_)) => {
let mut message = format!(
"alias `{}` is ignored, because it is shadowed by a built-in ",
cmd
))?;
);

if let Some(alias_for) = alias_for {
message.push_str(&format!("alias for `{}`", alias_for));
} else {
message.push_str("command");
}

config.shell().warn(message)?;
}
(_, None) => {}
}
Expand Down Expand Up @@ -152,11 +160,12 @@ fn execute_subcommand(config: &mut Config, args: &ArgMatches) -> CliResult {
args.is_present("frozen"),
args.is_present("locked"),
arg_target_dir,
&args.values_of_lossy("unstable-features")
&args
.values_of_lossy("unstable-features")
.unwrap_or_default(),
)?;

if let Some(exec) = commands::builtin_exec(cmd) {
if let Some(BuiltinExec { exec, .. }) = commands::builtin_exec(cmd) {
return exec(config, subcommand_args);
}

Expand Down
3 changes: 2 additions & 1 deletion src/bin/cargo/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use cargo::ops;

pub fn cli() -> App {
subcommand("build")
.alias("b")
// subcommand aliases are handled in commands::builtin_exec() and cli::expand_aliases()
// .alias("b")
.about("Compile a local package and all of its dependencies")
.arg_package_spec(
"Package to build (see `cargo help pkgid`)",
Expand Down
22 changes: 19 additions & 3 deletions src/bin/cargo/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ pub fn builtin() -> Vec<App> {
]
}

pub fn builtin_exec(cmd: &str) -> Option<fn(&mut Config, &ArgMatches) -> CliResult> {
let f = match cmd {
pub struct BuiltinExec<'a> {
pub exec: fn(&'a mut Config, &'a ArgMatches) -> CliResult,
pub alias_for: Option<&'a str>,
}

pub fn builtin_exec(cmd: &str) -> Option<BuiltinExec> {
let exec = match cmd {
"bench" => bench::exec,
"build" => build::exec,
"b" => build::exec,
"check" => check::exec,
"clean" => clean::exec,
"doc" => doc::exec,
Expand All @@ -58,18 +64,28 @@ pub fn builtin_exec(cmd: &str) -> Option<fn(&mut Config, &ArgMatches) -> CliResu
"publish" => publish::exec,
"read-manifest" => read_manifest::exec,
"run" => run::exec,
"r" => run::exec,
"rustc" => rustc::exec,
"rustdoc" => rustdoc::exec,
"search" => search::exec,
"test" => test::exec,
"t" => test::exec,
"uninstall" => uninstall::exec,
"update" => update::exec,
"verify-project" => verify_project::exec,
"version" => version::exec,
"yank" => yank::exec,
_ => return None,
};
Some(f)

let alias_for = match cmd {
"b" => Some("build"),
"r" => Some("run"),
"t" => Some("test"),
_ => None,
};

Some(BuiltinExec { exec, alias_for })
}

pub mod bench;
Expand Down
3 changes: 2 additions & 1 deletion src/bin/cargo/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use cargo::ops::{self, CompileFilter};

pub fn cli() -> App {
subcommand("run")
.alias("r")
// subcommand aliases are handled in commands::builtin_exec() and cli::expand_aliases()
// .alias("r")
.setting(AppSettings::TrailingVarArg)
.about("Run the main binary of the local package (src/main.rs)")
.arg(Arg::with_name("args").multiple(true))
Expand Down
3 changes: 2 additions & 1 deletion src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use cargo::ops::{self, CompileFilter};

pub fn cli() -> App {
subcommand("test")
.alias("t")
// subcommand aliases are handled in commands::builtin_exec() and cli::expand_aliases()
// .alias("t")
.setting(AppSettings::TrailingVarArg)
.about("Execute all unit and integration tests of a local package")
.arg(
Expand Down
25 changes: 24 additions & 1 deletion tests/testsuite/cargo_alias_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,30 @@ fn cant_shadow_builtin() {
p.cargo("build")
.with_stderr(
"\
[WARNING] alias `build` is ignored, because it is shadowed by a built in command
[WARNING] alias `build` is ignored, because it is shadowed by a built-in command
[COMPILING] foo v0.5.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
).run();
}

#[test]
fn cant_shadow_builtin_alias() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/main.rs", "fn main() {}")
.file(
".cargo/config",
r#"
[alias]
b = "fetch"
"#,
).build();

p.cargo("b")
.with_stderr(
"\
[WARNING] alias `b` is ignored, because it is shadowed by a built-in alias for `build`
[COMPILING] foo v0.5.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
Expand Down