Use the analyzer from the SDK when available (#18885)

* Use the analyzer from the SDK when available

This prevents a build warning when building a project that contains a reference to
Microsoft.AspNetCore.Components and a netcoreapp3.0 or newer targeting Web project.

The Web SDK implicitly adds the Components.Analyzer for netcoreapp3.0 or newer targeting projects.
If the project additionally referenced this package (directly or transitively), the package would
set up a property that prevented the implicit analyzer reference. This prevented the analyzer from
being referenced twice.

There were two issues with the current approach:

a) The props file wasn't propogated via buildTransitive. Consequently transitive project references
would reference two copies of the analyzer. When these were different versions, it resulted in a compiler
warning.

b) Forward looking, this prevents newer versions of the analyzer shipped from the SDK from ever being used.
This is particularly problematic since apps are likely to reference component libraries that were previously
compiled against 3.x.

This change attempts to mitigate both of these issues:

a) We add a buildTransitive so our build targets flow
b) We knock out the analyzer added by the package if the SDK's already added it.

Fixes https://github.com/dotnet/aspnetcore/issues/18563

* Update Microsoft.AspNetCore.Components.Analyzers.targets

* Update Microsoft.AspNetCore.Components.Analyzers.targets

* Add a description

* Update Microsoft.AspNetCore.Components.Analyzers.targets
This commit is contained in:
Pranav K 2020-02-14 08:34:23 -08:00 committed by GitHub
parent d10a352e8c
commit 1027e5372f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 5 deletions

View File

@ -18,6 +18,7 @@
<ItemGroup>
<None Include="$(TargetPath)" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="build/netstandard2.0/*" Pack="true" PackagePath="build/netstandard2.0" />
<None Include="buildTransitive/netstandard2.0/*" Pack="true" PackagePath="buildTransitive/netstandard2.0" />
</ItemGroup>
</Project>

View File

@ -1,5 +0,0 @@
<Project>
<PropertyGroup>
<DisableImplicitComponentsAnalyzers>true</DisableImplicitComponentsAnalyzers>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,34 @@
<Project>
<!--
The Web.SDK unconditionally adds the components analyzer to all qualifying Web projects. A project qualifiies if it targets netcoreapp3.0 or later and does not have a flag that
prevents the implicit analyzer from being added. We want to ensure that when a Web project also references this package, typically as the result of referencing a component class library,
the analyzer in the SDK always wins.
"ResolvedAnalyzers" contains the list of analyzers that are resolved from package references. "Analyzer" contains the list of of analyzers used by the compiler.
The ResolveLockFileAnalyzers target copies items from ResolvedAnalyzers to Analyzer.
The target below runs before ResolveLockFileAnalyzers executes. If it discovers that the Analyzer item group was previously populated by the WebSDK, it prevents the analyzer from the package
from being used by removing it from the ResolvedAnalyzers list.
Note: Prior to 3.1.3, this package prevented the analyzer from the SDK from being used by setting DisableImplicitComponentsAnalyzers=true. This would have prevented newer versions of the analyzer
from the SDK from ever being used. A compilation of SDK and package version combinations:
Package Version SDK Version Result
==================================================
Earlier than 3.1.3 Any Package wins
3.1.3 or newer Any SDK wins
-->
<Target Name="_RemoveComponentAnalyzer" BeforeTargets="ResolveLockFileAnalyzers" Condition="'@(Analyzer->Count())' != '0'">
<ItemGroup>
<_AspNetCoreComponentsAnalyzerName Include="$([System.IO.Path]::GetFileNameWithoutExtension('%(Analyzer.Identity)'))" />
</ItemGroup>
<PropertyGroup>
<_AspNetCoreComponentsAnalyzerPath>$([MSBuild]::NormalizePath('$(MSBuildThisFileDirectory)../../analyzers/dotnet/cs/Microsoft.AspNetCore.Components.Analyzers.dll'))</_AspNetCoreComponentsAnalyzerPath>
</PropertyGroup>
<ItemGroup Condition="'@(_AspNetCoreComponentsAnalyzerName->AnyHaveMetadataValue('Identity', 'Microsoft.AspNetCore.Components.Analyzers'))' == 'true'">
<ResolvedAnalyzers Remove="$(_AspNetCoreComponentsAnalyzerPath)" />
</ItemGroup>
</Target>
</Project>

View File

@ -0,0 +1,3 @@
<Project>
<Import Project="..\..\build\netstandard2.0\Microsoft.AspNetCore.Components.Analyzers.targets" />
</Project>