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

SA1518 fails whether there is a newline character or not #3887

Open
AArnott opened this issue Aug 27, 2024 · 5 comments
Open

SA1518 fails whether there is a newline character or not #3887

AArnott opened this issue Aug 27, 2024 · 5 comments

Comments

@AArnott
Copy link
Contributor

AArnott commented Aug 27, 2024

StyleCop.Analyzers: 1.2.0.556

SA1518 is claiming exactly one code file in my solution is not allowed to end with a newline character.
This is odd in itself, because I have a policy that all code files should end with a newline character, so I don't know why this file is not allowed to:
image

Even more confusing is that "correcting" this by removing the newline character produces the same diagnostic but with a contrary message, saying I must end the file with a newline character:
image

Repro:

git clone  https://github.com/microsoft/vs-servicehub.git
cd vs-servicehub
git checkout 6c6880dd568b1816d0c2afd089ec3a6f9407d107
.\init.ps1
.\Microsoft.ServiceBroker.sln

Then open the src\AssemblyInfo.cs file.

@bjornhellander
Copy link
Contributor

The init script fails for me (something with "yarn"), so I haven't been able to try it properly. But I notice that the Error List tab points to different projects in your two screenshots. Seems like the CodeFixes project gets the wrong stylecop settings for some reason and the AssemblyInfo file is shared by many projects.

@AArnott
Copy link
Contributor Author

AArnott commented Aug 27, 2024

But I notice that the Error List tab points to different projects in your two screenshots

Very good eye. :) I hadn't noticed that.

I have only one Stylecop.json in my whole repo, and all relevant projects get it. And it has no policy regarding file endings either.
I have several .editorconfig files, but I don't know if they might impact them. And anyway none of them align with the subsets of projects that seem to disagree with the rule.

Command line build produces no warnings.

@bjornhellander
Copy link
Contributor

If the stylecop.json doesn't set "newlineAtEndOfFile", then we default to using the "insert_final_newline" setting, which you have in your top .editorconfig file. This means that SA1518 should behave as if you had newlineAtEndOfFile set to "require" in your stylecop.json file. And it seems to do so for all projets except CodeFixes.

I don't see exactly the same behavior as you, but it is similar. I can't get SA1518 to trigger at all in the CodeFixes project, regardless of AssemblyInfo has a new line at the end or not.

I managed to debug a stripped down version of your repo yesterday and saw that the correct value was not received when trying to read "insert_final_newline".

If I remove the AssemblyInfo link and add another file without newline, SA1518 triggers in that file. If I add AssemblyInfo back again, it doesn't trigger in any of the files.

At this point, I am inclined to blaim Roslyn, but I will try to dig a little bit more.

@MarcinSzczygiel
Copy link

@AArnott I see you are using Visual Studio Preview.
Today I updated my VS Preview to 17.12 Preview 2.0 and I got a similar problem.
I don't have a stylecop.json file and my settings in .editorconfig says as follows:
insert_final_newline = true

When I open the same solution in VS 17.11.3, this problem doesn't occur. In fact, now I don't see a warning when a new line is missing at the end.

@bjornhellander
Copy link
Contributor

I still experience the same thing in VS 17.11.3 and could reproduce it outside StyleCop.Analyzers, so I created an issue in the Roslyn repo.

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

No branches or pull requests

3 participants