Skip to content

Commit

Permalink
Allow opting out of transitive private assets package reference packing
Browse files Browse the repository at this point in the history
Sometimes it may be useful to just pack the top-level dependency from a reference (i.e. you're getting the other deps by some other means, or the environment they run on doesn't require them, such as for Microsoft.Build.Framework, say).

This allows just setting PackTransitive=false to stop the transitivity.

Fixes #37
  • Loading branch information
kzu committed Dec 4, 2020
1 parent 32f8bee commit ab87442
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ Package references are turned into package dependencies by default (essentially

Build-only dependencies that don't contribute assemblies to the output (i.e. analyzers or things like [GitInfo](https://github.com/kzu/GitInfo) or [ThisAssembly](https://github.com/kzu/ThisAssembly) won't cause any extra items).

This even works transitively, so if you use *PrivateAssets=all* on package reference *A*, which in turn has a package dependency on *B* and *B* in turn depends on *C*, all of *A*, *B* and *C* assets will be packed.
This even works transitively, so if you use *PrivateAssets=all* on package reference *A*, which in turn has a package dependency on *B* and *B* in turn depends on *C*, all of *A*, *B* and *C* assets will be packed. You can opt out of the transitive packing with `PackTransitive=false`.

As usual, you can change this default behavior by using `Pack=false` metadata.
As usual, you can change this default behavior by using `Pack=false` metadata.

### ProjectReference

Expand Down
8 changes: 4 additions & 4 deletions src/NuGetizer.Tasks/NuGetizer.Inference.targets
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ Copyright (c) .NET Foundation. All rights reserved.
'%(Facade)' != 'true' and
'%(FrameworkFile)' != 'true' and
'%(Pack)' != 'false'"/>
<_PrivateAssetsPackageReference Include="@(PackageReference -> WithMetadataValue('PrivateAssets', 'all'))"
Condition="'%(PackageReference.IsImplicitlyDefined)' != 'true' and '%(PackageReference.Pack)' != 'false'"/>
<_TransitivePackageReference Include="@(PackageReference -> WithMetadataValue('PrivateAssets', 'all'))"
Condition="'%(PackageReference.IsImplicitlyDefined)' != 'true' and '%(PackageReference.Pack)' != 'false' and '%(PackageReference.PackTransitive)' != 'false'"/>
</ItemGroup>
<InferImplicitPackageReference Condition="'@(_PrivateAssetsPackageReference)' != '' and '@(PackageDependencies)' != ''"
PackageReferences="@(_PrivateAssetsPackageReference)"
<InferImplicitPackageReference Condition="'@(_TransitivePackageReference)' != '' and '@(PackageDependencies)' != ''"
PackageReferences="@(_TransitivePackageReference)"
PackageDependencies="@(PackageDependencies)">
<Output TaskParameter="ImplicitPackageReferences" ItemName="ImplicitPackageReference" />
</InferImplicitPackageReference>
Expand Down
32 changes: 31 additions & 1 deletion src/NuGetizer.Tests/InlineProjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public void when_pack_on_build_multitargeting_then_contains_all_targets()
}

[Fact]
public void when_packing_transive_reference_then_packs_lib()
public void when_packing_private_transive_reference_then_packs_lib()
{
var result = Builder.BuildProject(@"
<Project Sdk='Microsoft.NET.Sdk'>
Expand All @@ -495,5 +495,35 @@ public void when_packing_transive_reference_then_packs_lib()
Assert.Contains(result.Items, item
=> item.GetMetadata("FullPath").EndsWith(Path.Combine("lib", "netstandard2.0", "System.Threading.Tasks.Extensions.dll")));
}

[Fact]
public void when_packing_private_dependency_then_can_opt_out_of_transitive()
{
var result = Builder.BuildProject(@"
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<IsPackable>true</IsPackable>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include='Scriban' Version='3.0.4' PrivateAssets='all' PackTransitive='false' />
</ItemGroup>
</Project>", output: output);

result.AssertSuccess(output);

Assert.DoesNotContain(result.Items, item => item.Matches(new
{
Filename = "System.Runtime.CompilerServices.Unsafe",
Extension = ".dll"
}));

Assert.DoesNotContain(result.Items, item => item.Matches(new
{
Filename = "System.Threading.Tasks.Extensions",
Extension = ".dll"
}));
}

}
}

0 comments on commit ab87442

Please sign in to comment.