@@ -7,19 +7,18 @@ use rustc_span::symbol;
77
88pub ( crate ) use syntux:: session:: ParseSess ;
99
10- use self :: newline_style:: apply_newline_style;
1110use crate :: config:: { Config , FileName } ;
12- use crate :: formatting:: modules:: Module ;
1311use crate :: formatting:: {
1412 comment:: { CharClasses , FullCodeCharKind } ,
13+ modules:: Module ,
14+ newline_style:: apply_newline_style,
1515 report:: NonFormattedRange ,
1616 syntux:: parser:: { DirectoryOwnership , Parser , ParserError } ,
1717 utils:: count_newlines,
1818 visitor:: FmtVisitor ,
1919} ;
20- use crate :: result:: OperationError ;
2120use crate :: {
22- result:: { ErrorKind , FormatError } ,
21+ result:: { ErrorKind , FormatError , OperationError } ,
2322 FormatReport , FormatResult , Input , OperationSetting , Verbosity ,
2423} ;
2524
@@ -136,7 +135,7 @@ fn format_project(
136135 & module,
137136 & format_report,
138137 original_snippet. clone ( ) ,
139- ) ;
138+ ) ? ;
140139 }
141140 timer = timer. done_formatting ( ) ;
142141
@@ -159,7 +158,7 @@ fn format_file(
159158 module : & Module < ' _ > ,
160159 report : & FormatReport ,
161160 original_snippet : Option < String > ,
162- ) {
161+ ) -> Result < ( ) , OperationError > {
163162 let snippet_provider = parse_session. snippet_provider ( module. as_ref ( ) . inner ) ;
164163 let mut visitor =
165164 FmtVisitor :: from_parse_sess ( & parse_session, config, & snippet_provider, report. clone ( ) ) ;
@@ -187,22 +186,33 @@ fn format_file(
187186 report. clone ( ) ,
188187 ) ;
189188
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) ;
195203
196204 if visitor. macro_rewrite_failure {
197205 report. add_macro_format_failure ( path. clone ( ) ) ;
198206 }
199207 let format_result = FormatResult :: success (
200208 visitor. buffer . to_owned ( ) ,
201209 visitor. skipped_range . borrow ( ) . clone ( ) ,
202- original_snippet ,
210+ original_text ,
203211 config. newline_style ( ) ,
204212 ) ;
205213 report. add_format_result ( path, format_result) ;
214+
215+ Ok ( ( ) )
206216}
207217
208218#[ derive( Clone , Copy , Debug ) ]
0 commit comments