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

fix: using --release/debug and --profile together becomes an error #13971

Merged
merged 2 commits into from
Jun 9, 2024
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
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
args.compile_options(gctx, CompileMode::Bench, Some(&ws), ProfileChecking::Custom)?;

compile_opts.build_config.requested_profile =
args.get_profile_name(gctx, "bench", ProfileChecking::Custom)?;
args.get_profile_name("bench", ProfileChecking::Custom)?;

let ops = TestOptions {
no_run: args.flag("no-run"),
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
gctx,
spec: values(args, "package"),
targets: args.targets()?,
requested_profile: args.get_profile_name(gctx, "dev", ProfileChecking::Custom)?,
requested_profile: args.get_profile_name("dev", ProfileChecking::Custom)?,
profile_specified: args.contains_id("profile") || args.flag("release"),
doc: args.flag("doc"),
dry_run: args.dry_run(),
Expand Down
13 changes: 8 additions & 5 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ pub fn cli() -> Command {
)
.arg_features()
.arg_parallel()
.arg(flag(
"debug",
"Build in debug mode (with the 'dev' profile) instead of release mode",
))
.arg(
flag(
"debug",
"Build in debug mode (with the 'dev' profile) instead of release mode",
)
.conflicts_with("profile"),
)
.arg_redundant_default_mode("release", "install", "debug")
.arg_profile("Install artifacts with the specified profile")
.arg_target_triple("Build for the target triple")
Expand Down Expand Up @@ -196,7 +199,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
)?;

compile_opts.build_config.requested_profile =
args.get_profile_name(gctx, "release", ProfileChecking::Custom)?;
args.get_profile_name("release", ProfileChecking::Custom)?;

if args.flag("list") {
ops::install_list(root, gctx)?;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
args.compile_options(gctx, CompileMode::Test, Some(&ws), ProfileChecking::Custom)?;

compile_opts.build_config.requested_profile =
args.get_profile_name(gctx, "test", ProfileChecking::Custom)?;
args.get_profile_name("test", ProfileChecking::Custom)?;

// `TESTNAME` is actually an argument of the test binary, but it's
// important, so we explicitly mention it and reconfigure.
Expand Down
36 changes: 10 additions & 26 deletions src/cargo/util/command_prelude.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#13971 (comment)

@heisen-li have you considered this option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to you guys, I used this suggestion. Regarding tests, I keep a relevant test for each command (rustc, build, check, install).

Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ pub trait CommandExt: Sized {
) -> 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))
self._arg(
flag(default_mode, "")
.conflicts_with("profile")
.value_parser(value_parser)
.hide(true),
)
}

fn arg_targets_all(
Expand Down Expand Up @@ -226,6 +231,7 @@ pub trait CommandExt: Sized {
self._arg(
flag("release", release)
.short('r')
.conflicts_with("profile")
.help_heading(heading::COMPILATION_OPTIONS),
)
}
Expand Down Expand Up @@ -558,7 +564,6 @@ Run `{cmd}` to see possible targets."

fn get_profile_name(
&self,
gctx: &GlobalContext,
default: &str,
profile_checking: ProfileChecking,
) -> CargoResult<InternedString> {
Expand All @@ -571,40 +576,19 @@ 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.maybe_flag("release") {
gctx.shell().warn(
"the `--release` flag should not be specified with the `--profile` flag\n\
The `--release` flag will be ignored.\n\
This was historically accepted, but will become an error \
in a future release."
)?;
}
return Ok(InternedString::new(name));
}
_ => {}
}

