@@ -7,19 +7,18 @@ use rustc_span::symbol;
7
7
8
8
pub ( crate ) use syntux:: session:: ParseSess ;
9
9
10
- use self :: newline_style:: apply_newline_style;
11
10
use crate :: config:: { Config , FileName } ;
12
- use crate :: formatting:: modules:: Module ;
13
11
use crate :: formatting:: {
14
12
comment:: { CharClasses , FullCodeCharKind } ,
13
+ modules:: Module ,
14
+ newline_style:: apply_newline_style,
15
15
report:: NonFormattedRange ,
16
16
syntux:: parser:: { DirectoryOwnership , Parser , ParserError } ,
17
17
utils:: count_newlines,
18
18
visitor:: FmtVisitor ,
19
19
} ;
20
- use crate :: result:: OperationError ;
21
20
use crate :: {
22
- result:: { ErrorKind , FormatError } ,
21
+ result:: { ErrorKind , FormatError , OperationError } ,
23
22
FormatReport , FormatResult , Input , OperationSetting , Verbosity ,
24
23
} ;
25
24
@@ -136,7 +135,7 @@ fn format_project(
136
135
& module,
137
136
& format_report,
138
137
original_snippet. clone ( ) ,
139
- ) ;
138
+ ) ? ;
140
139
}
141
140
timer = timer. done_formatting ( ) ;
142
141
@@ -159,7 +158,7 @@ fn format_file(
159
158
module : & Module < ' _ > ,
160
159
report : & FormatReport ,
161
160
original_snippet : Option < String > ,
162
- ) {
161
+ ) -> Result < ( ) , OperationError > {
163
162
let snippet_provider = parse_session. snippet_provider ( module. as_ref ( ) . inner ) ;
164
163
let mut visitor =
165
164
FmtVisitor :: from_parse_sess ( & parse_session, config, & snippet_provider, report. clone ( ) ) ;
@@ -187,22 +186,33 @@ fn format_file(
187
186
report. clone ( ) ,
188
187
) ;
189
188
190
- apply_newline_style (
191
- config. newline_style ( ) ,
192
- & mut visitor. buffer ,
193
- snippet_provider. entire_snippet ( ) ,
194
- ) ;
189
+ // SourceFile's in the SourceMap will always have Unix-style line endings
190
+ // See: https://github.com/rust-lang/rustfmt/issues/3850
191
+ // So we must check the file system to get the original file value in order
192
+ // to detect newline_style conflicts.
193
+ // Otherwise, parse session is around (cfg(not(test))) and newline_style has been
194
+ // left as the default value, then try getting source from the parse session
195
+ // source map instead of hitting the file system.
196
+ let original_text = match original_snippet {
197
+ Some ( snippet) => snippet,
198
+ None => std:: fs:: read_to_string ( path. as_path ( ) . ok_or ( OperationError :: IoError (
199
+ std:: io:: Error :: from ( std:: io:: ErrorKind :: InvalidInput ) ,
200
+ ) ) ?) ?,
201
+ } ;
202
+ apply_newline_style ( config. newline_style ( ) , & mut visitor. buffer , & original_text) ;
195
203
196
204
if visitor. macro_rewrite_failure {
197
205
report. add_macro_format_failure ( path. clone ( ) ) ;
198
206
}
199
207
let format_result = FormatResult :: success (
200
208
visitor. buffer . to_owned ( ) ,
201
209
visitor. skipped_range . borrow ( ) . clone ( ) ,
202
- original_snippet ,
210
+ original_text ,
203
211
config. newline_style ( ) ,
204
212
) ;
205
213
report. add_format_result ( path, format_result) ;
214
+
215
+ Ok ( ( ) )
206
216
}
207
217
208
218
#[ derive( Clone , Copy , Debug ) ]
0 commit comments