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

Way to explicitly reference IDE analyzers from project file #34907

Closed
kindermannhubert opened this issue Apr 10, 2019 · 10 comments
Closed

Way to explicitly reference IDE analyzers from project file #34907

kindermannhubert opened this issue Apr 10, 2019 · 10 comments
Assignees
Milestone

Comments

@kindermannhubert
Copy link

Version Used: VS 2019 Professional (16.0.1)

Is there any way to explicitly reference IDE analyzers from project file (probably with Analyzer tag) so IDE analyzers will be used also while building solution outside of VS only with msbuild? I was discussing this problem in #33558.

Custom analyzers can be activated by adding following tag to csproj (I'm not interested in installing it as VS extension):

<Analyzer Include="somePathToAnalyzer\CustomAnalyzer.dll" />

IDE analyzers do not have to be included this way. They are present automatically (while building from VS).
Both types (custom and IDE) can be adjusted (=change default severity of analyzers) by ruleset file. Ruleset file is included by following tag in csproj:

<CodeAnalysisRuleSet>somePathToRulesetFile\MyRuleset.ruleset</CodeAnalysisRuleSet>

When working in VS it works (except the issue #33558 - when IDE analyzers has Warning/Error severity set up they are not breaking a build).
Example (simple program with error, but build succeeds):
issue1
issue2

My problem probably comes from the very same reason why the build is not broken in example above.
When I build solution just with msbuild as

msbuild MySolution.sln

no error is detected and build succeeds.

What I tried:

  1. I added Analyzer element inside project file with reference to Microsoft.CodeAnalysis.CSharp.Features.dll from VS. But during build it was missing references of this lib.
  2. I added also the references (located also in VS folder). But those references have another version than version which is referenced by Microsoft.CodeAnalysis.CSharp.Features.dll (e.g. System.Composition.AttributedModel (1.0.31) is referenced, but 1.0.27 version is shipped with VS). There is no(?) simple way to guide MsBuild to use binding redirection.
  3. Finally I downloaded Microsoft.CodeAnalysis.CSharp.Features.dll through Nuget with its dependencies and added those dependencies to Analyzer tags as well (which is thing would really like not to do). Another problem appeared - MsBuild has already loaded some assemblies by itself (from some MsBuild/???/Roslyn folder - do not remember exactly) which collided with NuGet assemblies.
  4. I removed those colliding references from Analyzer references and it looked better but I was still getting some weird null reference exceptions from CSharpValidateFormatStringDiagnosticAnalyzer.
  5. I gave up.
@jmarolf
Copy link
Contributor

jmarolf commented Apr 10, 2019

I believe you want this nuget package

<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="3.1.0-beta3-19210-01" />

You can add it to your project using the following command

dotnet add package Microsoft.CodeAnalysis.CSharp.CodeStyle --version 3.1.0-beta3-19210-01 --source https://dotnet.myget.org/F/roslyn/api/v3/index.json

@sharwell
Copy link
Member

@jmarolf That will work for FormattingAnalyzer, but will not include the others. Currently there is no command line support for any of the IDE analyzers except for IDE0055 (Fix formatting).

@Youssef1313
Copy link
Member

@jmarolf That will work for FormattingAnalyzer, but will not include the others. Currently there is no command line support for any of the IDE analyzers except for IDE0055 (Fix formatting).

@sharwell, This is now outdated right? If so, I think the issue can be closed.

@mavasani
Copy link
Contributor

@Youssef1313 Yes, that is correct. This feature is now implemented and analyzers are available in the .NET 5 SDK as well as via a NuGet package: https://docs.microsoft.com/dotnet/fundamentals/code-analysis/overview#code-style-analysis

Closing this issue.

@stewartadam
Copy link

@mavasani are code style/IDE errors expected to be provided via the NuGet package? I see only CA* errors being produced when setting my SDK to .NET Core 3.1.

@mavasani
Copy link
Contributor

@stewartadam If you are using the new SDK-style projects, then you can enable the code style rules on build by following the steps here: https://docs.microsoft.com/dotnet/fundamentals/code-analysis/overview#enable-on-build

@stewartadam
Copy link

@mavasani, can you confirm those are expected to work from Microsoft.CodeAnalysis.NetAnalyzers NuGet package with a .NET Core 3.1 SDK? I do not see any code style (IDE*) errors being produce on build in that scenario, only code quality (CA*). Using .NET 5 SDK (without NuGet package) does have IDE* errors enforced on build.

This seems to line up with what I can see in the roslyn-analyzers repo - the only reference to IDE0001 for example is in its own editorconfig.

Oddly, I can use dotnet-format -s warn --check to reveal code style (IDE*) errors on .NET Core 3.1 SDKs though.

@Youssef1313
Copy link
Member

Microsoft.CodeAnalysis.NetAnalyzers doesn't contain any IDExxxx rules. It indeed only contains CAxxxx rules.
I believe IDExxxx are supported to be enforced in build starting with .NET 5 SDK.

@JoeRobich
Copy link
Member

Oddly, I can use dotnet-format -s warn --check to reveal code style (IDE*) errors on .NET Core 3.1 SDKs though.

dotnet-format ships code style analyzers separately from the SDK, which is why it is working for you.

@stewartadam
Copy link

dotnet-format ships code style analyzers separately from the SDK, which is why it is working for you.

That's what I've found as well -- I just meant it was odd in that given dotnet-format can detect them with its analyzers, the IDE* rules can be packaged, but they are not included in NetAnalyzers.

I feel the docs need clarifying, as the docs talk about "code analysis" (consisting of code quality and code style rules) and mention the NuGet package can be used on older SDKs, but it's never mentioned that code style is dropped from the NuGet package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants