Don't enable ILLink analyzers for unsupported TFMs (#87071)
authorSven Boemer <sbomer@gmail.com>
Thu, 8 Jun 2023 17:10:58 +0000 (10:10 -0700)
committerGitHub <noreply@github.com>
Thu, 8 Jun 2023 17:10:58 +0000 (10:10 -0700)
* Don't enable ILLink analyzers for unsupported TFMs

This adds a condition to EnableSingleFileAnalyzer to prevent it from requiring
the ILLink pack that is only supported on netcoreapp3.0 and above.

Adding a condition on the TFM requires moving the setting into a targets file.

Also consolidates more of the analyzer logic into this file.

---------

Co-authored-by: Viktor Hofer <viktor.hofer@microsoft.com>
Directory.Build.props
Directory.Build.targets
docs/project/analyzers.md
eng/Analyzers.targets [moved from eng/Analyzers.props with 65% similarity]
eng/illink.targets
src/coreclr/nativeaot/Test.CoreLib/src/Directory.Build.targets [moved from src/coreclr/nativeaot/Test.CoreLib/src/Directory.Build.props with 81% similarity]
src/tools/illink/src/ILLink.Tasks/ILLink.Tasks.csproj

index 902f793..66120a0 100644 (file)
   <!-- RepositoryEngineeringDir isn't set when Installer tests import this file. -->
   <Import Project="$(RepositoryEngineeringDir)native\naming.props" />
   <Import Project="$(RepositoryEngineeringDir)Subsets.props" />
-  <Import Project="$(RepositoryEngineeringDir)Analyzers.props" />
 
   <PropertyGroup>
     <CoreLibSharedDir>$([MSBuild]::NormalizeDirectory('$(LibrariesProjectRoot)', 'System.Private.CoreLib', 'src'))</CoreLibSharedDir>
     <IsShipping Condition="'$(IsTestProject)' == 'true' or '$(IsTestSupportProject)' == 'true' or '$(IsPublishedAppTestProject)' == 'true'">false</IsShipping>
   </PropertyGroup>
 
-  <ItemGroup Condition="'$(IsTestProject)' == 'true'">
-    <EditorConfigFiles Remove="$(RepositoryEngineeringDir)CodeAnalysis.src.globalconfig" />
-    <EditorConfigFiles Include="$(RepositoryEngineeringDir)CodeAnalysis.test.globalconfig" />
-  </ItemGroup>
-
   <PropertyGroup>
     <!-- Treat as a generator project if either the parent or the parent parent directory is named gen. -->
     <IsGeneratorProject Condition="$([System.IO.Path]::GetFileName('$(MSBuildProjectDirectory)')) == 'gen' or
index 14d08b1..88c4fb8 100644 (file)
@@ -7,6 +7,7 @@
     <ImportDirectoryBuildTargets>false</ImportDirectoryBuildTargets>
   </PropertyGroup>
 
+  <Import Project="$(RepositoryEngineeringDir)Analyzers.targets" />
   <Import Project="Sdk.targets" Sdk="Microsoft.DotNet.Arcade.Sdk" />
   <Import Project="$(RepositoryEngineeringDir)liveBuilds.targets" />
   <Import Project="$(RepositoryEngineeringDir)generators.targets" />
@@ -35,7 +36,6 @@
       unconditionally in Microsoft.NETCoreSdk.BundledVersions.props.
     -->
     <NETCoreAppMaximumVersion>$(MajorVersion).$(MinorVersion)</NETCoreAppMaximumVersion>
-    <EnableNETAnalyzers Condition="'$(EnableNETAnalyzers)' == ''">$(RunAnalyzers)</EnableNETAnalyzers>
     <!-- SDK sets product to assembly but we want it to be our product name -->
     <Product>Microsoft%AE .NET</Product>
     <!-- Use the .NET product branding version for informational version description -->
