Skip to content

Make line endings of saved files consistent across platforms - #774

Open
CoffeeFlux wants to merge 2 commits into
TypesettingTools:masterfrom
CoffeeFlux:consistent-line-endings
Open

CoffeeFlux wants to merge 2 commits into
TypesettingTools:masterfrom
CoffeeFlux:consistent-line-endings

Conversation

@CoffeeFlux

Copy link
Copy Markdown
Member

Previously TextFileWriter wrote \r\n on Windows and \n everywhere else, so the same script saved on different platforms produced spurious version-control diffs.

Always write \n for newly created files on every platform. When overwriting an existing file, detect the line endings it already uses (in the target charset, so UTF-16 files are handled too) and keep them, so files already committed with \r\n don't churn when resaved.

Also drop the platform-dependent LINEBREAK macro from the ASS writer's format lines, which bypassed TextFileWriter's newline handling.

Fixes #757.

Requested-by: Ryan Lucia

@rcombs rcombs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with a couple nits.

Comment thread src/text_file_writer.cpp
Comment on lines +33 to +37
std::string cr("\r"), lf("\n");
if (conv) {
cr = conv->Convert(cr);
lf = conv->Convert(lf);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this just exists to handle UTF-16? Kinda awkward that it gets searched byte-wise, but eh, probably good enough.

Comment thread src/text_file_writer.cpp Resolved
@CoffeeFlux
CoffeeFlux marked this pull request as ready for review November 12, 2026 23:19
@petzku

petzku commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

When overwriting an existing file, detect the line endings it already uses

This seems like an odd way to do it; basically all editors I can think of would keep the original file's line endings in memory, and write back what they read (notably, files read as CRLF would still be written as CRLF after a "Save as..."). This is unlikely to directly break anything, so it's more of a philosophical question than a practical concern, but I can think of at least one edge case:
If an existing ASS file is opened and edited, and the original file is removed or renamed, saving it from Aegisub will now always write LF (as we are writing a "new" file), even if the original file was CRLF, creating potential VCS churn. Similarly, this would mean that save-copy-rename behaves differently than save-as, which is not necessarily wrong but feels like a potential cause for problems.

All that said if the primary concern is about VCS churn: is this actually a relevant concern in the first place? The only platform where Aegisub would write CRLF is Windows, and though Git defaults to core.autocrlf false in the absence of config, the Windows installer by default sets it to true.

Previously TextFileWriter wrote \r\n on Windows and \n everywhere else,
so the same script saved on different platforms produced spurious
version-control diffs.

Always write \n for newly created files on every platform. When
overwriting an existing file, detect the line endings it already uses
(in the target charset, so UTF-16 files are handled too) and keep them,
so files already committed with \r\n don't churn when resaved.

Also drop the platform-dependent LINEBREAK macro from the ASS writer's
format lines, which bypassed TextFileWriter's newline handling.

Fixes TypesettingTools#757.

Requested-by: Ryan Lucia
DetectExistingNewline already converts CR and LF to the target
encoding for detection, so return those directly instead of
converting the result a second time in the constructor.

Requested-by: Ryan Lucia
@final-ai-dev
final-ai-dev Bot force-pushed the consistent-line-endings branch from c464bd8 to f94b655 Compare November 13, 2026 22:20
@arch1t3cht

Copy link
Copy Markdown
Member

I mostly agree with petzku:

  • While I can't really point out any specific deal-breaking case where the "detect target file's line endings* logic breaks badly, it does seem very nonstandard and might be surprising for the user.

  • We should probably investigate whether this autodetection would even be necessary:

    1. Is there any reason other than VCS churn why simply switching to LF could cause problems?
    2. Do we know of any existing groups or workflows that use git with their subtitles with CRLF endings in the actual repository (i.e. using Windows and having autocrlf disabled)?

    Note also that ffmpeg will unconditionally write LF line endings for .ass files it generates even on Windows (though mkvextract will use CRLF) and I am not aware of that having ever caused any problems.

My current gut feeling is that simply switching Aegisub to unconditionally writing LF would be a much simpler solution that should not actually break any existing workflows. But if anyone knows a genuine real-world case where this would break something, I'd be very interested in that.

@rcombs

rcombs commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

VCS churn is the only modern concern I'm aware of (setting aside, like, WinXP Notepad users). As for CRLFs in repos, I'm aware of it happening (running into it was the reason I opened this issue in the first place); breaking autocrlf seems to be the national pastime of windows users, somehow. On the other hand, I'm not sure if anyone would actually mind those getting forced to LFs on save; it'd just make affected users with updated aegisub equivalent to non-windows users. I suggested the autodetection out of an abundance of caution, and have no strong opinions on how it should work (or if it should be implemented at all); my only strong opinion here is that we should do something promptly. I think any of the proposals here is strictly better than the status quo, and bikeshedding on tweaks for these edge cases after merge/release is no worse than doing the bikeshedding first.

@arch1t3cht

Copy link
Copy Markdown
Member

As for CRLFs in repos, I'm aware of it happening (running into it was the reason I opened this issue in the first place)

As in people intentionally storing CRLF in the repository, or as in them accidentally storing CRLF, e.g. by uploading files through the GitHub website rather than a local checkout (leading to the affected file then being permanently marked "modified" for any Windows user with autocrlf enabled)?
I'm asking since I've seen the latter, but not the former, and the latter would also be improved by switching to always-LF.

@rcombs

rcombs commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Accidentally storing CRLF, because core.autocrlf was accidentally globally disabled (apparently depctrl sometimes screws this up? idk I don't go there), though I've also seen the githubweb version of this. I don't know of any cases whatsoever where someone knows the difference between LF and CRLF and intentionally uses CRLF in ass files.

@arch1t3cht

Copy link
Copy Markdown
Member

In that case I would definitely prefer just unconditionally writing LF everywhere.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make line-ending behavior consistent across platforms

4 participants