From f262297e9aed013ca61a891951f639b85788ef14 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 19 Sep 2023 10:46:05 +0800 Subject: [PATCH 1/5] Add test for using unsupported mode in build command --- tests/testsuite/build.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 3c650ca5213..b5ad0836958 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -135,6 +135,27 @@ fn incremental_config() { .run(); } +#[cargo_test] +fn cargo_compile_with_unsupported_mode() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) + .build(); + + p.cargo("build --debug") + .with_stderr( + "\ +error: unexpected argument '--debug' found + +Usage: cargo[EXE] build [OPTIONS] + +For more information, try '--help'. +", + ) + .with_status(1) + .run(); +} + #[cargo_test] fn cargo_compile_with_workspace_excluded() { let p = project().file("src/main.rs", "fn main() {}").build(); From ed86337492b41713333490df7d8f12e79dad9960 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 19 Sep 2023 10:35:12 +0800 Subject: [PATCH 2/5] Better suggestion for unsupported mode in build command --- src/bin/cargo/commands/build.rs | 1 + src/cargo/util/command_prelude.rs | 21 +++++++++++++++++++-- tests/testsuite/build.rs | 2 ++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/bin/cargo/commands/build.rs b/src/bin/cargo/commands/build.rs index 7197cdbad35..8d94f3d1db0 100644 --- a/src/bin/cargo/commands/build.rs +++ b/src/bin/cargo/commands/build.rs @@ -30,6 +30,7 @@ pub fn cli() -> Command { ) .arg_features() .arg_release("Build artifacts in release mode, with optimizations") + .arg_unsupported_mode("debug", "build", "release") .arg_profile("Build artifacts with the specified profile") .arg_parallel() .arg_target_triple("Build for the target triple") diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index c8dc4df87c1..303f965a74e 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -112,6 +112,19 @@ pub trait CommandExt: Sized { self._arg(flag("keep-going", "").value_parser(value_parser).hide(true)) } + fn arg_unsupported_mode( + self, + want: &'static str, + command: &'static str, + actual: &'static str, + ) -> Self { + let msg = format!( + "There is no `--{want}` for `cargo {command}`. Only `--{actual}` is supported." + ); + let value_parser = UnknownArgumentValueParser::suggest(msg); + self._arg(flag(want, "").value_parser(value_parser).hide(true)) + } + fn arg_targets_all( self, lib: &'static str, @@ -486,7 +499,7 @@ Run `{cmd}` to see possible targets." (Some(name @ ("dev" | "test" | "bench" | "check")), ProfileChecking::LegacyRustc) // `cargo fix` and `cargo check` has legacy handling of this profile name | (Some(name @ "test"), ProfileChecking::LegacyTestOnly) => { - if self.flag("release") { + if self.maybe_flag("release") { config.shell().warn( "the `--release` flag should not be specified with the `--profile` flag\n\ The `--release` flag will be ignored.\n\ @@ -510,7 +523,11 @@ Run `{cmd}` to see possible targets." ) }; - let name = match (self.flag("release"), self.flag("debug"), specified_profile) { + let name = match ( + self.maybe_flag("release"), + self.flag("debug"), + specified_profile, + ) { (false, false, None) => default, (true, _, None | Some("release")) => "release", (true, _, Some(name)) => return Err(conflict("release", "release", name)), diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index b5ad0836958..cb8c274939f 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -147,6 +147,8 @@ fn cargo_compile_with_unsupported_mode() { "\ error: unexpected argument '--debug' found + tip: There is no `--debug` for `cargo build`. Only `--release` is supported. + Usage: cargo[EXE] build [OPTIONS] For more information, try '--help'. From 7644a4bd969d5b3913c87d3dcbe0eca0bbda57f2 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 19 Sep 2023 10:52:27 +0800 Subject: [PATCH 3/5] Add test for using unsupported mode in install command --- tests/testsuite/install.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index c7658037271..ccd9be49bf0 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -2443,3 +2443,23 @@ fn ambiguous_registry_vs_local_package() { .run(); assert_has_installed_exe(cargo_home(), "foo"); } + +#[cargo_test] +fn install_with_unsupported_mode() { + pkg("foo", "0.0.1"); + + cargo_process("install foo --release") + .with_stderr( + "\ +error: unexpected argument '--release' found + + tip: a similar argument exists: '--examples' + +Usage: cargo[EXE] install --examples [crate]... + +For more information, try '--help'. +", + ) + .with_status(1) + .run(); +} From 817c2cbbb5b266440efcf4a1aa1a750d79694be9 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 19 Sep 2023 10:47:09 +0800 Subject: [PATCH 4/5] Better suggestion for unsupported mode in install command --- src/bin/cargo/commands/install.rs | 1 + src/cargo/util/command_prelude.rs | 2 +- tests/testsuite/install.rs | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 010a4f48385..57063f11dc8 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -83,6 +83,7 @@ pub fn cli() -> Command { "debug", "Build in debug mode (with the 'dev' profile) instead of release mode", )) + .arg_unsupported_mode("release", "install", "debug") .arg_profile("Install artifacts with the specified profile") .arg_target_triple("Build for the target triple") .arg_target_dir() diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 303f965a74e..270b558c6c2 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -525,7 +525,7 @@ Run `{cmd}` to see possible targets." let name = match ( self.maybe_flag("release"), - self.flag("debug"), + self.maybe_flag("debug"), specified_profile, ) { (false, false, None) => default, diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index ccd9be49bf0..b49cbec315e 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -2453,9 +2453,9 @@ fn install_with_unsupported_mode() { "\ error: unexpected argument '--release' found - tip: a similar argument exists: '--examples' + tip: There is no `--release` for `cargo install`. Only `--debug` is supported. -Usage: cargo[EXE] install --examples [crate]... +Usage: cargo[EXE] install [OPTIONS] [crate]... For more information, try '--help'. ", From 97656fbf8787551f002119b971c333cbfdb888ca Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 20 Sep 2023 19:32:35 +0800 Subject: [PATCH 5/5] Correct the helper name and the tip message --- src/bin/cargo/commands/build.rs | 2 +- src/bin/cargo/commands/install.rs | 2 +- src/cargo/util/command_prelude.rs | 12 +++++------- tests/testsuite/build.rs | 4 ++-- tests/testsuite/install.rs | 4 ++-- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/bin/cargo/commands/build.rs b/src/bin/cargo/commands/build.rs index 8d94f3d1db0..d503f43ddcc 100644 --- a/src/bin/cargo/commands/build.rs +++ b/src/bin/cargo/commands/build.rs @@ -30,7 +30,7 @@ pub fn cli() -> Command { ) .arg_features() .arg_release("Build artifacts in release mode, with optimizations") - .arg_unsupported_mode("debug", "build", "release") + .arg_redundant_default_mode("debug", "build", "release") .arg_profile("Build artifacts with the specified profile") .arg_parallel() .arg_target_triple("Build for the target triple") diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 57063f11dc8..2e5163bdc94 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -83,7 +83,7 @@ pub fn cli() -> Command { "debug", "Build in debug mode (with the 'dev' profile) instead of release mode", )) - .arg_unsupported_mode("release", "install", "debug") + .arg_redundant_default_mode("release", "install", "debug") .arg_profile("Install artifacts with the specified profile") .arg_target_triple("Build for the target triple") .arg_target_dir() diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 270b558c6c2..dcfabb3c259 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -112,17 +112,15 @@ pub trait CommandExt: Sized { self._arg(flag("keep-going", "").value_parser(value_parser).hide(true)) } - fn arg_unsupported_mode( + fn arg_redundant_default_mode( self, - want: &'static str, + default_mode: &'static str, command: &'static str, - actual: &'static str, + supported_mode: &'static str, ) -> Self { - let msg = format!( - "There is no `--{want}` for `cargo {command}`. Only `--{actual}` is supported." - ); + let msg = format!("`--{default_mode}` is the default for `cargo {command}`; instead `--{supported_mode}` is supported"); let value_parser = UnknownArgumentValueParser::suggest(msg); - self._arg(flag(want, "").value_parser(value_parser).hide(true)) + self._arg(flag(default_mode, "").value_parser(value_parser).hide(true)) } fn arg_targets_all( diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index cb8c274939f..1afa83918f0 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -136,7 +136,7 @@ fn incremental_config() { } #[cargo_test] -fn cargo_compile_with_unsupported_mode() { +fn cargo_compile_with_redundant_default_mode() { let p = project() .file("Cargo.toml", &basic_bin_manifest("foo")) .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) @@ -147,7 +147,7 @@ fn cargo_compile_with_unsupported_mode() { "\ error: unexpected argument '--debug' found - tip: There is no `--debug` for `cargo build`. Only `--release` is supported. + tip: `--debug` is the default for `cargo build`; instead `--release` is supported Usage: cargo[EXE] build [OPTIONS] diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index b49cbec315e..0a3670e6c8b 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -2445,7 +2445,7 @@ fn ambiguous_registry_vs_local_package() { } #[cargo_test] -fn install_with_unsupported_mode() { +fn install_with_redundant_default_mode() { pkg("foo", "0.0.1"); cargo_process("install foo --release") @@ -2453,7 +2453,7 @@ fn install_with_unsupported_mode() { "\ error: unexpected argument '--release' found - tip: There is no `--release` for `cargo install`. Only `--debug` is supported. + tip: `--release` is the default for `cargo install`; instead `--debug` is supported Usage: cargo[EXE] install [OPTIONS] [crate]...