Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 5141dcb

Browse files
committed
Add private assets from package reference
Fixes NuGet/Home#5103 This is only performed for resolved NuGet artifacts that have one of the specified values in the referenced NuGet specification at https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets. This minimizes the impact of the new inference and preserves existing behavior.
1 parent 0f8fa8f commit 5141dcb

13 files changed

Lines changed: 195 additions & 40 deletions

build.proj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<RunCodeAnalysis Condition=" '$(RunCodeAnalysis)' == ''">false</RunCodeAnalysis>
1717
<IntermediateOutputPath>.nuget\</IntermediateOutputPath>
1818
<PackagesPath>$(IntermediateOutputPath)packages</PackagesPath>
19-
<CommonBuildProperties>WarningLevel=0;NoWarn=1591;Out=$(Out);Configuration=$(Configuration);RunCodeAnalysis=$(RunCodeAnalysis);PackageOutputPath=$(Out);NuGetBuildTasksPackTargets=NO-SDK-PACK</CommonBuildProperties>
19+
<CommonBuildProperties>WarningLevel=0;NoWarn=1591;Out=$(Out);Configuration=$(Configuration);RunCodeAnalysis=$(RunCodeAnalysis);PackageOutputPath=$(Out)</CommonBuildProperties>
2020
<DefaultImportance Condition=" '$(DefaultImportance)' == '' ">high</DefaultImportance>
2121
<PS>%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe</PS>
2222
</PropertyGroup>
@@ -47,7 +47,7 @@
4747

4848
<Target Name="Package">
4949
<MakeDir Directories="$(Out)" Condition="!Exists('$(Out)')" />
50-
<MSBuild Projects="src\Build\NuGet.Build.Packaging.Tasks\NuGet.Build.Packaging.Tasks.csproj" Targets="Pack" Properties="$(CommonBuildProperties);PackOnBuild=true" />
50+
<MSBuild Projects="src\Build\NuGet.Build.Packaging.Tasks\NuGet.Build.Packaging.Tasks.csproj" Targets="Pack" Properties="$(CommonBuildProperties);PackOnBuild=true;NuGetBuildTasksPackTargets=NO-SDK-PACK" />
5151
<MSBuild Projects="src\VisualStudio\NuGet.Packaging.VisualStudio.15\NuGet.Packaging.VisualStudio.15.csproj" Properties="$(CommonBuildProperties);PackOnBuild=true" />
5252
<ItemGroup>
5353
<OutputFile Include="$(Out)\*.nupkg" />
@@ -79,6 +79,7 @@
7979
<NuGetRestore Include="src\Build\NuGet.Build.Packaging.Tests\Scenarios\given_a_multi_platform_solution\forms.sln" />
8080
<NuGetRestore Include="src\Build\NuGet.Build.Packaging.Tests\Scenarios\given_a_library_with_json_dependencies\project.json" />
8181
<MSBuildRestore Include="src\Build\NuGet.Build.Packaging.Tests\Scenarios\given_a_packaging_project_with_netstandard\a.nuproj" />
82+
<MSBuildRestore Include="src\Build\NuGet.Build.Packaging.Tests\Scenarios\given_a_library_with_private_assets_reference\a.csproj" />
8283

8384
<MSBuildRestore Include="external\ApiIntersect\ApiIntersect.sln" />
8485
<MSBuildRestore Include="src\Build\NuGet.Build.Packaging.sln" />

src/Build/NuGet.Build.Packaging.Tasks/NuGet.Build.Packaging.Inference.targets

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,22 @@ Copyright (c) .NET Foundation. All rights reserved.
3737
<PropertyGroup>
3838
<GetPackageContentsDependsOn>
3939
$(GetPackageContentsDependsOn);
40+
_PrimaryOutputFrameworkSpecific;
4041
InferPackageContents
4142
</GetPackageContentsDependsOn>
4243
</PropertyGroup>
4344

44-
<Target Name="InferPackageContents" DependsOnTargets="$(InferPackageContentsDependsOn)" Returns="@(PackageFile)">
45+
<Target Name="_PrimaryOutputFrameworkSpecific" Returns="$(PrimaryOutputFrameworkSpecific)">
4546
<!-- Determine whether primary output is framework specific -->
4647
<ItemGroup Condition="'$(PrimaryOutputFrameworkSpecific)' == ''">
4748
<_PrimaryOutputKind Include="@(PackageItemKind->'%(FrameworkSpecific)')" Condition="'%(Identity)' == '$(PrimaryOutputKind)'" />
4849
</ItemGroup>
4950
<PropertyGroup Condition="'$(PrimaryOutputFrameworkSpecific)' == ''">
5051
<PrimaryOutputFrameworkSpecific>@(_PrimaryOutputKind)</PrimaryOutputFrameworkSpecific>
5152
</PropertyGroup>
53+
</Target>
5254

55+
<Target Name="InferPackageContents" DependsOnTargets="$(InferPackageContentsDependsOn)" Returns="@(PackageFile)">
5356
<!-- Ensure TargetPath -->
5457
<AssignTargetPath Files="@(Content)" RootFolder="$(MSBuildProjectDirectory)"
5558
Condition="'%(Content.Pack)' == 'true' or
@@ -125,7 +128,7 @@ Copyright (c) .NET Foundation. All rights reserved.
125128
</_InferredPackageFile>
126129

127130

128-
<_InferredPackageFile Include="@(PackageReference)" Condition="'%(Identity)' != 'NuGet.Build.Packaging'">
131+
<_InferredPackageFile Include="@(PackageReference)" Condition="'%(PackageReference.Identity)' != 'NuGet.Build.Packaging' and '%(PackageReference.PrivateAssets)' != 'all'">
129132
<Kind>Dependency</Kind>
130133
</_InferredPackageFile>
131134

@@ -153,19 +156,62 @@ Copyright (c) .NET Foundation. All rights reserved.
153156
</ItemGroup>
154157
</Target>
155158

159+
<Target Name="_CollectPrimaryOutputRelatedFiles" DependsOnTargets="BuildOnlySettings;ResolveReferences" Returns="@(_PrimaryOutputRelatedFile)">
160+
<ItemGroup>
161+
<_PrimaryOutputRelatedFile Include="@(ReferencePath);@(_ReferenceRelatedPaths)" />
162+
</ItemGroup>
163+
</Target>
164+
165+
<Target Name="InferPrimaryOutputDependencies"
166+
Inputs="@(_PrimaryOutputRelatedFile)"
167+
Outputs="%(_PrimaryOutputRelatedFile.NuGetPackageId)"
168+
Returns="@(_InferredPackageFile)"
169+
DependsOnTargets="_CollectPrimaryOutputRelatedFiles">
170+
<ItemGroup>
171+
<_NuGetPackageId Include="@(_PrimaryOutputRelatedFile -> '%(NuGetPackageId)')" />
172+
</ItemGroup>
173+
<PropertyGroup>
174+
<_NuGetPackageId>@(_NuGetPackageId -> Distinct())</_NuGetPackageId>
175+
</PropertyGroup>
176+
<ItemGroup>
177+
<_PrimaryPackageReference Include="@(PackageReference)" Condition="'$(_NuGetPackageId)' != '' and '%(Identity)' == '$(_NuGetPackageId)'" />
178+
</ItemGroup>
179+
<PropertyGroup>
180+
<_PrivateAssets>@(_PrimaryPackageReference -> '%(PrivateAssets)')</_PrivateAssets>
181+
</PropertyGroup>
182+
183+
<ItemGroup Condition="'$(_PrivateAssets)' == 'all'">
184+
<_InferredPackageFile Include="@(_PrimaryOutputRelatedFile)" Condition="'%(_PrimaryOutputRelatedFile.FrameworkFile)' != 'true'">
185+
<Kind>$(PrimaryOutputKind)</Kind>
186+
<FrameworkSpecific>$(PrimaryOutputFrameworkSpecific)</FrameworkSpecific>
187+
</_InferredPackageFile>
188+
</ItemGroup>
189+
190+
<ItemGroup Condition="'$(_PrivateAssets)' != 'all' and '$(_PrivateAssets)' != 'none' and '$(_PrivateAssets)' != ''">
191+
<!-- In this case, we only add files that have a matching path to the private assets value.
192+
i.e. for Mono.Options, PrivateAssets=lib, we'll include the file if its full path contains
193+
'Mono.Options\*\lib\*', meaning the file is a lib. -->
194+
<_InferredPackageFile Include="@(_PrimaryOutputRelatedFile)" Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('%(_PrimaryOutputRelatedFile.FullPath)', '$(_NuGetPackageId)\\.+\\$(_PrivateAssets)\\.*')) == 'true'">
195+
<Kind>$(PrimaryOutputKind)</Kind>
196+
<FrameworkSpecific>$(PrimaryOutputFrameworkSpecific)</FrameworkSpecific>
197+
</_InferredPackageFile>
198+
</ItemGroup>
199+
</Target>
200+
156201
<Target Name="_SetInferPackageContentsDependsOn" AfterTargets="_SetPropertiesFromCapabilities">
157202
<PropertyGroup>
158203
<!-- NOTE: this avoids taking dependencies on targets that are only available when the project supports the concept of references -->
159204
<_SupportsReferences Condition="
160205
$(_AllProjectCapabilities.Contains('AssemblyReferences')) or
161206
$(_AllProjectCapabilities.Contains('COMReferences')) or
162207
$(_AllProjectCapabilities.Contains('ProjectReferences')) or
208+
$(_AllProjectCapabilities.Contains('PackageReferences')) or
163209
$(_AllProjectCapabilities.Contains('WinRTReferences')) or
164210
$(_AllProjectCapabilities.Contains('SDKReferences'))">true</_SupportsReferences>
165211

