Skip to content

fix: normalize input to LF before parsing - #1140

Open
keithharvey wants to merge 1 commit into
JohnnyMorganz:mainfrom
keithharvey:fix_windows_line_endings
Open

fix: normalize input to LF before parsing#1140
keithharvey wants to merge 1 commit into
JohnnyMorganz:mainfrom
keithharvey:fix_windows_line_endings

Conversation

@keithharvey

Copy link
Copy Markdown

A token keeps whatever line ending the source used. In my case, I am trying to add stylua to Beyond-All-Reason, which sets eol=crlf in .gitattributes, so the input was always CRLF. That repo also has a TON of single-line comments.

A single-line comment is tokenised as SingleLineComment(" text\r") with the \n as separate whitespace. Comments are relocated by take_leading_comments / take_trailing_comments without passing through format_token, so that \r reaches the output, and the configured line ending is then appended after it, producing \r\r\n. The output line ending is decided solely by the line_endings config, so the input's must not survive parsing.

This makes formatting non-idempotent: with line_endings = "Windows", each run adds another \r to affected lines. Related to #665, which fixed the same class of bug for multiline comments and strings.

A token keeps whatever line ending the source used. In my case, I am trying to
add stylua to [Beyond-All-Reason](https://github.com/beyond-all-reason/Beyond-All-Reason/),
which sets eol=crlf in .gitattributes, so the input was always CRLF. That repo
also has a TON of single-line comments.

A single-line comment is tokenised as `SingleLineComment(" text\r")` with the
`\n` as separate whitespace. Comments are relocated by `take_leading_comments` /
`take_trailing_comments` without passing through `format_token`, so that `\r`
reaches the output, and the configured line ending is then appended after it,
producing `\r\r\n`. The output line ending is decided solely by the
`line_endings` config, so the input's must not survive parsing.

This makes formatting non-idempotent: with `line_endings = "Windows"`, each run
adds another `\r` to affected lines. Related to JohnnyMorganz#665, which fixed the same class
of bug for multiline comments and strings.
keithharvey added a commit to keithharvey/bar that referenced this pull request Aug 4, 2026
.gitattributes marks *.lua as eol=crlf, so git writes CRLF on checkout.
stylua wrote LF, so running the formatter left every file it touched
reported as modified with an empty diff.

column_width drops from the 2000 sentinel to 120, the wrap width the
formatting migration actually applies.

StyLua keeps a comment's source \r when it relocates the comment
(JohnnyMorganz/StyLua#1140), so under CRLF output every comment sitting
on a token stylua moves or deletes came out as \r\r\n. Reshapes those
sites so the formatter passes them through: trailing semicolons before
comments dropped, trailing and/or operators moved below their comment,
and comments pulled out of expressions stylua rewrites every pass.
keithharvey added a commit to keithharvey/bar that referenced this pull request Aug 4, 2026
.gitattributes marks *.lua as eol=crlf, so git writes CRLF on checkout.
stylua wrote LF, so running the formatter left every file it touched
reported as modified with an empty diff.

column_width drops from the 2000 sentinel to 120, the wrap width the
formatting migration actually applies.

StyLua keeps a comment's source \r when it relocates the comment
(JohnnyMorganz/StyLua#1140), so under CRLF output every comment sitting
on a token stylua moves or deletes came out as \r\r\n. Reshapes those
sites so the formatter passes them through: trailing semicolons before
comments dropped, trailing and/or operators moved below their comment,
and comments pulled out of expressions stylua rewrites every pass.

Hand-aligned drawing and data blocks keep their layout: gl vertex fans,
paired-diamond vertices, index-aligned triangle strips, the sphere VBO
generator, and the buildmenu sort table sit inside stylua: ignore
ranges; tool-generated atlas tables join .styluaignore wholesale.
Ignore ranges still lose trailing semicolons on local/assignment
statements (upstream stylua bug), so the sphere generator trades its
C-style semicolons for spaces to keep the comment columns.
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.

1 participant