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

Allow using latest illink analyzer on netstandard projects #33158

Merged
merged 3 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Diagnostics.CodeAnalysis
{
/// <summary>
/// Indicates that the specified method requires dynamic access to code that is not referenced
/// statically, for example through <see cref="Reflection"/>.
/// </summary>
/// <remarks>
/// This allows tools to understand which methods are unsafe to call when removing unreferenced
/// code from an application.
/// </remarks>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class, Inherited = false)]
internal sealed class RequiresUnreferencedCodeAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="RequiresUnreferencedCodeAttribute"/> class
/// with the specified message.
/// </summary>
/// <param name="message">
/// A message that contains information about the usage of unreferenced code.
/// </param>
public RequiresUnreferencedCodeAttribute(string message)
{
Message = message;
}

/// <summary>
/// Gets a message that contains information about the usage of unreferenced code.
/// </summary>
public string Message { get; }

/// <summary>
/// Gets or sets an optional URL that contains more information about the method,
/// why it requires unreferenced code, and what options a consumer has to deal with it.
/// </summary>
public string? Url { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Diagnostics.CodeAnalysis;

class TrimmableNetStandardLibrary
{
public static void EntryPoint()
{
RequiresUnreferencedCode();
}


[RequiresUnreferencedCode("Requires unreferenced code")]
static void RequiresUnreferencedCode()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), testAsset.props))\testAsset.props" />

<PropertyGroup>
<!-- Test infra replaces this with the value of CurrentTargetFramework so we can compare against it below. -->
<CurrentTargetFramework>$(CurrentTargetFramework)</CurrentTargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<IsTrimmable>true</IsTrimmable>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AnalysisLevel>latest</AnalysisLevel>
</PropertyGroup>

<Target Name="_AddKnownILLinkPack" BeforeTargets="ProcessFrameworkReferences">
<ItemGroup>
<KnownILLinkPack Include="@(KnownILLinkPack)"
Condition="'%(TargetFramework)' == '$(CurrentTargetFramework)'"
TargetFramework="$(TargetFramework)" />
</ItemGroup>
</Target>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
Expand All @@ -15,6 +15,7 @@
using Microsoft.NET.Sdk.WorkloadManifestReader;
using Newtonsoft.Json;
using NuGet.Frameworks;
using NuGet.Versioning;

namespace Microsoft.NET.Build.Tasks
{
Expand Down Expand Up @@ -729,8 +730,11 @@ private ToolPackSupport AddToolPack(
}

// Before net8.0, ILLink analyzers shipped in a separate package.
// Add the analyzer package with version taken from KnownILLinkPack.
if (normalizedTargetFrameworkVersion < new Version(8, 0) && toolPackType is ToolPackType.ILLink)
// Add the analyzer package with version taken from KnownILLinkPack if the version is less than 8.0.0.
// The version comparison doesn't consider prerelease labels, so 8.0.0-foo will be considered equal to 8.0.0 and
// will not get the extra analyzer package reference.
if (toolPackType is ToolPackType.ILLink &&
new VersionComparer(VersionComparison.Version).Compare(NuGetVersion.Parse(packVersion), new NuGetVersion(8, 0, 0)) < 0)
{
var analyzerPackage = new TaskItem("Microsoft.NET.ILLink.Analyzers");
analyzerPackage.SetMetadata(MetadataKeys.Version, packVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ public void ILLink_fails_when_no_matching_pack_is_found(string targetFramework)
.And.HaveStdOutContaining("error NETSDK1195");
}

[RequiresMSBuildVersionTheory("17.0.0.32901")]
// TODO: enable netstandard2.0 once ProcessFrameworkReferences is fixed to run
// when tool packs are referenced. See https://github.com/dotnet/sdk/pull/33062.
// Currently netstandard2.0 skips ProcessFrameworkReference because FrameworkReference
// is empty.
// [InlineData("netstandard2.0")]
[InlineData("netstandard2.1")]
public void ILLink_can_use_latest_with_unsupported_target_framework(string targetFramework)
{
var projectName = "TrimmableNetstandardLibrary";
var testAsset = _testAssetsManager.CopyTestAsset(projectName)
.WithSource()
.WithTargetFramework(targetFramework);

var buildCommand = new BuildCommand(testAsset);
buildCommand.Execute("/p:IsTrimmable=true")
.Should().Pass()
.And.HaveStdOutContaining("warning IL2026");
}

[RequiresMSBuildVersionTheory("17.0.0.32901")]
[MemberData(nameof(SupportedTfms), MemberType = typeof(PublishTestUtils))]
public void PrepareForILLink_can_set_IsTrimmable(string targetFramework)
Expand Down
8 changes: 7 additions & 1 deletion src/Tests/Microsoft.NET.TestFramework/TestAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ public TestAsset WithSource()
File.Copy(srcFile, destFile, true);
}

string[][] Properties = { new string[] { "TargetFramework", "$(CurrentTargetFramework)", ToolsetInfo.CurrentTargetFramework }, new string[] { "RuntimeIdentifier", "$(LatestWinRuntimeIdentifier)", ToolsetInfo.LatestWinRuntimeIdentifier }, new string[] { "RuntimeIdentifier", "$(LatestLinuxRuntimeIdentifier)", ToolsetInfo.LatestLinuxRuntimeIdentifier }, new string[] { "RuntimeIdentifier", "$(LatestMacRuntimeIdentifier)", ToolsetInfo.LatestMacRuntimeIdentifier }, new string[] { "RuntimeIdentifier", "$(LatestRuntimeIdentifiers)", ToolsetInfo.LatestRuntimeIdentifiers } };
string[][] Properties = {
new string[] { "TargetFramework", "$(CurrentTargetFramework)", ToolsetInfo.CurrentTargetFramework },
new string[] { "CurrentTargetFramework", "$(CurrentTargetFramework)", ToolsetInfo.CurrentTargetFramework },
new string[] { "RuntimeIdentifier", "$(LatestWinRuntimeIdentifier)", ToolsetInfo.LatestWinRuntimeIdentifier },
new string[] { "RuntimeIdentifier", "$(LatestLinuxRuntimeIdentifier)", ToolsetInfo.LatestLinuxRuntimeIdentifier },
new string[] { "RuntimeIdentifier", "$(LatestMacRuntimeIdentifier)", ToolsetInfo.LatestMacRuntimeIdentifier },
new string[] { "RuntimeIdentifier", "$(LatestRuntimeIdentifiers)", ToolsetInfo.LatestRuntimeIdentifiers } };

foreach (string[] property in Properties)
{
Expand Down