-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Enable Microsoft.CodeAnalysis.NetAnalyzers code analyzers #12892
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
Conversation
6acc529 to
8faf1b4
Compare
|
In #11916 my intention is to force Roslyn analyzers at build time to exclude regressions in code formatting and code style. Here you add analyzers but not enable/activate them for build or design time (otherwise the build would fail). Also we already have editorconfig file - why do we need the ruleset. I am not sure that we need this in the time. It would great if you grabbed #11916 and improved it. |
|
@iSazonov yes this PR has a similar intention as #11916, but review should be easier 😛 Please see 6acc529 for proof analyzers are activated, The |
|
@xtqqczze Thanks for clarify! I see the behavior of VS was changed since 16.5. But It is not clear has EditorConfig a priority on CodeAnalysisRuleSet? |
|
@xtqqczze Please have a look at the failing builds. |
|
@xtqqczze Could you please check with CA1001 that EditorConfig has a priority over CodeAnalysisRuleSet? |
|
@iSazonov 2c736cb shows EditorConfig has a priority over CodeAnalysisRuleSet |
|
In the case I suggest to use default ruleset and do all customizations in EditorConfig. |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
2c736cb to
8faf1b4
Compare
I think using Ruleset only makes more sense than specifiying default rules in Ruleset and overriding some rules in EditorConfig. Currently there are issues with using EditorConfig to configure diagnostics, for example TreatWarningsAsErrors is ignored: dotnet/roslyn#43051. |
|
@iSazonov I think we should use a Ruleset until EditorConfig support for diagnostics is mature. |
|
@xtqqczze It seems the dotnet/roslyn#43051 issue consider a specific scenario to have an exclusion list of warnings. It is not our case. We use TreatWarningsAsErrors to force all warning as errors. |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
Based on `\microsoft.codeanalysis.netanalyzers\5.0.0-rc2.20460.4\rulesets\AllRulesDefault.ruleset`
94b5b6a to
705b3dc
Compare
This comment has been minimized.
This comment has been minimized.
|
@xtqqczze I see MSFT updated documentation.
So we already got this.
I think we already got this too and we should do nothing to evolve Roslyn Analyzer. We can check this by forcing some rule in our EditorConfig file. Can you check and confirm? https://github.com/dotnet/roslyn-analyzers#microsoftcodeanalysisnetanalyzers
So We have no need to reference the package. |
|
Please also see https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview#code-style-analysis
|
I have removed the PackageReference, and pushed commit be5c5c3 which proves that a change in the ruleset will break the build. |
I have tested EditorConfig configuration with TreatWarningsAsErrors and it is still not working with VS2019 16.8.0 Preview 5.0. The alternative is to use a global AnalyzerConfig (see #13835). See also: dotnet/roslyn#47707 |
|
@xtqqczze TreatWarningsAsErrors issue is about using warning severity only but we should use error severity if we want to force a rule. Yes? I mean why would we use warning severity if we want to stop build with error? |
That is a goal of this PR, to enforce various rules during build. |
If |
|
Since we get a fix for TreatWarningsAsErrors in near future I believe we have no need a workaround today. In EditorConfig we can set severity to error if we want to force a rule. |
|
We can use |
|
@xtqqczze Why would we need a global config? We can use our .editorconfig file. If we set a severity of a rule to error we will stop the build if the rule violates. Yes? |
Using a global AnalyzerConfig avoids bloating EditorConfig with analyzer configuration. Documentation is at https://docs.microsoft.com/dotnet/fundamentals/code-analysis/configuration-files#naming
|
Bloating? Our file has 194 lines and 142 lines of them with analyzer configuration. I don't expect we will add a lot of rules in near time (maybe ~10 to force new syntax). I believe can live with the EditorConfig while. Also global config does not still supported in VS GUI that is bad UX. |
I think we would want to specify the severity for each rule explicitly (like #13835). There are 246 rules in AnalyzerConfig is a simple text file, I'm not sure what IDE support is needed. The way I see it different configuration kinds should be in separate files:
|
Currently we use defaults and it is well. Do you expect that we will change defaults and set severity to error for 246 rules?
It is mandatory requirement that IDE support Roslyn analyzer configuration. I know VS Code supports EditorConfig but I am not sure it supports GlobalConfig. Can you confirm? |
|
I tried VS Code with GlobalConfig and it does not work at all :-( OmniSharp uses defaults and ignore the global config. I will open the feature request in OmniSharp repo. @xtqqczze Now I suggest to put all global config content to a end of out EditorConfig file. After we get the feature in OmniSharp we will be able to move the rules back to a global config file. |
|
@xtqqczze EditorConfig works but GlobalConfig doesn't work. I tested with |
|
@iSazonov I tested in VS Code 1.50.1 (Omnisharp 1.37.3) with As expected, a diagnostic with code CA1507 is listed in the problems tab. Then I set In my environment, OmniSharp is using the MSBuild instance provided by Perhaps you could you try setting |
I updated to latest preview and now it works. Thanks! |
Support for |
PR Summary
Minimal first step to enable code analyzers in build and live analysis.
Analyzers.propsCodeAnalysis.rulesetwith rules copied frommicrosoft.codeanalysis.netanalyzers\5.0.0-rtm.20502.2\rulesets\AllRulesDefault.rulesetPR Context
see also: #11916
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.