Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep duplicates logging issue 9585 #10820

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Build.UnitTests/BackEnd/IntrinsicTask_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,27 @@ public void ItemKeepDuplicatesFalse()
Assert.Single(group);
}

[Fact]
public void ItemKeepDuplicatesFalseTwoDuplicatesAtOnce()
{
string content = ObjectModelHelpers.CleanupFileContents("""
<Project>
<Target Name='t'>
<ItemGroup>
<i1 Include='a1'/>
<i1 Include='a1;a1' KeepDuplicates='false' />
</ItemGroup>
</Target>
</Project>
""");
IntrinsicTask task = CreateIntrinsicTask(content);
Lookup lookup = LookupHelpers.CreateEmptyLookup();
ExecuteTask(task, lookup);

var group = lookup.GetItems("i1");
Assert.Single(group);
}

[Fact]
public void ItemKeepDuplicatesAsCondition()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
Expand Down Expand Up @@ -215,21 +216,27 @@ private void ExecuteAdd(ProjectItemGroupTaskItemInstance child, ItemBucket bucke
FileSystems.Default,
LoggingContext);

Action<IList> logFunction = null;

if (LogTaskInputs && !LoggingContext.LoggingService.OnlyLogCriticalEvents && itemsToAdd?.Count > 0)
{
ItemGroupLoggingHelper.LogTaskParameter(
LoggingContext,
TaskParameterMessageKind.AddItem,
parameterName: null,
propertyName: null,
child.ItemType,
itemsToAdd,
logItemMetadata: true,
child.Location);
logFunction = (itemList) =>
{
ItemGroupLoggingHelper.LogTaskParameter(
LoggingContext,
TaskParameterMessageKind.AddItem,
parameterName: null,
propertyName: null,
child.ItemType,
itemList,
logItemMetadata: true,
child.Location);
};
}

// Now add the items we created to the lookup.
bucket.Lookup.AddNewItemsOfItemType(child.ItemType, itemsToAdd, !keepDuplicates); // Add in one operation for potential copy-on-write
bucket.Lookup.AddNewItemsOfItemType(child.ItemType, itemsToAdd, !keepDuplicates, logFunction);
// Add in one operation for potential copy-on-write
}

/// <summary>
Expand Down
31 changes: 25 additions & 6 deletions src/Build/BackEnd/Components/RequestBuilder/Lookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -636,7 +637,7 @@ internal void SetProperty(ProjectPropertyInstance property)
/// <summary>
/// Implements a true add, an item that has been created in a batch.
/// </summary>
internal void AddNewItemsOfItemType(string itemType, ICollection<ProjectItemInstance> group, bool doNotAddDuplicates = false)
internal void AddNewItemsOfItemType(string itemType, ICollection<ProjectItemInstance> group, bool doNotAddDuplicates = false, Action<IList> logFunction = null)
{
// Adding to outer scope could be easily implemented, but our code does not do it at present
MustNotBeOuterScope();
Expand All @@ -658,14 +659,32 @@ internal void AddNewItemsOfItemType(string itemType, ICollection<ProjectItemInst
IEnumerable<ProjectItemInstance> itemsToAdd = group;
if (doNotAddDuplicates)
{
// Remove duplicates from the inputs.
itemsToAdd = itemsToAdd.Distinct(ProjectItemInstance.EqualityComparer);

// Ensure we don't also add any that already exist.
var existingItems = GetItems(itemType);
if (existingItems.Count > 0)
var existingItemsHashSet = existingItems.ToHashSet(ProjectItemInstance.EqualityComparer);
SimaTian marked this conversation as resolved.
Show resolved Hide resolved

var deduplicatedItemsToAdd = new List<ProjectItemInstance>();
foreach (var item in itemsToAdd)
{
if (existingItemsHashSet.Add(item))
{
deduplicatedItemsToAdd.Add(item);
}
}
itemsToAdd = deduplicatedItemsToAdd;
}

if (logFunction != null)
{
if (doNotAddDuplicates)
{
// itemsToAdd is guaranteed to be a List if we're doing the doNotAddDuplicates part.
logFunction.Invoke(itemsToAdd as List<ProjectItemInstance>);
}
else
{
itemsToAdd = itemsToAdd.Where(item => !existingItems.Contains(item, ProjectItemInstance.EqualityComparer));
var groupAsList = group as List<ProjectItemInstance>;
logFunction.Invoke(groupAsList ?? group.ToList());
}
}

SimaTian marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading