From 6661105a887e50222566c2900755e203d5f4a490 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Tue, 23 Feb 2021 10:42:46 -0800 Subject: [PATCH] Suppress illink roslyn analyzer warnings Using the same property that suppresses the linker warnings. Includes a manual dependency update to unblock https://github.com/dotnet/sdk/pull/15955. --- eng/Version.Details.xml | 4 +-- eng/Versions.props | 2 +- .../targets/Microsoft.NET.ILLink.targets | 16 ++++++++-- .../GivenThatWeWantToPublishASingleFileApp.cs | 31 +++++++++++++++++-- .../GivenThatWeWantToRunILLink.cs | 17 +++++++--- 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/eng/Version.Details.xml b/eng/Version.Details.xml index 2cd22c471327..d40ea0186a3b 100644 --- a/eng/Version.Details.xml +++ b/eng/Version.Details.xml @@ -104,11 +104,11 @@ https://github.com/mono/linker - 2cb420dbabb4ec86d41a1b431696e58f45f031cd + ed016c44d716a4179ba6d0f532af0af5ebb04541 https://github.com/mono/linker - 2cb420dbabb4ec86d41a1b431696e58f45f031cd + ed016c44d716a4179ba6d0f532af0af5ebb04541 https://github.com/dotnet/runtime diff --git a/eng/Versions.props b/eng/Versions.props index 230f424dcfd0..f22958756690 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -73,7 +73,7 @@ - 6.0.0-alpha.1.21116.1 + 6.0.100-preview.2.21122.1 $(MicrosoftNETILLinkTasksPackageVersion) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.ILLink.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.ILLink.targets index 700274545491..859b9bf84620 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.ILLink.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.ILLink.targets @@ -40,6 +40,17 @@ Copyright (c) .NET Foundation. All rights reserved. false + + true + + + + + + $(NoWarn);IL2026 + + - - $(NoWarn);IL2026 $(NoWarn);IL2041;IL2042;IL2043;IL2056 diff --git a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishASingleFileApp.cs b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishASingleFileApp.cs index 30ce4417182b..9fd9f09598fa 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishASingleFileApp.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishASingleFileApp.cs @@ -510,9 +510,27 @@ public void ILLink_analyzer_warnings_are_produced(string targetFramework) var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); publishCommand .Execute(RuntimeIdentifier) - .Should() - .HaveStdOutContaining("(8,13): warning IL3000") - .And.HaveStdOutContaining("(9,13): warning IL3001"); + .Should().Pass() + .And.HaveStdOutContaining("(9,13): warning IL3000") + .And.HaveStdOutContaining("(10,13): warning IL3001"); + } + + [RequiresMSBuildVersionTheory("16.8.0")] + [InlineData("net6.0")] + public void ILLink_linker_analyzer_warnings_are_not_produced(string targetFramework) + { + var projectName = "ILLinkAnalyzerWarningsApp"; + var testProject = CreateTestProjectWithAnalyzerWarnings(targetFramework, projectName, true); + // Inactive linker settings should have no effect on the linker analyzer, + // unless PublishTrimmed is also set. + testProject.AdditionalProperties["SuppressTrimAnalysisWarnings"] = "false"; + var testAsset = _testAssetsManager.CreateTestProject(testProject); + + var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); + publishCommand + .Execute(RuntimeIdentifier) + .Should().Pass() + .And.NotHaveStdOutContaining("IL2026"); } private TestProject CreateTestProjectWithAnalyzerWarnings(string targetFramework, string projectName, bool isExecutable) @@ -526,6 +544,7 @@ private TestProject CreateTestProjectWithAnalyzerWarnings(string targetFramework testProject.SourceFiles[$"{projectName}.cs"] = @" using System.Reflection; +using System.Diagnostics.CodeAnalysis; class C { static void Main() @@ -533,6 +552,12 @@ static void Main() var a = Assembly.LoadFrom(""/some/path/not/in/bundle""); _ = a.Location; _ = a.GetFiles(); + ProduceLinkerAnalysisWarning(); + } + + [RequiresUnreferencedCode(""Linker analysis warning"")] + static void ProduceLinkerAnalysisWarning() + { } }"; diff --git a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs index 2d1f140cbb8b..31e11aa04876 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs @@ -217,10 +217,11 @@ public void ILLink_analysis_warnings_are_disabled_by_default(string targetFramew var rid = EnvironmentInfo.GetCompatibleRid(targetFramework); var testProject = CreateTestProjectWithAnalysisWarnings(targetFramework, projectName); + testProject.AdditionalProperties["PublishTrimmed"] = "true"; var testAsset = _testAssetsManager.CreateTestProject(testProject); var publishCommand = new PublishCommand(testAsset); - publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true", "/p:PublishTrimmed=true") + publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true") .Should().Pass() // trim analysis warnings are disabled .And.NotHaveStdOutContaining("warning IL2075") @@ -238,10 +239,12 @@ public void ILLink_accepts_option_to_enable_analysis_warnings(string targetFrame var rid = EnvironmentInfo.GetCompatibleRid(targetFramework); var testProject = CreateTestProjectWithAnalysisWarnings(targetFramework, projectName); + testProject.AdditionalProperties["PublishTrimmed"] = "true"; + testProject.AdditionalProperties["SuppressTrimAnalysisWarnings"] = "false"; var testAsset = _testAssetsManager.CreateTestProject(testProject); var publishCommand = new PublishCommand(testAsset); - publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true", "/p:PublishTrimmed=true", "/p:SuppressTrimAnalysisWarnings=false") + publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true") .Should().Pass() .And.HaveStdOutMatching("warning IL2075.*Program.IL_2075") .And.HaveStdOutMatching("warning IL2026.*Program.IL_2026.*Testing analysis warning IL2026") @@ -776,9 +779,11 @@ public void ILLink_can_treat_warnings_not_as_errors(string targetFramework) var publishCommand = new PublishCommand(testAsset); publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true", "/p:PublishTrimmed=true", "/p:SuppressTrimAnalysisWarnings=false", - "/p:TreatWarningsAsErrors=true", "/p:WarningsNotAsErrors=IL2075") + "/p:TreatWarningsAsErrors=true", "/p:WarningsNotAsErrors=\"IL2075;IL2026\"") .Should().Fail() - .And.HaveStdOutContaining("error IL2026") + // This warning is produced by both the analyzer and the linker. Don't make it an error for the test. + .And.HaveStdOutContaining("warning IL2026") + .And.HaveStdOutContaining("error IL2043") .And.HaveStdOutContaining("warning IL2075"); } @@ -847,8 +852,10 @@ public void ILLink_can_treat_warnings_as_errors_independently(string targetFrame var publishCommand = new PublishCommand(testAsset); publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true", "/p:PublishTrimmed=true", "/p:SuppressTrimAnalysisWarnings=false", - "/p:TreatWarningsAsErrors=true", "/p:ILLinkTreatWarningsAsErrors=false") + "/p:TreatWarningsAsErrors=true", "/p:ILLinkTreatWarningsAsErrors=false", "/p:NoWarn=IL2026") .Should().Pass() + // This warning is produced by both the analyzer and the linker. Ignore it for this test. + .And.NotHaveStdOutContaining("IL2026") .And.HaveStdOutContaining("warning IL2075"); }