Skip to content

Commit ad50d0d

Browse files
committed
Auto merge of #10082 - basile-henry:basile/alias-shadow-external-warning, r=ehuss
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. Should the warning mention that this will become a hard error?
2 parents adf601d + 0b4e2ca commit ad50d0d

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

src/bin/cargo/cli.rs

+15
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,21 @@ 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+
"\
271+
user-defined alias `{}` is shadowing an external subcommand found at: `{}`
272+
This was previously accepted but is being phased out; it will become a hard error in a future release.
273+
For more information, see issue #10049 <https://github.com/rust-lang/cargo/issues/10049>.",
274+
cmd,
275+
path.display(),
276+
))?;
277+
}
278+
264279
alias.extend(
265280
args.values_of("")
266281
.unwrap_or_default()

src/bin/cargo/main.rs

+7-3
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 => {

tests/testsuite/cargo_alias_config.rs

+45-3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,38 @@ fn dependent_alias() {
7676
.run();
7777
}
7878

79+
#[cargo_test]
80+
fn alias_shadowing_external_subcommand() {
81+
let echo = echo_subcommand();
82+
let p = project()
83+
.file("Cargo.toml", &basic_bin_manifest("foo"))
84+
.file("src/main.rs", "fn main() {}")
85+
.file(
86+
".cargo/config",
87+
r#"
88+
[alias]
89+
echo = "build"
90+
"#,
91+
)
92+
.build();
93+
94+
let mut paths: Vec<_> = env::split_paths(&env::var_os("PATH").unwrap_or_default()).collect();
95+
paths.push(echo.target_debug_dir());
96+
let path = env::join_paths(paths).unwrap();
97+
98+
p.cargo("echo")
99+
.env("PATH", &path)
100+
.with_stderr("\
101+
[WARNING] user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo[EXE]`
102+
This was previously accepted but is being phased out; it will become a hard error in a future release.
103+
For more information, see issue #10049 <https://github.com/rust-lang/cargo/issues/10049>.
104+
[COMPILING] foo v0.5.0 [..]
105+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
106+
",
107+
)
108+
.run();
109+
}
110+
79111
#[cargo_test]
80112
fn default_args_alias() {
81113
let echo = echo_subcommand();
@@ -100,14 +132,24 @@ fn default_args_alias() {
100132
p.cargo("echo")
101133
.env("PATH", &path)
102134
.with_status(101)
103-
.with_stderr("error: alias echo has unresolvable recursive definition: echo -> echo")
135+
.with_stderr("\
136+
[WARNING] user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo[EXE]`
137+
This was previously accepted but is being phased out; it will become a hard error in a future release.
138+
For more information, see issue #10049 <https://github.com/rust-lang/cargo/issues/10049>.
139+
error: alias echo has unresolvable recursive definition: echo -> echo
140+
",
141+
)
104142
.run();
105143

106144
p.cargo("test-1")
107145
.env("PATH", &path)
108146
.with_status(101)
109-
.with_stderr(
110-
"error: alias test-1 has unresolvable recursive definition: test-1 -> echo -> echo",
147+
.with_stderr("\
148+
[WARNING] user-defined alias `echo` is shadowing an external subcommand found at: `[ROOT]/cargo-echo/target/debug/cargo-echo[EXE]`
149+
This was previously accepted but is being phased out; it will become a hard error in a future release.
150+
For more information, see issue #10049 <https://github.com/rust-lang/cargo/issues/10049>.
151+
error: alias test-1 has unresolvable recursive definition: test-1 -> echo -> echo
152+
",
111153
)
112154
.run();
113155

0 commit comments

Comments
 (0)