Contains a repro for a problem where Roslyn analyzer warnings are wrongly shown as errors in Visual Studio IntelliSense design-time-build.
This repo contains a code sample showing a problem in Visual Studio IntelliSense behavior. The sample contains a class library, which builds just fine when running through the compiler, but Visual Studio displays IntelliSense errors in the Error Windows and in the code with underlined squiggles.
Start Visual Studio 2019/2022 and execute the following steps:
- Clone this repo
- Open the
src\RoslynIntellisenseErrorsRepro.sln
solution - Build the solution
- Close and reopen the solution
- Open the
\src\VisualStudioIntellisenseErrors\FxCopRules\CA1062\Violation.cs
file - Open the
Visual Studio ErrorList
- Change the issue filter to show
Build + IntelliSense
Important: It is required to reopen Visual Studio after the repo was cloned and the solution build for the first time (including first package restore)
Once the IntelliSense has completed its initialization the ErrorList will show one detected issue for the rule CA1062 as Error
despite the ruleset configuration sets its severity to Warning
and the project property TreatWarningsAsErrors
configured as <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
:
General preparation steps:
- Install .NET Framework 4.8 SDK
- Use a latest version of the Visual Studio 2019/2022 (for version details see separate section below)
- Create a new project using the Windows Class Library (.NET Framework) template
- Add Analyzer package
Microsoft.CodeAnalysis.NetAnalyzers, 6.0.0
(nuget.org) - Create a new ruleset file configured with e.g. rule
CA1062
set toWarning
(see default.ruleset). The configuration of all the other rules from the same NuGet Analyzer package does not matter for the purpose of this repro. - Add code which violates against the
CA1062
rule (see Violation.cs).
Special preparation steps required to reveal the problem:
- Create build\default.props containing the following properties:
<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
- Add the following properties to the project file:
<Import Project="..\..\build\default.props" />
<PropertyGroup>
<CodeAnalysisRuleSet>..\default.ruleset</CodeAnalysisRuleSet>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>
Microsoft Visual Studio Enterprise 2019
Version 16.11.7
VisualStudio.16.Release/16.11.7+31911.196
Microsoft .NET Framework
Version 4.8.03752
Installed Version: Enterprise
Microsoft Visual Studio Enterprise 2022
Version 17.0.2
VisualStudio.17.Release/17.0.2+31919.166
Microsoft .NET Framework
Version 4.8.03752
Installed Version: Enterprise