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

Commit a9cb8d6

Browse files
committed
Fail on Pack if duplicate PackagePaths are found
Fixes #4554 Rendering all the source files that end up causing the duplicate was more complicated to do in a useful manner that preserved the IDE as well as text logging readable, so I opted to just mention the PackagePath instead. Users can easily bump up the logging to Detailed or Diagnostic (or even better, use the MSBuild Structured Log Viewer ;)) to get a precise list of inputs to the task and see clearly which ones are the dupes. We should still provide a hook for them to de-duplicate easily if so desired. See #5167
1 parent b82e84d commit a9cb8d6

14 files changed

Lines changed: 125 additions & 24 deletions

File tree

src/Build/NuGet.Build.Packaging.Tasks/CreatePackage.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using NuGet.Packaging.Core;
1010
using NuGet.Packaging;
1111
using System.Collections.Generic;
12+
using static NuGet.Build.Packaging.Properties.Strings;
1213

1314
namespace NuGet.Build.Packaging.Tasks
1415
{
@@ -38,7 +39,7 @@ public override bool Execute()
3839
OutputPackage = new TaskItem(TargetPath);
3940
Manifest.CopyMetadataTo(OutputPackage);
4041

41-
return true;
42+
return !Log.HasLoggedErrors;
4243
}
4344
catch (Exception ex)
4445
{
@@ -134,6 +135,15 @@ void AddFiles(Manifest manifest)
134135
var contents = Contents.Where(item =>
135136
!string.IsNullOrEmpty(item.GetMetadata(MetadataName.PackagePath)));
136137

138+
var duplicates = contents.GroupBy(item => item.GetMetadata(MetadataName.PackagePath))
139+
.Where(x => x.Count() > 1)
140+
.Select(x => x.Key);
141+
142+
foreach (var duplicate in duplicates)
143+
{
144+
Log.LogErrorCode(nameof(ErrorCode.NG0012), ErrorCode.NG0012(duplicate));
145+
}
146+
137147
// All files need to be added so they are included in the nupkg
138148
manifest.Files.AddRange(contents
139149
.Select(item => new ManifestFile

src/Build/NuGet.Build.Packaging.Tasks/NuGet.Build.Packaging.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Copyright (c) .NET Foundation. All rights reserved.
2424
<PackageFile>
2525
<!-- See @(PackageItemKind) below -->
2626
<Kind>None</Kind>
27+
<!-- By default, we consider all PackageFile items to be explicitly added by the user
28+
the automatic discovery will annotate those with Implicit instead.
29+
This allows the duplicate item detection to either warn (Implicit) or error (Explicit). -->
30+
<Source>Explicit</Source>
2731
</PackageFile>
2832
<PackageReference>
2933
<!-- See https://github.com/NuGet/Home/wiki/PackageReference-Specification -->

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ Copyright (c) .NET Foundation. All rights reserved.
254254
</PackageFile>
255255

256256
<!-- NOTE: Content is opt-out (must have IncludeInPackage=false to exclude) -->
257-
<!-- @ContentFilesProjectOutputGroupOutput = @(ContentWithTargetPath->'%(FullPath)') -->
258-
<PackageFile Include="@(ContentFilesProjectOutputGroupOutput)"
259-
Condition="'$(IncludeContentInPackage)' == 'true' and '%(ContentFilesProjectOutputGroupOutput.IncludeInPackage)' != 'false'">
257+
<!-- @ContentFilesProjectOutputGroupOutput = @(ContentWithTargetPath -> '%(FullPath)') -->
258+
<PackageFile Include="@(ContentWithTargetPath->'%(FullPath)')"
259+
Condition="'$(IncludeContentInPackage)' == 'true' and '%(ContentWithTargetPath.IncludeInPackage)' != 'false'">
260260
<Kind>Content</Kind>
261261
</PackageFile>
262262

src/Build/NuGet.Build.Packaging.Tasks/Properties/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Build/NuGet.Build.Packaging.Tasks/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,7 @@
123123
<data name="ErrorCode_NG0011" xml:space="preserve">
124124
<value>Project references to include in package must be nugetized.</value>
125125
</data>
126+
<data name="ErrorCode_NG0012" xml:space="preserve">
127+
<value>Duplicate package source files detected for target package file path '{0}'.</value>
128+
</data>
126129
</root>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.IO;
45
using System.Xml.Linq;
56
using Microsoft.Build.Evaluation;
@@ -36,9 +37,9 @@ public static BuildResult Build(string projectOrSolution, string targets, Dictio
3637
var parameters = new BuildParameters
3738
{
3839
GlobalProperties = properties,
39-
DisableInProcNode = true,
40+
DisableInProcNode = !Debugger.IsAttached,
4041
EnableNodeReuse = false,
41-
ShutdownInProcNodeOnBuildFinish = true,
42+
ShutdownInProcNodeOnBuildFinish = !Debugger.IsAttached,
4243
// Without this, builds end up running in process and colliding with each other,
4344
// especially around the current directory used to resolve relative paths in projects.
4445
LogInitialPropertiesAndItems = true,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<SubType>Designer</SubType>
2222
</None>
2323
<Content Include="Scenarios\given_a_custom_build_project\Readme.txt" />
24+
<Content Include="Scenarios\given_duplicate_package_files\a.csproj" />
25+
<Content Include="Scenarios\given_duplicate_package_files\b.csproj" />
26+
<Content Include="Scenarios\given_duplicate_package_files\content\content.txt" />
27+
<Content Include="Scenarios\given_duplicate_package_files\content\inferred.txt" />
2428
<Content Include="Scenarios\given_a_library_with_content\content-with-include-true.txt" />
2529
<Content Include="Scenarios\given_a_library_with_content\content-with-include-false.txt" />
2630
<Content Include="Scenarios\given_a_library_with_content\none-with-include-false.txt" />
@@ -62,6 +66,7 @@
6266
<Content Include="Scenarios\given_a_packaging_project\e.csproj" />
6367
<Content Include="Scenarios\given_a_packaging_project_with_reference_assembly\b.sln" />
6468
<Content Include="Scenarios\given_a_packaging_project_with_reference_assembly\b\b.csproj" />
69+
<Content Include="Scenarios\given_duplicate_package_files\content\web\js\nuget.js" />
6570
<Content Include="Scenarios\given_library_with_config_dependencies\a.csproj" />
6671
<Content Include="Scenarios\given_library_with_config_dependencies\a.sln" />
6772
<Content Include="Scenarios\given_library_with_json_dependencies\project.json" />
@@ -90,6 +95,7 @@
9095
</Compile>
9196
<Compile Include="GetApiIntersectTargetPathsTests.cs" />
9297
<Compile Include="given_a_custom_build_project.cs" />
98+
<Compile Include="given_duplicate_package_files.cs" />
9399
<Compile Include="given_a_packaging_project.cs" />
94100
<Compile Include="given_a_multi_platform_solution.cs" />
95101
<Compile Include="given_a_library_with_content.cs" />
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<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-
<ProjectReference Include="b.csproj">
9-
<Project>$(GuidB)</Project>
10-
<Name>b</Name>
11-
<IncludeInPackage>$(IncludeInPackage)</IncludeInPackage>
12-
</ProjectReference>
13-
</ItemGroup>
14-
<ItemGroup>
15-
<PackageReference Include="ANuGetPackageReference">
16-
<Version>1.0.0-pre</Version>
17-
</PackageReference>
18-
</ItemGroup>
19-
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Scenario.targets))\Scenario.targets" />
3+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Scenario.props))\Scenario.props" />
4+
<PropertyGroup>
5+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<ProjectReference Include="b.csproj">
9+
<Project>$(GuidB)</Project>
10+
<Name>b</Name>
11+
<IncludeInPackage>$(IncludeInPackage)</IncludeInPackage>
12+
</ProjectReference>
13+
</ItemGroup>
14+
<ItemGroup>
15+
<PackageReference Include="ANuGetPackageReference">
16+
<Version>1.0.0-pre</Version>
17+
</PackageReference>
18+
</ItemGroup>
19+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Scenario.targets))\Scenario.targets" />
2020
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
<PackageId>A</PackageId>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<ProjectReference Include="b.csproj">
10+
<Project>$(GuidB)</Project>
11+
<Name>b</Name>
12+
</ProjectReference>
13+
</ItemGroup>
14+
<ItemGroup>
15+
<Content Include="content\web\**\*.*">
16+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17+
</Content>
18+
<PackageFile Include="content\content.txt">
19+
<PackagePath>content\content.txt</PackagePath>
20+
</PackageFile>
21+
</ItemGroup>
22+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Scenario.targets))\Scenario.targets" />
23+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
<ProjectGuid>$(GuidB)</ProjectGuid>
6+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<Content Include="content\web\**\*.*">
10+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
11+
</Content>
12+
<PackageFile Include="content\content.txt">
13+
<PackagePath>content\content.txt</PackagePath>
14+
</PackageFile>
15+
</ItemGroup>
16+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Scenario.targets))\Scenario.targets" />
17+
</Project>

0 commit comments

Comments
 (0)