166-
<!-- Only depend on ResolveReferences if we need to include framework references -->
167-
<InferPackageContentsDependsOn Condition="'$(IncludeFrameworkReferencesInPackage)' == 'true' and '$(_SupportsReferences)' == 'true'">
168-
ResolveReferences
212+
<InferPackageContentsDependsOn Condition="'$(_SupportsReferences)' == 'true'">
213+
ResolveReferences;
214+
InferPrimaryOutputDependencies
169215
</InferPackageContentsDependsOn>
170216
<InferPackageContentsDependsOn>
171217
$(InferPackageContentsDependsOn);

src/Build/NuGet.Build.Packaging.Tasks/NuGet.Build.Packaging.Tasks.targets

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,12 @@
2727
<Import Project="$(OutputPath)NuGet.Build.Packaging.targets"
2828
Condition="'$(PackOnBuild)' == 'true' and Exists('$(OutputPath)NuGet.Build.Packaging.targets')" />
2929

30-
<Target Name="AddBuiltOutput" BeforeTargets="GetPackageContents" DependsOnTargets="AllProjectOutputGroups" Returns="@(PackageFile)">
30+
<Target Name="CleanContents" BeforeTargets="GetPackageContents" DependsOnTargets="InferPackageContents" Returns="@(PackageFile)">
3131
<ItemGroup>
32-
<PackageFile Include="$(OutputPath)ICSharpCode.Decompiler.dll">
33-
<PackagePath>build\ICSharpCode.Decompiler.dll</PackagePath>
34-
</PackageFile>
35-
<PackageFile Include="$(OutputPath)ICSharpCode.NRefactory.CSharp.dll">
36-
<PackagePath>build\ICSharpCode.NRefactory.CSharp.dll</PackagePath>
37-
</PackageFile>
38-
<PackageFile Include="$(OutputPath)ICSharpCode.NRefactory.dll">
39-
<PackagePath>build\ICSharpCode.NRefactory.dll</PackagePath>
40-
</PackageFile>
41-
<PackageFile Include="$(OutputPath)Mono.Cecil.dll">
42-
<PackagePath>build\Mono.Cecil.dll</PackagePath>
43-
</PackageFile>
44-
<PackageFile Include="$(OutputPath)Mono.Options.dll">
45-
<PackagePath>build\Mono.Options.dll</PackagePath>
46-
</PackageFile>
32+
<PackageFile Remove="@(PackageFile)" Condition="
33+
$([System.String]::new('%(PackageFile.NuGetPackageId)').StartsWith('Microsoft')) or
34+
$([System.String]::new('%(PackageFile.NuGetPackageId)').StartsWith('Newtonsoft')) or
35+
$([System.String]::new('%(PackageFile.NuGetPackageId)').StartsWith('NuGet'))" />
4736
</ItemGroup>
4837
</Target>
4938

