Skip to content

Commit 5bfd345

Browse files
committed
Warn when alias shadows external subcommand
As per #10049, we start by emitting a warning when an alias shadows an existing external subcommand. After a transition period (duration not specified), we will make this a hard error.
1 parent 3a3a071 commit 5bfd345

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/bin/cargo/cli.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,18 @@ fn expand_aliases(
261261
}
262262
(None, None) => {}
263263
(_, Some(mut alias)) => {
264+
// Check if this alias is shadowing an external subcommand
265+
// (binary of the form `cargo-<subcommand>`)
266+
// Currently this is only a warning, but after a transition period this will become
267+
// a hard error.
268+
if let Some(path) = super::find_external_subcommand(config, cmd) {
269+
config.shell().warn(format!(
270+
"user-defined alias `{}` is shadowing an external subcommand found at: `{}`",
271+
cmd,
272+
path.display(),
273+
))?;
274+
}
275+
264276
alias.extend(
265277
args.values_of("")
266278
.unwrap_or_default()

src/bin/cargo/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,16 @@ fn list_commands(config: &Config) -> BTreeMap<String, CommandInfo> {
147147
commands
148148
}
149149

150-
fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> CliResult {
150+
fn find_external_subcommand(config: &Config, cmd: &str) -> Option<PathBuf> {
151151
let command_exe = format!("cargo-{}{}", cmd, env::consts::EXE_SUFFIX);
152-
let path = search_directories(config)
152+
search_directories(config)
153153
.iter()
154154
.map(|dir| dir.join(&command_exe))
155-
.find(|file| is_executable(file));
155+
.find(|file| is_executable(file))
156+
}
157+
158+
fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> CliResult {
159+
let path = find_external_subcommand(config, cmd);
156160
let command = match path {
157161
Some(command) => command,
158162
None => {

0 commit comments

Comments
 (0)