From 616e0ad3bfdf7b5832ba987f5d26fc472e77e196 Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Fri, 20 Jul 2018 12:15:50 +0200 Subject: [PATCH] Always rebuild targets when using cargo-fix --- src/cargo/core/compiler/build_config.rs | 3 + src/cargo/core/compiler/mod.rs | 3 +- src/cargo/ops/fix.rs | 2 + tests/testsuite/fix.rs | 91 +++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index 0ce9a3a40..77ed087ee 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -16,6 +16,8 @@ pub struct BuildConfig { pub mode: CompileMode, /// Whether to print std output in json format (for machine reading) pub message_format: MessageFormat, + /// Force cargo to do a full rebuild and treat each target as changed. + pub force_rebuild: bool, /// Output a build plan to stdout instead of actually compiling. pub build_plan: bool, /// Use Cargo itself as the wrapper around rustc, only used for `cargo fix` @@ -79,6 +81,7 @@ impl BuildConfig { release: false, mode, message_format: MessageFormat::Human, + force_rebuild: false, build_plan: false, cargo_as_rustc_wrapper: false, extra_rustc_env: Vec::new(), diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 787889439..3be3d6faa 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -132,6 +132,7 @@ fn compile<'a, 'cfg: 'a>( ) -> CargoResult<()> { let bcx = cx.bcx; let build_plan = bcx.build_config.build_plan; + let force_rebuild = bcx.build_config.force_rebuild; if !cx.compiled.insert(*unit) { return Ok(()); } @@ -164,7 +165,7 @@ fn compile<'a, 'cfg: 'a>( let dirty = work.then(link_targets(cx, unit, false)?).then(dirty); let fresh = link_targets(cx, unit, true)?.then(fresh); - if exec.force_rebuild(unit) { + if exec.force_rebuild(unit) || force_rebuild { freshness = Freshness::Dirty; } diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index 275a471bd..f0bb8931b 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -48,6 +48,8 @@ pub fn fix(ws: &Workspace, opts: &mut FixOptions) -> CargoResult<()> { )); let _started = lock_server.start()?; + opts.compile_opts.build_config.force_rebuild = true; + if opts.broken_code { let key = BROKEN_CODE_ENV.to_string(); opts.compile_opts.build_config.extra_rustc_env.push((key, "1".to_string())); diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index 0a5f9f2a0..903cd5191 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -950,3 +950,94 @@ For more information try --help .with_stderr(stderr) .run(); } + +#[test] +fn shows_warnings_on_second_run_without_changes() { + let p = project() + .file( + "src/lib.rs", + r#" + use std::default::Default; + + pub fn foo() { + } + "#, + ) + .build(); + + p.cargo("fix --allow-no-vcs") + .with_stderr_contains("[..]warning: unused import[..]") + .run(); + + p.cargo("fix --allow-no-vcs") + .with_stderr_contains("[..]warning: unused import[..]") + .run(); +} + +#[test] +fn shows_warnings_on_second_run_without_changes_on_multiple_targets() { + let p = project() + .file( + "src/lib.rs", + r#" + use std::default::Default; + + pub fn a() -> u32 { 3 } + "#, + ) + .file( + "src/main.rs", + r#" + use std::default::Default; + fn main() { println!("3"); } + "#, + ) + .file( + "tests/foo.rs", + r#" + use std::default::Default; + #[test] + fn foo_test() { + println!("3"); + } + "#, + ) + .file( + "tests/bar.rs", + r#" + use std::default::Default; + + #[test] + fn foo_test() { + println!("3"); + } + "#, + ) + .file( + "examples/fooxample.rs", + r#" + use std::default::Default; + + fn main() { + println!("3"); + } + "#, + ) + .build(); + + p.cargo("fix --allow-no-vcs --all-targets") + .with_stderr_contains(" --> examples/fooxample.rs:2:21") + .with_stderr_contains(" --> src/lib.rs:2:21") + .with_stderr_contains(" --> src/main.rs:2:21") + .with_stderr_contains(" --> tests/bar.rs:2:21") + .with_stderr_contains(" --> tests/foo.rs:2:21") + .run(); + + p.cargo("fix --allow-no-vcs --all-targets") + .with_stderr_contains(" --> examples/fooxample.rs:2:21") + .with_stderr_contains(" --> src/lib.rs:2:21") + .with_stderr_contains(" --> src/main.rs:2:21") + .with_stderr_contains(" --> tests/bar.rs:2:21") + .with_stderr_contains(" --> tests/foo.rs:2:21") + .run(); +}