Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying autofixes uses wrong line endings / should honour .editorconfig #3360

Closed
dgeelen-uipath opened this issue Jul 1, 2021 · 2 comments · Fixed by #3607
Closed

Applying autofixes uses wrong line endings / should honour .editorconfig #3360

dgeelen-uipath opened this issue Jul 1, 2021 · 2 comments · Fixed by #3607

Comments

@dgeelen-uipath
Copy link

.editorconfig:

[*.cs]
end_of_line = lf

Example.cs:

namespace Bug
{
    public static class Example
    {
        public static int MissingNewLine(bool condition)
        {
            if (condition)
            {
                return 42;
            } // SA1513: Closing brace should be followed by a blank line
            return 0;
        }
    }
}

Applying the automatic fix for SA1513 (and probably a bunch of others, but this is the one where I tracked down the problem) inserts a CRLF (carriage return + line feed) combination, but the file uses LF only. In Visual Studio, you can observe the state of line endings for the current file in the bottom right corner of the file-editor pane. It should initially say LF, when the automatic fix is applied it will switch to MIXED instead:
image

The next time the file is opened (but not immediately when the problem is introduced!), Visual Studio will complain about mixed line endings, and suggest to 'normalize' the line endings (using the wrong default option, of-course):
image

The root-cause is probably because the fixer appears to use a hard-coded CRLF (SyntaxFactory.CarriageReturnLineFeed) rather than the currently configured line-end character.

@bjornhellander
Copy link
Contributor

bjornhellander commented Feb 12, 2023

There are about 40 places in the code fixers that use SyntaxFactory.CarriageReturnLineFeed directly. Do you see any problem with replacing them with code that creates trivia from GetOption(FormattingOptions.NewLine, LanguageNames.CSharp), @sharwell?

@sharwell
Copy link
Member

sharwell commented Feb 15, 2023

For SA1513, I'd rather update it to match the newline already at the end of the line where the fix is being applied. Existing uses of FormattingOptions.NewLine are potentially problematic because they look at workspace options instead of focusing on the current document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants