Skip to content

Commit

Permalink
Pack runtime lib assets when inferring dependencies
Browse files Browse the repository at this point in the history
We were previously using a combination of @(ReferencePath) and @(_ReferenceRelatedPaths) to determine the candidate libs to pack as private, but this was incorrect since we would still sometimes pack ref assemblies.

Turns out that this is an issue that surfaced elsewhere (see NuGet/Home#9310 (comment)) which resulted in a new output group being available from the common MSBuild targets (see dotnet/msbuild#3069) that we can use instead to do this properly.

Closes #263
  • Loading branch information
kzu committed Feb 10, 2023
1 parent 094666e commit 1a69585
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
14 changes: 4 additions & 10 deletions src/NuGetizer.Tasks/NuGetizer.Inference.targets
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,12 @@ Copyright (c) .NET Foundation. All rights reserved.
<Pack Condition="'$(PackDependencies)' == 'false'">false</Pack>
<PrivateAssets />
</ImplicitPackageReference>
<ReferencePath>
<ReferenceCopyLocalPathsOutputGroupOutput>
<Facade>false</Facade>
<FrameworkFile>false</FrameworkFile>
<NuGetPackageId />
<Pack />
</ReferencePath>
<_ReferenceRelatedPaths>
<Facade>false</Facade>
<FrameworkFile>false</FrameworkFile>
<NuGetPackageId />
<Pack />
</_ReferenceRelatedPaths>
</ReferenceCopyLocalPathsOutputGroupOutput>
<PackageDependencies>
<ParentPackage />
</PackageDependencies>
Expand Down Expand Up @@ -373,11 +367,11 @@ Copyright (c) .NET Foundation. All rights reserved.

</Target>

<Target Name="_CollectPrimaryOutputDependencies" DependsOnTargets="BuildOnlySettings;RunResolvePackageDependencies;ResolveReferences" Returns="@(ImplicitPackageReference)">
<Target Name="_CollectPrimaryOutputDependencies" DependsOnTargets="ReferenceCopyLocalPathsOutputGroup;RunResolvePackageDependencies" Returns="@(ImplicitPackageReference)">
<Error Code="NG1003" Text="Centrally managed package versions is only supported when using the Microsoft.NET.Sdk."
Condition="'$(ManagePackageVersionsCentrally)' == 'true' and '$(UsingMicrosoftNETSdk)' != 'true'" />
<ItemGroup>
<_PrimaryOutputRelatedFile Include="@(ReferencePath);@(_ReferenceRelatedPaths)"
<_PrimaryOutputRelatedFile Include="@(ReferenceCopyLocalPathsOutputGroupOutput)"
Condition="'%(NuGetPackageId)' != 'NETStandard.Library' and
'%(Facade)' != 'true' and
'%(FrameworkFile)' != 'true' and
Expand Down
33 changes: 33 additions & 0 deletions src/NuGetizer.Tests/InlineProjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -714,5 +714,38 @@ public void when_multi_targeting_packing_transitive_dependency_then_retargets_to
TargetFramework = "netstandard2.0",
}));
}

[Fact]
public void when_packing_with_refs_then_includes_runtime_libs_for_private()
{
var result = Builder.BuildProject(
"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net472</TargetFramework>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>TestNuGetizer</PackageId>
<LangVersion>Latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Buffers" Version="4.5.1" PrivateAssets="all" />
<PackageReference Include="System.Memory" Version="4.5.5" PrivateAssets="all" />
</ItemGroup>
</Project>
""", output: output);

result.AssertSuccess(output);

Assert.Contains(result.Items, item => item.Matches(new
{
PathInPackage = "lib/net461/System.Buffers.dll",
}));
Assert.Contains(result.Items, item => item.Matches(new
{
PathInPackage = "lib/net461/System.Memory.dll",
}));
}
}
}
2 changes: 1 addition & 1 deletion src/NuGetizer.Tests/NuGetizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<DefaultItemExcludes>$(DefaultItemExcludes);Scenarios\**\*</DefaultItemExcludes>
<LangVersion>Preview</LangVersion>
<LangVersion>Latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 1a69585

Please sign in to comment.