DotNetAnalyzers / StyleCopAnalyzers

An implementation of StyleCop rules using the .NET Compiler Platform
MIT License
2.66k stars 507 forks source link

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

Open AArnott opened 2 months ago

AArnott commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 1 month ago

@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 commented 1 month ago

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.