From 5bfd345e1d3c5bbf840dd61a015db254abec15e9 Mon Sep 17 00:00:00 2001 From: Basile Henry Date: Sun, 14 Nov 2021 19:04:25 +0100 Subject: [PATCH 1/6] 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. --- src/bin/cargo/cli.rs | 12 ++++++++++++ src/bin/cargo/main.rs | 10 +++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index e0df4523cf0..735f0aa8a30 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -261,6 +261,18 @@ fn expand_aliases( } (None, None) => {} (_, Some(mut alias)) => { + // Check if this alias is shadowing an external subcommand + // (binary of the form `cargo-`) + // Currently this is only a warning, but after a transition period this will become + // a hard error. + if let Some(path) = super::find_external_subcommand(config, cmd) { + config.shell().warn(format!( + "user-defined alias `{}` is shadowing an external subcommand found at: `{}`", + cmd, + path.display(), + ))?; + } + alias.extend( args.values_of("") .unwrap_or_default() diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index 58056aa8375..57895b766ec 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -147,12 +147,16 @@ fn list_commands(config: &Config) -> BTreeMap { commands } -fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> CliResult { +fn find_external_subcommand(config: &Config, cmd: &str) -> Option { let command_exe = format!("cargo-{}{}", cmd, env::consts::EXE_SUFFIX); - let path = search_directories(config) + search_directories(config) .iter() .map(|dir| dir.join(&command_exe)) - .find(|file| is_executable(file)); + .find(|file| is_executable(file)) +} + +fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> CliResult { + let path = find_external_subcommand(config, cmd); let command = match path { Some(command) => command, None => { From 9f768bbcd85995ea02e05dd7ac411ce017ff7461 Mon Sep 17 00:00:00 2001 From: Basile Henry Date: Sun, 14 Nov 2021 20:38:40 +0100 Subject: [PATCH 2/6] Update test for warning for alias shadowing an external subcommand --- tests/testsuite/cargo_alias_config.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/testsuite/cargo_alias_config.rs b/tests/testsuite/cargo_alias_config.rs index 6e2804fd200..9509fd51763 100644 --- a/tests/testsuite/cargo_alias_config.rs +++ b/tests/testsuite/cargo_alias_config.rs @@ -100,14 +100,20 @@ fn default_args_alias() { p.cargo("echo") .env("PATH", &path) .with_status(101) - .with_stderr("error: alias echo has unresolvable recursive definition: echo -> echo") + .with_stderr("\ +warning: user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo` +error: alias echo has unresolvable recursive definition: echo -> echo +", + ) .run(); p.cargo("test-1") .env("PATH", &path) .with_status(101) - .with_stderr( - "error: alias test-1 has unresolvable recursive definition: test-1 -> echo -> echo", + .with_stderr("\ +warning: user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo` +error: alias test-1 has unresolvable recursive definition: test-1 -> echo -> echo +", ) .run(); From 2ead2d9e3dc829b82ea1017dd5374c8c76ea0615 Mon Sep 17 00:00:00 2001 From: Basile Henry Date: Sun, 14 Nov 2021 20:22:05 +0000 Subject: [PATCH 3/6] Update tests/testsuite/cargo_alias_config.rs --- tests/testsuite/cargo_alias_config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testsuite/cargo_alias_config.rs b/tests/testsuite/cargo_alias_config.rs index 9509fd51763..b26524a67e7 100644 --- a/tests/testsuite/cargo_alias_config.rs +++ b/tests/testsuite/cargo_alias_config.rs @@ -101,7 +101,7 @@ fn default_args_alias() { .env("PATH", &path) .with_status(101) .with_stderr("\ -warning: user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo` +warning: user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo[EXE]` error: alias echo has unresolvable recursive definition: echo -> echo ", ) From 3a5bb75467902181ecc281f5e8bc164cf9ccd9d4 Mon Sep 17 00:00:00 2001 From: Basile Henry Date: Sun, 14 Nov 2021 20:22:13 +0000 Subject: [PATCH 4/6] Update tests/testsuite/cargo_alias_config.rs --- tests/testsuite/cargo_alias_config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testsuite/cargo_alias_config.rs b/tests/testsuite/cargo_alias_config.rs index b26524a67e7..6194f96d678 100644 --- a/tests/testsuite/cargo_alias_config.rs +++ b/tests/testsuite/cargo_alias_config.rs @@ -111,7 +111,7 @@ error: alias echo has unresolvable recursive definition: echo -> echo .env("PATH", &path) .with_status(101) .with_stderr("\ -warning: user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo` +warning: user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo[EXE]` error: alias test-1 has unresolvable recursive definition: test-1 -> echo -> echo ", ) From cba8503e3ad310abd680b4987be1b99154c90e3d Mon Sep 17 00:00:00 2001 From: Basile Henry Date: Wed, 17 Nov 2021 09:22:08 +0100 Subject: [PATCH 5/6] Add details in warning for alias shadowing external subcommands --- src/bin/cargo/cli.rs | 5 ++++- tests/testsuite/cargo_alias_config.rs | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 735f0aa8a30..26d51984d2a 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -267,7 +267,10 @@ fn expand_aliases( // a hard error. if let Some(path) = super::find_external_subcommand(config, cmd) { config.shell().warn(format!( - "user-defined alias `{}` is shadowing an external subcommand found at: `{}`", + "\ +user-defined alias `{}` is shadowing an external subcommand found at: `{}` +This was previously accepted but is being phased out; it will become a hard error in a future release. +For more information, see issue #10049 .", cmd, path.display(), ))?; diff --git a/tests/testsuite/cargo_alias_config.rs b/tests/testsuite/cargo_alias_config.rs index 6194f96d678..6d6a82b3185 100644 --- a/tests/testsuite/cargo_alias_config.rs +++ b/tests/testsuite/cargo_alias_config.rs @@ -101,7 +101,9 @@ fn default_args_alias() { .env("PATH", &path) .with_status(101) .with_stderr("\ -warning: user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo[EXE]` +[WARNING] user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo[EXE]` +This was previously accepted but is being phased out; it will become a hard error in a future release. +For more information, see issue #10049 . error: alias echo has unresolvable recursive definition: echo -> echo ", ) @@ -111,7 +113,9 @@ error: alias echo has unresolvable recursive definition: echo -> echo .env("PATH", &path) .with_status(101) .with_stderr("\ -warning: user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo[EXE]` +[WARNING] user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo[EXE]` +This was previously accepted but is being phased out; it will become a hard error in a future release. +For more information, see issue #10049 . error: alias test-1 has unresolvable recursive definition: test-1 -> echo -> echo ", ) From 0b4e2ca330b7dd536d57990ec3f5f090810b5bc9 Mon Sep 17 00:00:00 2001 From: Basile Henry Date: Wed, 17 Nov 2021 09:23:06 +0100 Subject: [PATCH 6/6] Add test for alias shadowing external subcommand This is currently still permitted, so we only test that the warning is properly issued --- tests/testsuite/cargo_alias_config.rs | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/testsuite/cargo_alias_config.rs b/tests/testsuite/cargo_alias_config.rs index 6d6a82b3185..6777aeabf84 100644 --- a/tests/testsuite/cargo_alias_config.rs +++ b/tests/testsuite/cargo_alias_config.rs @@ -76,6 +76,38 @@ fn dependent_alias() { .run(); } +#[cargo_test] +fn alias_shadowing_external_subcommand() { + let echo = echo_subcommand(); + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/main.rs", "fn main() {}") + .file( + ".cargo/config", + r#" + [alias] + echo = "build" + "#, + ) + .build(); + + let mut paths: Vec<_> = env::split_paths(&env::var_os("PATH").unwrap_or_default()).collect(); + paths.push(echo.target_debug_dir()); + let path = env::join_paths(paths).unwrap(); + + p.cargo("echo") + .env("PATH", &path) + .with_stderr("\ +[WARNING] user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo[EXE]` +This was previously accepted but is being phased out; it will become a hard error in a future release. +For more information, see issue #10049 . +[COMPILING] foo v0.5.0 [..] +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); +} + #[cargo_test] fn default_args_alias() { let echo = echo_subcommand();