let conflict = |flag: &str, equiv: &str, specified: &str| -> anyhow::Error {
anyhow::format_err!(
"conflicting usage of --profile={} and --{flag}\n\
The `--{flag}` flag is the same as `--profile={equiv}`.\n\
Remove one flag or the other to continue.",
specified,
flag = flag,
equiv = equiv
)
};

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)),
(_, true, None | Some("dev")) => "dev",
(_, true, Some(name)) => return Err(conflict("debug", "dev", name)),
(true, _, None) => "release",
(_, true, None) => "dev",
// `doc` is separate from all the other reservations because
// [profile.doc] was historically allowed, but is deprecated and
// has no effect. To avoid potentially breaking projects, it is a
Expand Down Expand Up @@ -710,7 +694,7 @@ Run `{cmd}` to see possible targets."
mode,
)?;
build_config.message_format = message_format.unwrap_or(MessageFormat::Human);
build_config.requested_profile = self.get_profile_name(gctx, "dev", profile_checking)?;
build_config.requested_profile = self.get_profile_name("dev", profile_checking)?;
build_config.build_plan = self.flag("build-plan");
build_config.unit_graph = self.flag("unit-graph");
build_config.future_incompat_report = self.flag("future-incompat-report");
Expand Down
19 changes: 19 additions & 0 deletions tests/testsuite/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,27 @@ fn rustc_check() {

// Verify compatible usage of --profile with --release, issue #7488
foo.cargo("rustc --profile check --release -- --emit=metadata")
.with_status(1)
.with_stderr(
"\
[ERROR] the argument '--profile <PROFILE-NAME>' cannot be used with '--release'

Usage: cargo[EXE] rustc --profile <PROFILE-NAME> [ARGS]...

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

foo.cargo("rustc --profile test --release -- --emit=metadata")
.with_status(1)
.with_stderr(
"\
[ERROR] the argument '--profile <PROFILE-NAME>' cannot be used with '--release'

Usage: cargo[EXE] rustc --profile <PROFILE-NAME> [ARGS]...

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

Expand Down
90 changes: 14 additions & 76 deletions tests/testsuite/profile_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,100 +380,38 @@ fn conflicting_usage() {
.build();

p.cargo("build --profile=dev --release")
.with_status(101)
.with_status(1)
.with_stderr(
"\
error: conflicting usage of --profile=dev and --release
The `--release` flag is the same as `--profile=release`.
Remove one flag or the other to continue.
",
)
.run();
[ERROR] the argument '--profile <PROFILE-NAME>' cannot be used with '--release'

p.cargo("install --profile=release --debug")
.with_status(101)
.with_stderr(
"\
error: conflicting usage of --profile=release and --debug
The `--debug` flag is the same as `--profile=dev`.
Remove one flag or the other to continue.
",
)
.run();
Usage: cargo[EXE] build --profile <PROFILE-NAME>

p.cargo("rustc --profile=dev --release")
.with_stderr(
"\
warning: the `--release` flag should not be specified with the `--profile` flag
The `--release` flag will be ignored.
This was historically accepted, but will become an error in a future release.
[COMPILING] foo [..]
[FINISHED] `dev` profile [..]
",
For more information, try '--help'.",
)
.run();

p.cargo("check --profile=dev --release")
.with_status(101)
p.cargo("install --profile=release --debug")
.with_status(1)
.with_stderr(
"\
error: conflicting usage of --profile=dev and --release
The `--release` flag is the same as `--profile=release`.
Remove one flag or the other to continue.
",
)
.run();
[ERROR] the argument '--profile <PROFILE-NAME>' cannot be used with '--debug'

p.cargo("check --profile=test --release")
.with_stderr(
"\
warning: the `--release` flag should not be specified with the `--profile` flag
The `--release` flag will be ignored.
This was historically accepted, but will become an error in a future release.
[CHECKING] foo [..]
[FINISHED] `test` profile [..]
",
)
.run();
Usage: cargo[EXE] install --profile <PROFILE-NAME> [CRATE[@<VER>]]...

// This is OK since the two are the same.
p.cargo("rustc --profile=release --release")
.with_stderr(
"\
[COMPILING] foo [..]
[FINISHED] `release` profile [..]
",
For more information, try '--help'.",
)
.run();

p.cargo("build --profile=release --release")
p.cargo("check --profile=dev --release")
.with_status(1)
.with_stderr(
"\
[FINISHED] `release` profile [..]
",
)
.run();
[ERROR] the argument '--profile <PROFILE-NAME>' cannot be used with '--release'

p.cargo("install --path . --profile=dev --debug")
.with_stderr(
"\
[INSTALLING] foo [..]
[FINISHED] `dev` profile [..]
[INSTALLING] [..]
[INSTALLED] [..]
[WARNING] be sure to add [..]
",
)
.run();
Usage: cargo[EXE] check --profile <PROFILE-NAME>

p.cargo("install --path . --profile=release --debug")
.with_status(101)
.with_stderr(
"\
error: conflicting usage of --profile=release and --debug
The `--debug` flag is the same as `--profile=dev`.
Remove one flag or the other to continue.
",
For more information, try '--help'.",
)
.run();
}
Expand Down