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

Are roslyn analyzers working? #809

Closed
dominikjeske opened this issue Sep 11, 2020 · 27 comments · Fixed by #864
Closed

Are roslyn analyzers working? #809

dominikjeske opened this issue Sep 11, 2020 · 27 comments · Fixed by #864
Labels
Resolved-Fixed This issue has been fixed

Comments

@dominikjeske
Copy link

dominikjeske commented Sep 11, 2020

I'm not sure if Roslyn analyzers should already work in current version 5.0.146101. I have tried those:

dotnet_diagnostic.IDE0005.severity = warning

dotnet_diagnostic.SA1507.severity = warning

Visual studio is showing hints for fixing errors but after run:

dotnet format --fix-style warn

nothing is fixed ;(

@JoeRobich
Copy link
Member

@bigdnf Sorry you are running into issues. Can you run with -v diag option and share the output with us?

@JoeRobich JoeRobich added the Needs More Info This issue needs additional details to be actionable label Sep 22, 2020
@dominikjeske
Copy link
Author

I have tested it on new repo - for now only for IDE0005 (still not deleted unused using)

dotnet-format ./ConsoleApp10.sln --fix-style warn -v diag
The dotnet CLI version is '3.1.402'.
Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\3.1.402'.
Formatting code files in workspace 'C:\Users\zew_2_22122\source\repos\ConsoleApp10\ConsoleApp10.sln'.
Loading workspace.
Complete in 1869ms.
Determining formattable files.
Complete in 336ms.
Running formatters.
Running Code Style analysis.
Determining diagnostics...
Complete in 1176ms.
Fixing diagnostics...
Complete in 4ms.
Analysis complete in 1180ms.
Complete in 1598ms.
Formatted 0 of 3 files.
Format complete in 3806ms.

@xtqqczze
Copy link

xtqqczze commented Nov 14, 2020

I have also been unsuccessful in fixing diagnostics using this tool when setting the rule entry in an EditorConfig file:

dotnet_diagnostic.IDE0005.severity = error

dotnet-format --version
5.0.152601+7d7b7f05e9ed67cc4c983b9164add3eb32fa6518
dotnet-format --fix-style error -v diag
  The dotnet runtime version is '5.0.0'.
  The dotnet CLI version is '5.0.100'.
  Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\5.0.100\'.
  Formatting code files in workspace 'C:\source\PowerShell\PowerShell.sln'.
  Loading workspace.
  Complete in 7395ms.
  Determining formattable files.
  Complete in 6101ms.
  Running formatters.
  Running Code Style analysis.
  Determining diagnostics...
  Running 2 analyzers on powershell-win-core.
  Running 2 analyzers on System.Management.Automation.
  Running 2 analyzers on Microsoft.PowerShell.Commands.Utility.
  Running 2 analyzers on Microsoft.PowerShell.CoreCLR.Eventing.
  Running 2 analyzers on Microsoft.PowerShell.ConsoleHost.
  Running 2 analyzers on Microsoft.PowerShell.Commands.Management.
  Running 1 analyzers on Microsoft.PowerShell.SDK.
  Running 2 analyzers on Microsoft.Management.Infrastructure.CimCmdlets.
  Running 2 analyzers on Microsoft.PowerShell.Commands.Diagnostics.
  Running 2 analyzers on Microsoft.WSMan.Management.
  Running 2 analyzers on Microsoft.PowerShell.Security.
  Running 2 analyzers on Microsoft.WSMan.Runtime.
  Running 2 analyzers on powershell-unix.
  Running 2 analyzers on Microsoft.PowerShell.MarkdownRender.
  Running 2 analyzers on xUnit.tests.
  Complete in 22593ms.
  Fixing diagnostics...
  Complete in 7ms.
  Analysis complete in 22612ms.
  Complete in 22866ms.
  Formatted 0 of 1169 files.
  Format complete in 36378ms.

@JoeRobich
Copy link
Member

@dominikjeske & @ xtwwczze Can you try build 5.0.156601 and let me know if your issue is resolved?

@xtqqczze
Copy link

@JoeRobich It seems dotnet-format is not picking up rule entries in an EditorConfig or AnalyzerConfig file:

dotnet-format --version
5.0.156601+acb1b73526b0182799b63b5412c2ac9989aa19bc

dotnet_diagnostic.IDE0005.severity = error:

dotnet-format .\src\Microsoft.Management.Infrastructure.CimCmdlets\ --fix-style error -v diag
  The dotnet runtime version is '5.0.0'.
  The dotnet CLI version is '5.0.100'.
  Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\5.0.100\'.
  Formatting code files in workspace 'C:\source\PowerShell\src\Microsoft.Management.Infrastructure.CimCmdlets\Microsoft.Management.Infrastructure.CimCmdlets.csproj'.
  Loading workspace.
  Complete in 3444ms.
  Determining formattable files.
  Skipping referenced project 'Microsoft.PowerShell.CoreCLR.Eventing'.
  Skipping referenced project 'System.Management.Automation'.
  Complete in 669ms.
  Running formatters.
  Running Code Style analysis.
  Determining diagnostics...
  Running 2 analyzers on Microsoft.Management.Infrastructure.CimCmdlets.
  Running 1 analyzers on Microsoft.PowerShell.CoreCLR.Eventing.
  Running 1 analyzers on System.Management.Automation.
  Complete in 22167ms.
  Fixing diagnostics...
  Complete in 7ms.
  Analysis complete in 22179ms.
  Complete in 22386ms.
  Formatted 0 of 38 files.
  Format complete in 26503ms.

dotnet_diagnostic.IDE0005.severity = silent:

dotnet-format .\src\Microsoft.Management.Infrastructure.CimCmdlets\ --fix-style error -v diag
  The dotnet runtime version is '5.0.0'.
  The dotnet CLI version is '5.0.100'.
  Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\5.0.100\'.
  Formatting code files in workspace 'C:\source\PowerShell\src\Microsoft.Management.Infrastructure.CimCmdlets\Microsoft.Management.Infrastructure.CimCmdlets.csproj'.
  Loading workspace.
  Complete in 4101ms.
  Determining formattable files.
  Skipping referenced project 'Microsoft.PowerShell.CoreCLR.Eventing'.
  Skipping referenced project 'System.Management.Automation'.
  Complete in 806ms.
  Running formatters.
  Running Code Style analysis.
  Determining diagnostics...
  Running 2 analyzers on Microsoft.Management.Infrastructure.CimCmdlets.
  Running 1 analyzers on Microsoft.PowerShell.CoreCLR.Eventing.
  Running 1 analyzers on System.Management.Automation.
  Complete in 23706ms.
  Fixing diagnostics...
  Complete in 7ms.
  Analysis complete in 23719ms.
  Complete in 23971ms.
  Formatted 0 of 38 files.
  Format complete in 28890ms.

@JoeRobich
Copy link
Member

@xtqqczze I think that CimCmdlets may not have any unused usings. I added dotnet_diagnostic.IDE0005.severity = error as line 198 of the .editorconfig and introduced an unused using, using System.Collections; as line 6 of CimGetInstance.cs. The following is my output (--check flag has the side-effect of logging analyzer diagnostics):

~/Source/PowerShell [master ≡ +2 ~2 -0 !]> dotnet-format ./src/Microsoft.Management.Infrastructure.CimCmdlets/ --fix-style error -v diag --check
  The dotnet runtime version is '5.0.0'.
  The dotnet CLI version is '5.0.100'.
  Using MSBuild.exe located in '/usr/local/share/dotnet/sdk/5.0.100/'.
  Formatting code files in workspace '/Users/joeyrobichaud/Source/PowerShell/src/Microsoft.Management.Infrastructure.CimCmdlets/Microsoft.Management.Infrastructure.CimCmdlets.csproj'.
  Loading workspace.
  Complete in 3147ms.
  Determining formattable files.
  Skipping referenced project 'System.Management.Automation'.
  Complete in 601ms.
  Running formatters.
  Running Code Style analysis.
  Determining diagnostics...
  Running 3 analyzers on Microsoft.Management.Infrastructure.CimCmdlets.
  Running 1 analyzers on System.Management.Automation.
  CimGetInstance.cs(6,1): Using directive is unnecessary. (IDE0005)
  Complete in 16580ms.
  Analysis complete in 16580ms.
  Complete in 16757ms.
  Formatted code file '/Users/joeyrobichaud/Source/PowerShell/src/Microsoft.Management.Infrastructure.CimCmdlets/CimGetInstance.cs'.
  Formatted 1 of 38 files.
  Format complete in 20506ms.

@JoeRobich
Copy link
Member

@xtqqczze Your logs did make me realize that we were running analyzers against referenced projects even when the workspace is a specific project. Opened #865 to address this behavior.

@xtqqczze
Copy link

@xtqqczze I think that CimCmdlets may not have any unused usings. I added dotnet_diagnostic.IDE0005.severity = error as line 198 of the .editorconfig and introduced an unused using, using System.Collections; as line 6 of CimGetInstance.cs. The following is my output (--check flag has the side-effect of logging analyzer diagnostics):

Yeah, I had added an unused using. Comparing our logs, it appears as if dotnet-format is not loading the analyzer on my system. In my case the log reads Running 2 analyzers on Microsoft.Management.Infrastructure.CimCmdlets., in your case Running 3 analyzers.

@JoeRobich
Copy link
Member

@xtqqczze Can you share your branch with me where you are making the changes to .editorconfig? I want to be sure I am running against the same code as you are.

@xtqqczze
Copy link

@JoeRobich I've pushed to dotnet-format-GH809. With vagrant, I executing the following on a clean windows10 machine:

choco install git dotnet-5.0-sdk
exit
dotnet tool install -g dotnet-format --version 5.0.156601 --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json
git clone -b dotnet-format-GH809 https://github.com/xtqqczze/PowerShell.git
cd PowerShell
Import-Module build.psm1
Start-PSBuild -ResGen
dotnet-format .\src\Microsoft.Management.Infrastructure.CimCmdlets\ --fix-style error -v diag --check

@dominikjeske
Copy link
Author

dominikjeske commented Nov 17, 2020

@JoeRobich Thanks for response - I have done some testing.

dotnet_diagnostic.SA1507.severity = warning

@JoeRobich
Copy link
Member

JoeRobich commented Nov 17, 2020

We do support running 3rd party analyzers (see https://github.com/dotnet/format/tree/master/docs#3rd-party-analysis). Ensure the analyzers are added to the projects with a <PackageReference>. Ensure that NuGet restore has been run.

Added note that analyzer packages set the default severity for their analyzers, so without intentional configuration you will see lots of new warnings.

~/Source/PowerShell [dotnet-format-GH809 ≡ +1 ~2 -0 !]> dotnet-format ./src/Microsoft.Management.Infrastructure.CimCmdlets/ --fix-analyzers warn -v diag --check

  The dotnet runtime version is '5.0.0'.
  The dotnet CLI version is '5.0.100'.
  Using MSBuild.exe located in '/usr/local/share/dotnet/sdk/5.0.100/'.
  Formatting code files in workspace '/Users/joeyrobichaud/Source/PowerShell/src/Microsoft.Management.Infrastructure.CimCmdlets/Microsoft.Management.Infrastructure.CimCmdlets.csproj'.
  Loading workspace.
  Complete in 3201ms.
  Determining formattable files.
  Skipping referenced project 'System.Management.Automation'.
  Complete in 597ms.
  Running formatters.
  Running Analyzer Reference analysis.
  Determining diagnostics...
  Running 306 analyzers on Microsoft.Management.Infrastructure.CimCmdlets.
  AssemblyInfo.cs(1,1): The file header XML is invalid. (SA1633)
  NewCimSessionOptionCommand.cs(27,6): Code should not contain empty statements (SA1106)
  NewCimSessionOptionCommand.cs(25,9): Use trailing comma in multi-line initializers (SA1413)
  CimResultObserver.cs(156,32): A field should not follow a constructor (SA1201)
  CimResultObserver.cs(198,35): A field should not follow a constructor (SA1201)
  CimSessionOperations.cs(1276,5): Do not use regions (SA1124)
  CimSessionOperations.cs(1123,5): Do not use regions (SA1124)
  CimSessionOperations.cs(1193,9): Do not use regions (SA1124)
  CimSessionOperations.cs(1200,5): Do not use regions (SA1124)
  CimResultObserver.cs(161,58): Documentation text should end with a period (SA1629)
  CimResultObserver.cs(136,42): Documentation text should end with a period (SA1629)
...

@xtqqczze
Copy link

@JoeRobich I see you have analyzers working on dotnet-format-GH809. There must be some difference in our environment, I have tried without success using a clean windows10 vm as well as WSL 2.

@JoeRobich
Copy link
Member

JoeRobich commented Nov 17, 2020

I did the following:

    <PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
  • ran a dotnet restore of the powershell.sln
  • invoked dotnet-format with --fix-analyzers warn

If you make the configuration changes and push updates to your branch, I would be happy to try running against them.

@JoeRobich JoeRobich added Resolved-Fixed This issue has been fixed and removed Needs More Info This issue needs additional details to be actionable labels Nov 17, 2020
@dominikjeske
Copy link
Author

dominikjeske commented Nov 18, 2020

I don't know what I'm doing wrong but this is not working for me. I put my test project here https://github.com/dominikjeske/Samples/tree/main/DotnetFormat and log from my console. I want to remove new lines from here https://github.com/dominikjeske/Samples/blob/main/DotnetFormat/ConsoleApp8/Program.cs#L9 by the rule set here https://github.com/dominikjeske/Samples/blob/main/DotnetFormat/ConsoleApp8/.editorconfig#L6

dotnet-format

@jmarolf
Copy link
Contributor

jmarolf commented Nov 18, 2020

@dominikjeske in your screenshot you did this:

dotnet-format --fix-style warn 

That will only fix built-in stlye warnings. You need to do this to fix third-party analyzer warnings like stylecop:

dotnet-format --fix-analyzers warn 

@dominikjeske
Copy link
Author

Yes this is it - now everything is working like a charm

@xtqqczze
Copy link

xtqqczze commented Nov 18, 2020

@dominikjeske Neither code style analyzers nor 3rd party analyzers are working for me.

c:\source>git clone -q https://github.com/dominikjeske/Samples.git dominikjeske-Samples

c:\source>cd dominikjeske-Samples\DotnetFormat

c:\source\dominikjeske-Samples\DotnetFormat>git checkout -q 75770f41ae5cd6b16d9c701d3d70737fe2e6daa3

c:\source\dominikjeske-Samples\DotnetFormat>dotnet --version
5.0.100

c:\source\dominikjeske-Samples\DotnetFormat>dotnet-format --version
5.0.156601+acb1b73526b0182799b63b5412c2ac9989aa19bc

c:\source\dominikjeske-Samples\DotnetFormat>dotnet-format . --fix-analyzers warn -v diag --check
  The dotnet runtime version is '5.0.0'.
  The dotnet CLI version is '5.0.100'.
  Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\5.0.100\'.
  Formatting code files in workspace 'c:\source\dominikjeske-Samples\DotnetFormat\ConsoleApp8.sln'.
  Loading workspace.
  Complete in 2430ms.
  Determining formattable files.
  Complete in 257ms.
  Running formatters.
  Running Analyzer Reference analysis.
  Determining diagnostics...
  Complete in 15ms.
  Analysis complete in 16ms.
  Complete in 42ms.
  Formatted 0 of 3 files.
  Format complete in 2730ms.

c:\source\dominikjeske-Samples\DotnetFormat>dotnet-format . --fix-style warn -v diag --check
  The dotnet runtime version is '5.0.0'.
  The dotnet CLI version is '5.0.100'.
  Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\5.0.100\'.
  Formatting code files in workspace 'c:\source\dominikjeske-Samples\DotnetFormat\ConsoleApp8.sln'.
  Loading workspace.
  Complete in 2195ms.
  Determining formattable files.
  Complete in 235ms.
  Running formatters.
  Running Code Style analysis.
  Determining diagnostics...
  Running 3 analyzers on ConsoleApp8.
  Complete in 1350ms.
  Analysis complete in 1353ms.
  Complete in 1526ms.
  Formatted 0 of 3 files.
  Format complete in 3957ms.

@dominikjeske
Copy link
Author

I don't know what is the diffrence ;( Logs from my machine...

image

@JoeRobich
Copy link
Member

Neither code style analyzers nor 3rd party analyzers are working for me.

For 3rd party analyzers to work you need to run a dotnet restore to restore the analyzer packages.

@xtqqczze
Copy link

Neither code style analyzers nor 3rd party analyzers are working for me.

For 3rd party analyzers to work you need to run a dotnet restore to restore the analyzer packages.

@JoeRobich I missed the restore in this instance, however still no files are formatted as shown by the output of git diff:

c:\source\dominikjeske-Samples\DotnetFormat>dotnet restore
  Determining projects to restore...
  Restored c:\source\dominikjeske-Samples\DotnetFormat\ConsoleApp8\ConsoleApp8.csproj (in 344 ms).

c:\source\dominikjeske-Samples\DotnetFormat>dotnet-format . --fix-analyzers warn -v diag --check
  The dotnet runtime version is '5.0.0'.
  The dotnet CLI version is '5.0.100'.
  Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\5.0.100\'.
  Formatting code files in workspace 'c:\source\dominikjeske-Samples\DotnetFormat\ConsoleApp8.sln'.
  Loading workspace.
  Complete in 5266ms.
  Determining formattable files.
  Complete in 452ms.
  Running formatters.
  Running Analyzer Reference analysis.
  Determining diagnostics...
  Running 178 analyzers on ConsoleApp8.
  ConsoleApp8\Program.cs(3,1): Code should not contain multiple blank lines in a row (SA1507)
  ConsoleApp8\Program.cs(9,1): Code should not contain multiple blank lines in a row (SA1507)
  ConsoleApp8\Program.cs(1,1): The file header is missing or not located at the top of the file. (SA1633)
  ConsoleApp8\Program.cs(1,1): Using directive should appear within a namespace declaration (SA1200)
  ConsoleApp8\Program.cs(2,1): Using directive should appear within a namespace declaration (SA1200)
  ConsoleApp8\Program.cs(1,1): Using directives should be ordered alphabetically by the namespaces. (SA1210)
  ConsoleApp8\Program.cs(7,11): Element 'Program' should declare an access modifier (SA1400)
  ConsoleApp8\Program.cs(8,5): An opening brace should not be followed by a blank line. (SA1505)
  ConsoleApp8\Program.cs(7,11): Elements should be documented (SA1600)
  ConsoleApp8\Program.cs(13,21): Element 'Main' should declare an access modifier (SA1400)
  Complete in 2601ms.
  Analysis complete in 2603ms.
  Complete in 3208ms.
  Formatted code file 'c:\source\dominikjeske-Samples\DotnetFormat\ConsoleApp8\Program.cs'.
  Formatted 1 of 3 files.
  Format complete in 8929ms.

c:\source\dominikjeske-Samples\DotnetFormat>git diff

@JoeRobich
Copy link
Member

however still no files are formatted

The --check option puts dotnet-format into a mode where it does not save changes but simply reports whether there were issues via the exit code. See https://github.com/dotnet/format/tree/master/docs#validate-formatting

@xtqqczze
Copy link

however still no files are formatted

The --check option puts dotnet-format into a mode where it does not save changes but simply reports whether there were issues via the exit code. See https://github.com/dotnet/format/tree/master/docs#validate-formatting

Ah yes, of course. I have the third-party analyzers working now. The code style analyzers however are still not working:

c:\source\dominikjeske-Samples\DotnetFormat>dotnet-format . --fix-style warn -v diag
  The dotnet runtime version is '5.0.0'.
  The dotnet CLI version is '5.0.100'.
  Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\5.0.100\'.
  Formatting code files in workspace 'c:\source\dominikjeske-Samples\DotnetFormat\ConsoleApp8.sln'.
  Loading workspace.
  Complete in 3251ms.
  Determining formattable files.
  Complete in 331ms.
  Running formatters.
  Running Code Style analysis.
  Determining diagnostics...
  Running 3 analyzers on ConsoleApp8.
  Complete in 424ms.
  Fixing diagnostics...
  Complete in 9ms.
  Analysis complete in 434ms.
  Complete in 2948ms.
  Formatted 0 of 3 files.
  Format complete in 6537ms.

@JoeRobich
Copy link
Member

The code style analyzers however are still not working

@xtqqczze What errors are you expecting to be reported? I cloned the Samples repo and checked out the SHA from you logs. Like you no violations were reported. I then added using System.Collections; to Program.cs, reran format, and saw IDE0005 reported as expected.

@xtqqczze
Copy link

The code style analyzers however are still not working

@xtqqczze What errors are you expecting to be reported? I cloned the Samples repo and checked out the SHA from you logs. Like you no violations were reported. I then added using System.Collections; to Program.cs, reran format, and saw IDE0005 reported as expected.

My bad, I had assumed there were already some style violations in @dominikjeske's project. All seems to be working now. Tomorrow I will try again on PowerShell repo.

@iSazonov
Copy link

Neither code style analyzers nor 3rd party analyzers are working for me.

For 3rd party analyzers to work you need to run a dotnet restore to restore the analyzer packages.

@JoeRobich I missed the restore in this instance, however still no files are formatted as shown by the output of git diff:

This seems to be a common mistake. Can it always run by default as it is done at compilation?

@JoeRobich
Copy link
Member

This seems to be a common mistake. Can it always run by default as it is done at compilation?

@iSazonov We are already tracking this feature request with #848 =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolved-Fixed This issue has been fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants