Skip to content

Commit 0ce96cc

Browse files
committed
Match original line endings in format output on Windows
On Windows, bsc writes CRLF to stdout in text mode. When the original source file uses LF line endings, the formatted output would introduce unwanted CRLF conversions. Detect the original file's line ending style and normalize the formatted output to match.
1 parent 5f93173 commit 0ce96cc

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

rewatch/src/format.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,25 @@ fn format_files(bsc_exe: &Path, files: Vec<String>, check: bool) -> Result<()> {
9898

9999
if output.status.success() {
100100
let original_content = fs::read_to_string(file)?;
101-
let formatted_content = String::from_utf8_lossy(&output.stdout);
101+
let formatted_content = {
102+
let raw = String::from_utf8_lossy(&output.stdout);
103+
// On Windows, bsc writes CRLF to stdout (text mode).
104+
// Match the line ending style of the original file so we
105+
// don't introduce unwanted conversions.
106+
#[cfg(windows)]
107+
{
108+
let original_has_crlf = original_content.contains("\r\n");
109+
if !original_has_crlf && raw.contains("\r\n") {
110+
std::borrow::Cow::Owned(raw.replace("\r\n", "\n"))
111+
} else {
112+
raw
113+
}
114+
}
115+
#[cfg(not(windows))]
116+
{
117+
raw
118+
}
119+
};
102120
if original_content != formatted_content {
103121
if check {
104122
eprintln!("[format check] {file}");

0 commit comments

Comments
 (0)