Skip to content

check subcommand should force rebuild #6664

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
1 change: 1 addition & 0 deletions src/bin/cargo/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn cli() -> App {
"Check all targets",
)
.arg_release("Check artifacts in release mode, with optimizations")
.arg_force_rebuild("Force rebuild of the selected target(s)")
.arg(opt("profile", "Profile to build the selected target for").value_name("PROFILE"))
.arg_features()
.arg_target_triple("Check for the target triple")
Expand Down
10 changes: 10 additions & 0 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ pub trait AppExt: Sized {
fn arg_dry_run(self, dry_run: &'static str) -> Self {
self._arg(opt("dry-run", dry_run))
}

fn arg_force_rebuild(self, force_rebuild: &'static str) -> Self {
self._arg(opt("force-rebuild", force_rebuild))
}
}

impl AppExt for App {
Expand Down Expand Up @@ -362,6 +366,12 @@ pub trait ArgMatchesExt {
let mut build_config = BuildConfig::new(config, self.jobs()?, &self.target(), mode)?;
build_config.message_format = message_format.unwrap_or(MessageFormat::Human);
build_config.release = self._is_present("release");
build_config.force_rebuild = self._is_present("force-rebuild");
if build_config.force_rebuild && !config.cli_unstable().unstable_options {
Err(failure::format_err!(
"`--force-rebuild` flag is unstable, pass `-Z unstable-options` to enable it"
))?;
};
build_config.build_plan = self._is_present("build-plan");
if build_config.build_plan {
config
Expand Down
26 changes: 26 additions & 0 deletions tests/testsuite/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,32 @@ fn build_check() {
foo.cargo("check -v").run();
}

// Checks that --force-rebuild displays warnings even if the project has not changed
// since the last check/build
#[test]
fn force_rebuild_displays_error() {
let foo = project()
.file(
"src/main.rs",
"use std::default::Default; fn main() {}",
)
.build();

foo.cargo("check")
.with_stderr_contains("[..]warning: unused import[..]")
.run();

// for now this requires the unstable feature flag, so expect an error here
let output = foo.cargo("check --force-rebuild")
.exec_with_output();
assert!(output.is_err());

foo.cargo("check -Z unstable-options --force-rebuild")
.masquerade_as_nightly_cargo() // remove this when `-Z unstable-options` is no longer required
.with_stderr_contains("[..]warning: unused import[..]")
.run();
}

// Checks that where a project has both a lib and a bin, the lib is only checked
// not built.
#[cargo_test]
Expand Down