Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit a646d16

Browse files
authored
Merge pull request #94 from killercup/warn-when-rustfix-cant-replace-things
Custom warning when rustfix replacing fails
2 parents 17bcea9 + 040686f commit a646d16

File tree

8 files changed

+79
-6
lines changed

8 files changed

+79
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ serde = "1.0"
2121
serde_json = "1.0"
2222
serde_derive = "1.0"
2323
failure = "0.1.1"
24+
log = "0.4.1"
2425

2526
[dev-dependencies]
2627
duct = "0.8.2"

cargo-fix/src/cli.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ fn log_message(msg: &Message, stream: &mut StandardStream) -> Result<(), Error>
102102
stream,
103103
)?;
104104
}
105+
ReplaceFailed { ref file, ref message } => {
106+
stream.set_color(ColorSpec::new().set_bold(true).set_fg(Some(Color::Yellow)))?;
107+
write!(stream, "warning")?;
108+
stream.reset()?;
109+
stream.set_color(ColorSpec::new().set_bold(true))?;
110+
write!(stream, ": error applying suggestions to `{}`\n", file)?;
111+
stream.reset()?;
112+
write!(stream, "The full error message was:\n\n> {}\n\n", message)?;
113+
stream.write(PLEASE_REPORT_THIS_BUG.as_bytes())?;
114+
}
105115
FixFailed { ref files, ref krate } => {
106116
stream.set_color(ColorSpec::new().set_bold(true).set_fg(Some(Color::Yellow)))?;
107117
write!(stream, "warning")?;

cargo-fix/src/diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static DIAGNOSICS_SERVER_VAR: &str = "__CARGO_FIX_DIAGNOSTICS_SERVER";
1717
pub enum Message {
1818
Fixing { file: String, fixes: usize },
1919
FixFailed { files: Vec<String>, krate: Option<String> },
20+
ReplaceFailed { file: String, message: String },
2021
}
2122

2223
impl Message {

cargo-fix/src/main.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,19 @@ fn rustfix_crate(rustc: &Path, filename: &str) -> Result<FixedCrate, Error> {
229229

230230
messages.push(Message::fixing(&file, num_suggestions));
231231

232-
let new_code = rustfix::apply_suggestions(&code, &suggestions)?;
233-
File::create(&file)
234-
.and_then(|mut f| f.write_all(new_code.as_bytes()))
235-
.with_context(|_| format!("failed to write file `{}`", file))?;
236-
original_files.insert(file, code);
232+
match rustfix::apply_suggestions(&code, &suggestions) {
233+
Err(e) => {
234+
diagnostics::Message::ReplaceFailed { file: file, message: e.to_string() }.post()?;
235+
// TODO: Add flag to decide if we want to continue or bail out
236+
continue;
237+
}
238+
Ok(new_code) => {
239+
File::create(&file)
240+
.and_then(|mut f| f.write_all(new_code.as_bytes()))
241+
.with_context(|_| format!("failed to write file `{}`", file))?;
242+
original_files.insert(file, code);
243+
}
244+
}
237245
}
238246

239247
Ok(FixedCrate {

cargo-fix/tests/all/broken_lints.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//! Ensure we give good error message when rustfix failes to apply changes
2+
//!
3+
//! TODO: Add rustc shim that outputs wrong suggestions instead of depending on
4+
//! actual rustc bugs!
5+
6+
use super::project;
7+
8+
#[test]
9+
fn tell_user_about_broken_lints() {
10+
let p = project()
11+
.file(
12+
"src/lib.rs",
13+
r#"
14+
pub fn foo() {
15+
let mut i = 42;
16+
}
17+
"#,
18+
)
19+
.build();
20+
21+
p.expect_cmd("cargo-fix fix")
22+
.stderr_contains(r"warning: error applying suggestions to `src/lib.rs`")
23+
.stderr_contains("The full error message was:")
24+
.stderr_contains("> Could not replace range 56...60 in file -- maybe parts of it were already replaced?")
25+
.stderr_contains("\
26+
This likely indicates a bug in either rustc or rustfix itself,\n\
27+
and we would appreciate a bug report! You're likely to see \n\
28+
a number of compiler warnings after this message which rustfix\n\
29+
attempted to fix but failed. If you could open an issue at\n\
30+
https://github.com/rust-lang-nursery/rustfix/issues\n\
31+
quoting the full output of this command we'd be very appreciative!\n\n\
32+
")
33+
.status(0)
34+
.run();
35+
}

cargo-fix/tests/all/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ fn diff(expected: &str, actual: &str) {
316316
}
317317

318318
mod broken_build;
319+
mod broken_lints;
319320
mod dependencies;
320321
mod smoke;
321322
mod subtargets;

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#[macro_use]
2+
extern crate log;
3+
#[macro_use]
24
extern crate failure;
35
#[cfg(test)]
46
#[macro_use]

src/replace.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,23 @@ impl Data {
8989
.iter()
9090
.position(|p| p.start <= from && p.end >= up_to_and_including)
9191
.ok_or_else(|| {
92+
use log::Level::Debug;
93+
if log_enabled!(Debug) {
94+
let slices = self.parts
95+
.iter()
96+
.map(|p| (p.start, p.end, match p.data {
97+
State::Initial => "initial",
98+
State::Replaced(..) => "replaced",
99+
}))
100+
.collect::<Vec<_>>();
101+
debug!("no single slice covering {}...{}, current slices: {:?}",
102+
from, up_to_and_including, slices,
103+
);
104+
}
105+
92106
format_err!(
93-
"Could not find data slice that covers range {}..{}",
107+
"Could not replace range {}...{} in file \
108+
-- maybe parts of it were already replaced?",
94109
from,
95110
up_to_and_including
96111
)

0 commit comments

Comments
 (0)