dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.84k stars 666 forks source link

Request for Clarification on Roslyn Analyzer Ruleset implementation #3174

Closed majeric closed 1 day ago

majeric commented 5 years ago

Issue Description

Documentation Request:

1) Please Clarify how to manage rulesets for the rosyln analyzers. Does one need to modify the csproj files to include the custom ruleset file.

2) There's not a lot of documentation on how to write a rulset xml files.

3) Given that this vscode extension is used by Unity a lot, is there a ruleset file that a project tries to load by default. Unity regenerates the csproj file frequently so any manual edits of the project file gets thrown out frequently. It would be nice to have a default file to fall back on so that one isn't editing the csproj files

Environment information

VSCode version: 1.36.1 C# Extension: 1.20.0

Dotnet Information .NET Core SDK (reflecting any global.json): Version: 2.1.701 Commit: 8cf7278aa1 Runtime Environment: OS Name: Windows OS Version: 10.0.17134 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\2.1.701\ Host (useful for support): Version: 2.1.12 Commit: ccea2e606d .NET Core SDKs installed: 2.1.4 [C:\Program Files\dotnet\sdk] 2.1.104 [C:\Program Files\dotnet\sdk] 2.1.502 [C:\Program Files\dotnet\sdk] 2.1.505 [C:\Program Files\dotnet\sdk] 2.1.508 [C:\Program Files\dotnet\sdk] 2.1.602 [C:\Program Files\dotnet\sdk] 2.1.701 [C:\Program Files\dotnet\sdk] .NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download
Visual Studio Code Extensions |Extension|Author|Version| |---|---|---| |azure-account|ms-vscode|0.8.4| |beautify|HookyQR|1.5.0| |bracket-pair-colorizer|CoenraadS|1.0.61| |code-runner|formulahendry|0.9.11| |cpptools|ms-vscode|0.24.0| |csharp|ms-vscode|1.20.0| |csharpextensions|jchannon|1.3.0| |csharpfixformat|Leopotam|0.0.81| |debugger-for-chrome|msjsdiag|4.11.6| |dotnet|formulahendry|0.0.4| |electron-debug|kodetech|18.11.1| |eppz-code|eppz|1.2.41| |es7-react-js-snippets|dsznajder|2.3.0| |heroku-command|pkosta2005|0.0.8| |JavaScriptSnippets|xabikos|1.7.2| |jshint|dbaeumer|0.10.20| |LiveServer|ritwickdey|5.6.1| |MagicPython|magicstack|1.1.0| |Material-theme|zhuangtongfa|2.26.0| |msbuild-project-tools|tintoy|0.2.55| |mssql|ms-mssql|1.6.0| |node-module-intellisense|leizongmin|1.5.0| |node-modules-context-menu|darthyody|0.0.3| |nodejs-extension-pack|waderyan|0.1.9| |npm-intellisense|christian-kohler|1.3.0| |path-intellisense|christian-kohler|1.4.2| |plantuml|jkeys089|1.8.1| |plantuml|jebbs|2.11.2| |powershell|ms-vscode|2019.5.0| |prettier-vscode|esbenp|1.9.0| |prettify-json|mohsen1|0.0.3| |python|tht13|0.2.3| |qub-msbuild|qub|0.6.3| |search-node-modules|jasonnutter|1.3.0| |svg|jock|0.1.6| |svgpreview|kisstkondoros|0.2.0| |svn-scm|johnstoncode|1.54.3| |tslint|eg2|1.0.44| |unity-debug|Unity|2.7.2| |unity-snippets|YclepticStudios|0.1.2| |unity-tools|Tobiah|1.1.1| |vscode-cosmosdb|ms-azuretools|0.10.2| |vscode-dashboard|kruemelkatze|1.4.1| |vscode-eslint|dbaeumer|1.9.0| |vscode-exec-node|miramac|0.5.1| |vscode-html-css|ecmel|0.2.0| |vscode-npm-script|eg2|0.3.8| |vscode-nuget-package-manager|jmrog|1.1.6| |vscode-react-native|msjsdiag|0.10.1| |vscode-svgviewer|cssho|2.0.0| |xml|DotJoshJohnson|2.5.0| |yog-plantuml-highlight|Yog|0.0.5|;
savpek commented 5 years ago

One very major improvement for this may be language support for .ruleset files.

savpek commented 5 years ago

Answer for 1: They need to be added to .csproj files. Basically they work same way as in visual studio. Visual studio has additional tooling on top of them however.

rchande commented 5 years ago

@majeric Thanks for filing this, I think this is a great issue. @savpek I'll write something up and ask you to review it.

filipw commented 5 years ago

we could consider adding support for "global" rulesets via Omnisharp.json file too

majeric commented 5 years ago

One of the common places this is used is by Unity which auto generates csproj files frequently. Any suggestion on how this may be worked around?

caiolmd commented 4 years ago

Unity auto generates csproj files, its impossible to use rulesets. @majeric Did you manage to find a workaround? Im all for the "rulesets via omnisharp.json" idea.

majeric commented 4 years ago

@caiolmd No, I have a script in my Unity project that injects the file in the csproj.

kabamjdenton commented 4 years ago

@caiolmd I should have mentioned that there's an undocumented event in Unity called:

private static void OnGeneratedCSProjectFiles()

You can use it to parse your csproj files and inject the ruleset in your csproj files when unity initializes them. It's a clumsy work around and it's far from perfect.

nbaum commented 4 years ago

It's possible to add solution-wide ruleset support in a way Unity won't overwrite, by creating a Directory.Build.props file in the project root. e.g.

<Project>
  <PropertyGroup>
    <CodeAnalysisRuleset>$(MSBuildThisFileDirectory)Rules.ruleset</CodeAnalysisRuleset>
  </PropertyGroup>
</Project>
samlletas commented 4 years ago

Found another alternative by creating an .editorconfig file in the project's root directory, for example:

# Rules configuration
[*.cs]
dotnet_diagnostic.IDE0051.severity = none
dotnet_diagnostic.RCS1213.severity = warning
dotnet_diagnostic.RCS1169.severity = error

# Prevent analyzers from running in these directories
[Assets/ProCamera2D/**.cs]
generated_code = true

[Assets/Tayx/**.cs]
generated_code = true

Note: In order for this to work enable .editorconfig support in vscode by setting "omnisharp.enableEditorConfigSupport": true in your settings.json.

Here is some documentation about editorconfig:

Smoovsky commented 3 years ago

Found another alternative by creating an .editorconfig file in the project's root directory, for example:

# Rules configuration
[*.cs]
dotnet_diagnostic.IDE0051.severity = none
dotnet_diagnostic.RCS1213.severity = warning
dotnet_diagnostic.RCS1169.severity = error

# Prevent analyzers from running in these directories
[Assets/ProCamera2D/**.cs]
generated_code = true

[Assets/Tayx/**.cs]
generated_code = true

Note: In order for this to work enable .editorconfig support in vscode by setting "omnisharp.enableEditorConfigSupport": true in your settings.json.

Here is some documentation about editorconfig:

Nice one! I'm surprised that only a tiny section of M$ doc mentioned how to control the severity of a code issue in .editorconfig

andyleejordan commented 3 years ago

Don't forget (like I did) that OmniSharp doesn't care about the EnableNETAnalyzers setting in your .csproj file, so you need both of these extension settings for your .NET Analyzer diagnostics configured in .editorconfig to show up on VS Code's "Problems" pane:

"omnisharp.enableRoslynAnalyzers": true,
"omnisharp.enableEditorConfigSupport": true,