src/Build/NuGet.Build.Packaging.Tasks/NuGet.Build.Packaging.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ Copyright (c) .NET Foundation. All rights reserved.
369369
<Target Name="Pack" DependsOnTargets="$(PackDependsOn)" Returns="@(PackageTargetPath)" Condition="'$(IsPackable)' == 'true'">
370370
<MakeDir Directories="$(PackageOutputPath)" Condition="!Exists('$(PackageOutputPath)')" />
371371
<PropertyGroup>
372-
<NuspecFile Condition="'$(EmitNuspec)' == 'true' And '$(NuspecFile)' ==''">$(OutputPath)\$(PackageId).nuspec</NuspecFile>
372+
<NuspecFile Condition="'$(EmitNuspec)' == 'true' And '$(NuspecFile)' == ''">$(OutputPath)\$(PackageId).nuspec</NuspecFile>
373373
</PropertyGroup>
374374
<CreatePackage Manifest="@(PackageTargetPath)" NuspecFile="$(NuspecFile)" Contents="@(_PackageContent)" TargetPath="@(PackageTargetPath->'%(FullPath)')">
375375
<Output TaskParameter="OutputPackage" ItemName="_PackageTargetPath" />

src/Build/NuGet.Build.Packaging.Tests/Builder.NuGetizer.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using System.Reflection;
56
using System.Threading;
67
using Microsoft.Build.Execution;
78
using Microsoft.Build.Framework;
9+
using NuGet.Build.Packaging;
810
using Xunit;
911
using Xunit.Abstractions;
1012
using Xunit.Sdk;
@@ -53,12 +55,16 @@ public static TargetResult BuildScenario(string scenarioName, object properties
5355
logger = new TestOutputLogger(null);
5456
}
5557

56-
if (properties != null)
57-
return new TargetResult(Build(projectOrSolution, target,
58-
properties: properties.GetType().GetProperties().ToDictionary(prop => prop.Name, prop => prop.GetValue(properties).ToString()),
59-
logger: logger), target, logger);
60-
else
61-
return new TargetResult(Build(projectOrSolution, target, logger: logger), target, logger);
58+
var buildProps = properties?.GetType().GetProperties()
59+
.ToDictionary(prop => prop.Name, prop => prop.GetValue(properties).ToString())
60+
?? new Dictionary<string, string>();
61+
62+
buildProps[nameof(ThisAssembly.Project.Properties.NuGetRestoreTargets)] = ThisAssembly.Project.Properties.NuGetRestoreTargets;
63+
buildProps[nameof(ThisAssembly.Project.Properties.NuGetTargets)] = ThisAssembly.Project.Properties.NuGetTargets;
64+
65+
return new TargetResult(Build(projectOrSolution, target,
66+
properties: buildProps,
67+
logger: logger), target, logger);
6268
}
6369

6470
public class TargetResult : ITargetResult

src/Build/NuGet.Build.Packaging.Tests/CreatePackageTests.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ public void when_creating_package_with_non_framework_secific_dependency_then_con
177177

178178
Assert.NotNull(manifest);
179179
Assert.Equal(2, manifest.Metadata.DependencyGroups.Count());
180-
Assert.Equal(NuGetFramework.UnsupportedFramework, manifest.Metadata.DependencyGroups.First().TargetFramework);
180+
Assert.True(
181+
manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.AnyFramework ||
182+
manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.UnsupportedFramework);
183+
181184
Assert.Equal(NuGetFramework.Parse(".NETFramework,Version=v4.5"), manifest.Metadata.DependencyGroups.Last().TargetFramework);
182185

183186
Assert.Equal(1, manifest.Metadata.DependencyGroups.First().Packages.Count());
@@ -212,11 +215,12 @@ public void when_creating_package_with_any_framework_secific_dependency_then_con
212215

213216
var manifest = ExecuteTask();
214217

