Skip to content

Commit aecaef3

Browse files
committed
Auto merge of #5944 - dwijnand:fix-force-rebuild, r=alexcrichton
Force `cargo fix` to rebuild Fixes #5736 This is a resubmit of @killercup's #5750, rebased on current master. @alexcrichton From browsing the code I feel like `-p` would still restrict the packages to rebuild, despite the rebuild flag added. But I might be misreading or not-fully-reading the code. Could you give me some mentoring instructions for the test cases you're concerned with?
2 parents e954520 + 7a42790 commit aecaef3

File tree

6 files changed

+142
-4
lines changed

6 files changed

+142
-4
lines changed

src/bin/cargo/commands/fix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn cli() -> App {
7373
)
7474
.after_help(
7575
"\
76-
This Cargo subcommmand will automatically take rustc's suggestions from
76+
This Cargo subcommand will automatically take rustc's suggestions from
7777
diagnostics like warnings and apply them to your source code. This is intended
7878
to help automate tasks that rustc itself already knows how to tell you to fix!
7979
The `cargo fix` subcommand is also being developed for the Rust 2018 edition

src/cargo/core/compiler/build_config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub struct BuildConfig {
1616
pub mode: CompileMode,
1717
/// Whether to print std output in json format (for machine reading)
1818
pub message_format: MessageFormat,
19+
/// Force cargo to do a full rebuild and treat each target as changed.
20+
pub force_rebuild: bool,
1921
/// Output a build plan to stdout instead of actually compiling.
2022
pub build_plan: bool,
2123
/// Use Cargo itself as the wrapper around rustc, only used for `cargo fix`
@@ -79,6 +81,7 @@ impl BuildConfig {
7981
release: false,
8082
mode,
8183
message_format: MessageFormat::Human,
84+
force_rebuild: false,
8285
build_plan: false,
8386
cargo_as_rustc_wrapper: false,
8487
extra_rustc_env: Vec::new(),

src/cargo/core/compiler/context/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
157157
// part of this, that's all done next as part of the `execute`
158158
// function which will run everything in order with proper
159159
// parallelism.
160-
super::compile(&mut self, &mut queue, &mut plan, unit, exec)?;
160+
let force_rebuild = self.bcx.build_config.force_rebuild;
161+
super::compile(&mut self, &mut queue, &mut plan, unit, exec, force_rebuild)?;
161162
}
162163

163164
// Now that we've figured out everything that we're going to do, do it!

src/cargo/core/compiler/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ fn compile<'a, 'cfg: 'a>(
129129
plan: &mut BuildPlan,
130130
unit: &Unit<'a>,
131131
exec: &Arc<Executor>,
132+
force_rebuild: bool,
132133
) -> CargoResult<()> {
133134
let bcx = cx.bcx;
134135
let build_plan = bcx.build_config.build_plan;
@@ -164,7 +165,7 @@ fn compile<'a, 'cfg: 'a>(
164165
let dirty = work.then(link_targets(cx, unit, false)?).then(dirty);
165166
let fresh = link_targets(cx, unit, true)?.then(fresh);
166167

167-
if exec.force_rebuild(unit) {
168+
if exec.force_rebuild(unit) || force_rebuild {
168169
freshness = Freshness::Dirty;
169170
}
170171

@@ -175,7 +176,7 @@ fn compile<'a, 'cfg: 'a>(
175176

176177
// Be sure to compile all dependencies of this target as well.
177178
for unit in cx.dep_targets(unit).iter() {
178-
compile(cx, jobs, plan, unit, exec)?;
179+
compile(cx, jobs, plan, unit, exec, false)?;
179180
}
180181
if build_plan {
181182
plan.add(cx, unit)?;

src/cargo/ops/fix.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub fn fix(ws: &Workspace, opts: &mut FixOptions) -> CargoResult<()> {
4949
));
5050
let _started = lock_server.start()?;
5151

52+
opts.compile_opts.build_config.force_rebuild = true;
53+
5254
if opts.broken_code {
5355
let key = BROKEN_CODE_ENV.to_string();
5456
opts.compile_opts.build_config.extra_rustc_env.push((key, "1".to_string()));

tests/testsuite/fix.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,3 +986,134 @@ For more information try --help
986986
.with_stderr(stderr)
987987
.run();
988988
}
989+
990+
#[test]
991+
fn shows_warnings_on_second_run_without_changes() {
992+
let p = project()
993+
.file(
994+
"src/lib.rs",
995+
r#"
996+
use std::default::Default;
997+
998+
pub fn foo() {
999+
}
1000+
"#,
1001+
)
1002+
.build();
1003+
1004+
p.cargo("fix --allow-no-vcs")
1005+
.with_stderr_contains("[..]warning: unused import[..]")
1006+
.run();
1007+
1008+
p.cargo("fix --allow-no-vcs")
1009+
.with_stderr_contains("[..]warning: unused import[..]")
1010+
.run();
1011+
}
1012+
1013+
#[test]
1014+
fn shows_warnings_on_second_run_without_changes_on_multiple_targets() {
1015+
let p = project()
1016+
.file(
1017+
"src/lib.rs",
1018+
r#"
1019+
use std::default::Default;
1020+
1021+
pub fn a() -> u32 { 3 }
1022+
"#,
1023+
)
1024+
.file(
1025+
"src/main.rs",
1026+
r#"
1027+
use std::default::Default;
1028+
fn main() { println!("3"); }
1029+
"#,
1030+
)
1031+
.file(
1032+
"tests/foo.rs",
1033+
r#"
1034+
use std::default::Default;
1035+
#[test]
1036+
fn foo_test() {
1037+
println!("3");
1038+
}
1039+
"#,
1040+
)
1041+
.file(
1042+
"tests/bar.rs",
1043+
r#"
1044+
use std::default::Default;
1045+
1046+
#[test]
1047+
fn foo_test() {
1048+
println!("3");
1049+
}
1050+
"#,
1051+
)
1052+
.file(
1053+
"examples/fooxample.rs",
1054+
r#"
1055+
use std::default::Default;
1056+
1057+
fn main() {
1058+
println!("3");
1059+
}
1060+
"#,
1061+
)
1062+
.build();
1063+
1064+
p.cargo("fix --allow-no-vcs --all-targets")
1065+
.with_stderr_contains(" --> examples/fooxample.rs:2:21")
1066+
.with_stderr_contains(" --> src/lib.rs:2:21")
1067+
.with_stderr_contains(" --> src/main.rs:2:21")
1068+
.with_stderr_contains(" --> tests/bar.rs:2:21")
1069+
.with_stderr_contains(" --> tests/foo.rs:2:21")
1070+
.run();
1071+
1072+
p.cargo("fix --allow-no-vcs --all-targets")
1073+
.with_stderr_contains(" --> examples/fooxample.rs:2:21")
1074+
.with_stderr_contains(" --> src/lib.rs:2:21")
1075+
.with_stderr_contains(" --> src/main.rs:2:21")
1076+
.with_stderr_contains(" --> tests/bar.rs:2:21")
1077+
.with_stderr_contains(" --> tests/foo.rs:2:21")
1078+
.run();
1079+
}
1080+
1081+
#[test]
1082+
fn doesnt_rebuild_dependencies() {
1083+
let p = project()
1084+
.file(
1085+
"Cargo.toml",
1086+
r#"
1087+
[package]
1088+
name = "foo"
1089+
version = "0.1.0"
1090+
1091+
[dependencies]
1092+
bar = { path = 'bar' }
1093+
1094+
[workspace]
1095+
"#,
1096+
).file("src/lib.rs", "extern crate bar;")
1097+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
1098+
.file("bar/src/lib.rs", "")
1099+
.build();
1100+
1101+
p.cargo("fix --allow-no-vcs -p foo")
1102+
.env("__CARGO_FIX_YOLO", "1")
1103+
.with_stdout("")
1104+
.with_stderr("\
1105+
[CHECKING] bar v0.1.0 ([..])
1106+
[CHECKING] foo v0.1.0 ([..])
1107+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1108+
")
1109+
.run();
1110+
1111+
p.cargo("fix --allow-no-vcs -p foo")
1112+
.env("__CARGO_FIX_YOLO", "1")
1113+
.with_stdout("")
1114+
.with_stderr("\
1115+
[CHECKING] foo v0.1.0 ([..])
1116+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1117+
")
1118+
.run();
1119+
}

0 commit comments

Comments
 (0)