Skip to content

Commit b616fc4

Browse files
authored
Merge pull request dotnet#675 from dsplaisted/design-time-duplicate-item-error-664
Show duplicate item errors due to implicit items in design-time builds
2 parents 366ba01 + 8897865 commit b616fc4

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/CheckForDuplicateItems.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,18 @@ public class CheckForDuplicateItems : TaskBase
2727
[Required]
2828
public string MoreInformationLink { get; set; }
2929

30+
[Output]
31+
public ITaskItem [] DeduplicatedItems { get; set; }
32+
3033
protected override void ExecuteCore()
3134
{
35+
DeduplicatedItems = Array.Empty<ITaskItem>();
36+
3237
if (DefaultItemsEnabled && DefaultItemsOfThisTypeEnabled)
3338
{
34-
var duplicateItems = Items.GroupBy(i => i.ItemSpec).Where(g => g.Count() > 1).ToList();
39+
var itemGroups = Items.GroupBy(i => i.ItemSpec);
40+
41+
var duplicateItems = itemGroups.Where(g => g.Count() > 1).ToList();
3542
if (duplicateItems.Any())
3643
{
3744
string duplicateItemsFormatted = string.Join("; ", duplicateItems.Select(d => $"'{d.Key}'"));
@@ -43,6 +50,8 @@ protected override void ExecuteCore()
4350
duplicateItemsFormatted);
4451

4552
Log.LogError(message);
53+
54+
DeduplicatedItems = itemGroups.Select(g => g.First()).ToArray();
4655
}
4756
}
4857
}

src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Sdk.DefaultItems.targets

+33-5
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,17 @@ Copyright (c) .NET Foundation. All rights reserved.
114114

115115
<UsingTask TaskName="CheckForDuplicateItems" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
116116

117-
<Target Name="CheckForDuplicateItems" BeforeTargets="_CheckForInvalidConfigurationAndPlatform">
117+
<Target Name="CheckForDuplicateItems" BeforeTargets="_CheckForInvalidConfigurationAndPlatform;CoreCompile">
118118

119119
<PropertyGroup>
120120
<DefaultItemsMoreInformationLink>https://aka.ms/sdkimplicititems</DefaultItemsMoreInformationLink>
121+
122+
<!-- For the design-time build, we will continue on error and remove the duplicate items.
123+
This is because otherwise there won't be any references to pass to the compiler, leading to design-time
124+
compilation errors for every API that is used in the project. Amidst all the compile errors, it would
125+
be easy to miss the duplicate items error which is the real source of the problem. -->
126+
<CheckForDuplicateItemsContinueOnError>false</CheckForDuplicateItemsContinueOnError>
127+
<CheckForDuplicateItemsContinueOnError Condition="'$(DesignTimeBuild)' == 'true'">ErrorAndContinue</CheckForDuplicateItemsContinueOnError>
121128
</PropertyGroup>
122129

123130
<CheckForDuplicateItems
@@ -127,7 +134,9 @@ Copyright (c) .NET Foundation. All rights reserved.
127134
DefaultItemsOfThisTypeEnabled="$(EnableDefaultCompileItems)"
128135
PropertyNameToDisableDefaultItems="EnableDefaultCompileItems"
129136
MoreInformationLink="$(DefaultItemsMoreInformationLink)"
130-
/>
137+
ContinueOnError="$(CheckForDuplicateItemsContinueOnError)">
138+
<Output TaskParameter="DeduplicatedItems" ItemName="DeduplicatedCompileItems" />
139+
</CheckForDuplicateItems>
131140

132141
<CheckForDuplicateItems
133142
Items="@(EmbeddedResource)"
@@ -136,17 +145,36 @@ Copyright (c) .NET Foundation. All rights reserved.
136145
DefaultItemsOfThisTypeEnabled="$(EnableDefaultEmbeddedResourceItems)"
137146
PropertyNameToDisableDefaultItems="EnableDefaultEmbeddedResourceItems"
138147
MoreInformationLink="$(DefaultItemsMoreInformationLink)"
139-
/>
148+
ContinueOnError="$(CheckForDuplicateItemsContinueOnError)">
149+
<Output TaskParameter="DeduplicatedItems" ItemName="DeduplicatedEmbeddedResourceItems" />
150+
</CheckForDuplicateItems>
140151

141-
<!-- TODO: Do we check for duplicate content items here, or in the Web SDK? EnableDefaultContentItems isn't defined or used at all by the .NET SDK -->
152+
<!-- Default content items are enabled by the Web SDK, not the .NET SDK, but we check it here for simplicity -->
142153
<CheckForDuplicateItems
143154
Items="@(Content)"
144155
ItemName="Content"
145156
DefaultItemsEnabled="$(EnableDefaultItems)"
146157
DefaultItemsOfThisTypeEnabled="$(EnableDefaultContentItems)"
147158
PropertyNameToDisableDefaultItems="EnableDefaultContentItems"
148159
MoreInformationLink="$(DefaultItemsMoreInformationLink)"
149-
/>
160+
ContinueOnError="$(CheckForDuplicateItemsContinueOnError)">
161+
<Output TaskParameter="DeduplicatedItems" ItemName="DeduplicatedContentItems" />
162+
</CheckForDuplicateItems>
163+
164+
<ItemGroup Condition="'$(DesignTimeBuild)' == 'true' And '@(DeduplicatedCompileItems)' != ''">
165+
<Compile Remove="@(Compile)" />
166+
<Compile Include="@(DeduplicatedCompileItems)" />
167+
</ItemGroup>
168+
169+
<ItemGroup Condition="'$(DesignTimeBuild)' == 'true' And '@(DeduplicatedEmbeddedResourceItems)' != ''">
170+
<EmbeddedResource Remove="@(EmbeddedResource)" />
171+
<EmbeddedResource Include="@(DeduplicatedEmbeddedResourceItems)" />
172+
</ItemGroup>
173+
174+
<ItemGroup Condition="'$(DesignTimeBuild)' == 'true' And '@(DeduplicatedContentItems)' != ''">
175+
<Content Remove="@(Content)" />
176+
<Content Include="@(DeduplicatedContentItems)" />
177+
</ItemGroup>
150178

151179
</Target>
152180
</Project>

0 commit comments

Comments
 (0)