215-
//Process.Start("notepad.exe", task.NuspecFile);
216-
217218
Assert.NotNull(manifest);
218219
Assert.Equal(2, manifest.Metadata.DependencyGroups.Count());
219-
Assert.Equal(NuGetFramework.UnsupportedFramework, manifest.Metadata.DependencyGroups.First().TargetFramework);
220+
Assert.True(
221+
manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.AnyFramework ||
222+
manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.UnsupportedFramework);
223+
220224
Assert.Equal(NuGetFramework.Parse(".NETFramework,Version=v4.5"), manifest.Metadata.DependencyGroups.Last().TargetFramework);
221225

222226
Assert.Equal(1, manifest.Metadata.DependencyGroups.First().Packages.Count());
@@ -274,9 +278,6 @@ public void when_creating_package_with_empty_dependency_groups_then_succeeds()
274278

275279
var manifest = ExecuteTask();
276280

277-
//if (Debugger.IsAttached)
278-
// Process.Start("notepad.exe", task.NuspecFile);
279-
280281
Assert.NotNull(manifest);
281282
Assert.Equal(4, manifest.Metadata.DependencyGroups.Count());
282283
Assert.All(manifest.Metadata.DependencyGroups, d => Assert.Empty(d.Packages));

src/Build/NuGet.Build.Packaging.Tests/NuGet.Build.Packaging.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFramework>net46</TargetFramework>
66
<SignAssembly>false</SignAssembly>
77
<PublicSign>false</PublicSign>
8+
<NuGetBuildTasksPackTargets>NO-SDK-PACK</NuGetBuildTasksPackTargets>
89
</PropertyGroup>
910
<ItemGroup>
1011
<Reference Include="NuGet.Build.Packaging.Tasks">

src/Build/NuGet.Build.Packaging.Tests/NuGet.Build.Packaging.Tests.targets

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
<AddSyntheticProjectReferencesForSolutionDependencies>false</AddSyntheticProjectReferencesForSolutionDependencies>
99
</PropertyGroup>
1010

11+
<ItemGroup>
12+
<ThisAssemblyProjectProperty Include="NuGetRestoreTargets" />
13+
<ThisAssemblyProjectProperty Include="NuGetTargets" />
14+
</ItemGroup>
15+
1116
<Target Name="CopyScenarioFiles" AfterTargets="Build">
1217
<ItemGroup>
1318
<ScenarioFile Include="Scenarios\**\*.*" Exclude="Scenarios\**\bin\**\*.*;Scenarios\**\obj\**\*.*" />

src/Build/NuGet.Build.Packaging.Tests/Scenarios/Scenario.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" Condition="'$(IsPortable)' != 'true'" />
88
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets"
99
Condition="'$(IsPortable)' == 'true'" />
10+
<!-- Ensure we get proper NuGet behavior always -->
11+
<Import Project="$(NuGetRestoreTargets)" Condition="'$(IsRestoreTargetsFileLoaded)' != 'true' and '$(NuGetRestoreTargets)' != ''" />
12+
<Import Project="$(NuGetTargets)" Condition="'$(ResolveNuGetPackageAssetsDependsOn)' == '' and '$(NuGetTargets)' != ''" />
1013

1114
<Import Project="$(NuGetTargetsPath)\NuGet.Build.Packaging.targets" Condition="'$(NuGetBuildPackagingTargetsImported)' != 'true' and Exists('$(NuGetTargetsPath)\NuGet.Build.Packaging.targets')" />
1215

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Scenario.props))\Scenario.props" />
4+
<PropertyGroup>
5+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Mono.Options">
9+
<Version>*</Version>
10+
<PrivateAssets>all</PrivateAssets>
11+
</PackageReference>
12+
<PackageReference Include="Newtonsoft.Json">
13+
<Version>*</Version>
14+
<PrivateAssets>lib</PrivateAssets>
15+
</PackageReference>
16+
<PackageReference Include="xunit">
17+
<Version>*</Version>
18+
</PackageReference>
19+
</ItemGroup>
20+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Scenario.targets))\Scenario.targets" />
21+
</Project>

0 commit comments

Comments
 (0)