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

Commit fd11964

Browse files
kzuadalon
authored andcommitted
Include None/Content that has CopyToOutputDirectory!=None by default
Improve the inference so that by default we include both item types if they have been assigned a target path by the built-in targets, meaning that they have a `%(CopyToOutputDirectory) != None`. Also, introduce a new `$(IncludeNoneInPackage)` value that acts as a default (true) also for `@(None) items, making it consistent with `@(Content)` Fixes NuGet/Home#5105
1 parent bc03917 commit fd11964

2 files changed

Lines changed: 60 additions & 7 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ Copyright (c) .NET Foundation. All rights reserved.
3030

3131
<!-- Infer PackageRererence elements from packages.config/project.json files in the project -->
3232
<InferLegacyPackageReferences Condition="'$(InferLegacyPackageReferences)' == ''">true</InferLegacyPackageReferences>
33-
<!-- Whether to include @(Content) items in the package -->
33+
<!-- Whether to include @(Content) items with CopyToOutputDirectory != 'None' in the package -->
3434
<IncludeContentInPackage Condition="'$(IncludeContentInPackage)' == ''">true</IncludeContentInPackage>
35+
<!-- Whether to include @(None) items with CopyToOutputDirectory != 'None' in the package -->
36+
<IncludeNoneInPackage Condition="'$(IncludeNoneInPackage)' == ''">true</IncludeNoneInPackage>
3537
<!-- Whether to include @(BuiltProjectOutputGroupOutput), @(DocumentationProjectOutputGroupOutput) and @(SatelliteDllsProjectOutputGroupOutput) items in the package -->
3638
<IncludeOutputsInPackage Condition="'$(IncludeOutputsInPackage)' == ''">true</IncludeOutputsInPackage>
3739
<!-- Whether to include @(DebugSymbolsProjectOutputGroupOutput) items in the package -->
@@ -286,16 +288,16 @@ Copyright (c) .NET Foundation. All rights reserved.
286288
<FrameworkSpecific>$(PrimaryOutputFrameworkSpecific)</FrameworkSpecific>
287289
</PackageFile>
288290

289-
<!-- NOTE: Content is opt-out (must have IncludeInPackage=false to exclude) -->
291+
<!-- NOTE: Content is opt-out (must have IncludeInPackage=false to exclude if IncludeContentInPackage=true) -->
290292
<!-- @ContentFilesProjectOutputGroupOutput = @(ContentWithTargetPath -> '%(FullPath)') -->
291293
<PackageFile Include="@(ContentWithTargetPath->'%(FullPath)')"
292-
Condition="'$(IncludeContentInPackage)' == 'true' and '%(ContentWithTargetPath.IncludeInPackage)' != 'false'">
294+
Condition="'%(ContentWithTargetPath.IncludeInPackage)' == 'true' or ('$(IncludeContentInPackage)' == 'true' and '%(ContentWithTargetPath.IncludeInPackage)' != 'false')">
293295
<Kind>Content</Kind>
294296
</PackageFile>
295297

296-
<!-- NOTE: None is opt-in (must have IncludeInPackage=true to include) -->
297-
<PackageFile Include="@(_NoneWithTargetPath)"
298-
Condition="'%(_NoneWithTargetPath.IncludeInPackage)' == 'true'">
298+
<!-- NOTE: None is also opt-out (must have IncludeInPackage=false to exclude if IncludeNoneInPackage=true) -->
299+
<PackageFile Include="@(_NoneWithTargetPath->'%(FullPath)')"
300+
Condition="'%(_NoneWithTargetPath.IncludeInPackage)' == 'true' or ('$(IncludeNoneInPackage)' == 'true' and '%(_NoneWithTargetPath.IncludeInPackage)' != 'false')">
299301
<Kind>None</Kind>
300302
</PackageFile>
301303

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ public void when_library_is_packable_then_contains_content_files_in_anylang_tfm_
6060
}
6161

6262
[Fact]
63-
public void when_none_item_has_no_include_in_package_then_it_is_not_included()
63+
public void when_include_none_in_package_is_false_then_inferred_none_is_not_included()
6464
{
6565
var result = Builder.BuildScenario(nameof(given_a_library_with_content), new
6666
{
6767
PackageId = "ContentPackage",
68+
IncludeNoneInPackage = "false",
6869
});
6970

7071
result.AssertSuccess(output);
@@ -75,6 +76,39 @@ public void when_none_item_has_no_include_in_package_then_it_is_not_included()
7576
}));
7677
}
7778

79+
[Fact]
80+
public void when_include_none_in_package_is_false_then_explicitly_included_none_is_included()
81+
{
82+
var result = Builder.BuildScenario(nameof(given_a_library_with_content), new
83+
{
84+
PackageId = "ContentPackage",
85+
IncludeNoneInPackage = "false",
86+
});
87+
88+
result.AssertSuccess(output);
89+
90+
Assert.Contains(result.Items, item => item.Matches(new
91+
{
92+
TargetPath = "none-with-include-true.txt",
93+
}));
94+
}
95+
96+
[Fact]
97+
public void when_none_item_has_no_include_in_package_then_it_is_included_by_default()
98+
{
99+
var result = Builder.BuildScenario(nameof(given_a_library_with_content), new
100+
{
101+
PackageId = "ContentPackage",
102+
});
103+
104+
result.AssertSuccess(output);
105+
106+
Assert.Contains(result.Items, item => item.Matches(new
107+
{
108+
TargetPath = "none.txt",
109+
}));
110+
}
111+
78112
[Fact]
79113
public void when_none_item_has_include_in_package_true_then_it_is_included()
80114
{
@@ -139,6 +173,23 @@ public void when_content_item_has_include_in_package_true_then_it_is_included()
139173
}));
140174
}
141175

176+
[Fact]
177+
public void when_include_content_in_package_is_false_then_explicitly_included_content_is_included()
178+
{
179+
var result = Builder.BuildScenario(nameof(given_a_library_with_content), new
180+
{
181+
PackageId = "ContentPackage",
182+
IncludeContentInPackage = "false",
183+
});
184+
185+
result.AssertSuccess(output);
186+
187+
Assert.Contains(result.Items, item => item.Matches(new
188+
{
189+
TargetPath = "content-with-include-true.txt",
190+
}));
191+
}
192+
142193
[Fact]
143194
public void when_removing_inferred_package_file_from_content_then_content_is_not_included()
144195
{

0 commit comments

Comments
 (0)