index 61318e8..cef5cc1 100644 (file)
@@ -4,7 +4,7 @@ This repo relies on [.NET Compiler Platform analyzers](https://learn.microsoft.c
 
 To add an analyzer package to the build:
 1. Select a package you want to employ, for example https://www.nuget.org/packages/SonarAnalyzer.CSharp/.  This analyzer package's name is `SonarAnalyzer.CSharp` and the latest version as of this edit is `8.50.0.58025`.
-2. Add a `PackageReference` entry to <https://github.com/dotnet/runtime/blob/main/eng/Analyzers.props>, e.g.
+2. Add a `PackageReference` entry to <https://github.com/dotnet/runtime/blob/main/eng/Analyzers.targets>, e.g.
     ```XML
     <PackageReference Include="SonarAnalyzer.CSharp" Version="8.50.0.58025" PrivateAssets="all" />
     ```
similarity index 65%
rename from eng/Analyzers.props
rename to eng/Analyzers.targets
index 2af4c0c..4ca3df7 100644 (file)
@@ -2,9 +2,12 @@
   <PropertyGroup>
     <!-- Disable analyzers in sourcebuild -->
     <RunAnalyzers Condition="'$(DotNetBuildFromSource)' == 'true'">false</RunAnalyzers>
+    <EnableNETAnalyzers Condition="'$(EnableNETAnalyzers)' == ''">$(RunAnalyzers)</EnableNETAnalyzers>
   </PropertyGroup>
   <PropertyGroup Condition="'$(RunAnalyzers)' != 'false'">
-    <EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
+    <EnableSingleFileAnalyzer Condition="
+      '$(EnableSingleFileAnalyzer)' == '' And
+      '$(TargetFrameworkIdentifier)' == '.NETCoreApp'">true</EnableSingleFileAnalyzer>
   </PropertyGroup>
   <ItemGroup Condition="'$(RunAnalyzers)' != 'false'">
     <EditorConfigFiles Include="$(MSBuildThisFileDirectory)CodeAnalysis.src.globalconfig" />
@@ -13,4 +16,8 @@
     <PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="$(MicrosoftCodeAnalysisCSharpCodeStyleVersion)" PrivateAssets="all" />
     <PackageReference Include="StyleCop.Analyzers" Version="$(StyleCopAnalyzersVersion)" PrivateAssets="all" />
   </ItemGroup>
+  <ItemGroup Condition="'$(IsTestProject)' == 'true'">
+    <EditorConfigFiles Remove="$(RepositoryEngineeringDir)CodeAnalysis.src.globalconfig" />
+    <EditorConfigFiles Include="$(RepositoryEngineeringDir)CodeAnalysis.test.globalconfig" />
+  </ItemGroup>
 </Project>
index 99c7f97..c837c96 100644 (file)
@@ -2,8 +2,11 @@
 
   <PropertyGroup>
     <IsTrimmable Condition="'$(IsTrimmable)' == ''">true</IsTrimmable>
-    <!-- Don't use SDK's trimming functionality. -->
+    <!-- Don't use SDK's trimming functionality. 
+         TODO: Remove this once we build using preview 6 SDK. -->
     <_IsTrimmingEnabled>false</_IsTrimmingEnabled>
+    <!-- Same fix for preview 6 SDK. -->
+    <_RequiresILLinkPack>false</_RequiresILLinkPack>
     <PrepareResourcesDependsOn>_EmbedILLinkXmls;$(PrepareResourcesDependsOn)</PrepareResourcesDependsOn>
     <TargetsTriggeredByCompilation Condition="'$(DesignTimeBuild)' != 'true'">$(TargetsTriggeredByCompilation);ILLinkTrimAssembly</TargetsTriggeredByCompilation>
 
@@ -1,5 +1,5 @@
 <Project>
-  <Import Project="..\..\Directory.Build.props" />
+  <Import Project="..\..\Directory.Build.targets" />
   <ItemGroup>
     <EditorConfigFiles Remove="$(RepositoryEngineeringDir)CodeAnalysis.src.globalconfig" />
     <EditorConfigFiles Include="$(RepositoryEngineeringDir)CodeAnalysis.test.globalconfig" />
index ff511c3..528e4b1 100644 (file)
@@ -22,6 +22,8 @@
     <IncludeBuildOutput>false</IncludeBuildOutput>
     <IncludeMultiTargetRoslynComponentTargets>false</IncludeMultiTargetRoslynComponentTargets>
     <TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);AddBuildOutputToToolsPackage</TargetsForTfmSpecificContentInPackage>
+    <!-- Disable analyzer to prevent a circular dependency due to PackageReference to the LKG Microsoft.NET.ILLink.Tasks. -->
+    <EnableSingleFileAnalyzer>false</EnableSingleFileAnalyzer>
   </PropertyGroup>
 
   <ItemGroup>