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

Better suggestion for redundant mode in build and install commands #12693

Merged
merged 5 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/bin/cargo/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn cli() -> Command {
)
.arg_features()
.arg_release("Build artifacts in release mode, with optimizations")
.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")
Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub fn cli() -> Command {
"debug",
"Build in debug mode (with the 'dev' profile) instead of release mode",
))
.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()
Expand Down
19 changes: 17 additions & 2 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ pub trait CommandExt: Sized {
self._arg(flag("keep-going", "").value_parser(value_parser).hide(true))
}

fn arg_redundant_default_mode(
self,
default_mode: &'static str,
command: &'static str,
supported_mode: &'static str,
) -> Self {
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(default_mode, "").value_parser(value_parser).hide(true))
}

fn arg_targets_all(
self,
lib: &'static str,
Expand Down Expand Up @@ -486,7 +497,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\
Expand All @@ -510,7 +521,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.maybe_flag("debug"),
specified_profile,
) {
(false, false, None) => default,
(true, _, None | Some("release")) => "release",
(true, _, Some(name)) => return Err(conflict("release", "release", name)),
Expand Down
23 changes: 23 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ fn incremental_config() {
.run();
}

#[cargo_test]
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""#, &[]))
.build();

p.cargo("build --debug")
.with_stderr(
"\
error: unexpected argument '--debug' found

tip: `--debug` is the default for `cargo build`; instead `--release` is supported

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();
Expand Down
20 changes: 20 additions & 0 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2443,3 +2443,23 @@ fn ambiguous_registry_vs_local_package() {
.run();
assert_has_installed_exe(cargo_home(), "foo");
}

#[cargo_test]
fn install_with_redundant_default_mode() {
pkg("foo", "0.0.1");

cargo_process("install foo --release")
.with_stderr(
"\
error: unexpected argument '--release' found

tip: `--release` is the default for `cargo install`; instead `--debug` is supported

Usage: cargo[EXE] install [OPTIONS] [crate]...

For more information, try '--help'.
",
)
.with_status(1)
.run();
}