Skip to content
Merged
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
25 changes: 25 additions & 0 deletions src/Build.UnitTests/BackEnd/TaskBuilder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,31 @@ public void NullMetadataOnLegacyOutputItems()
logger.AssertLogContains("[foo: ]");
}

/// <summary>
/// If an item returned from a task has bare-minimum metadata implementation, we shouldn't crash.
/// </summary>
[Fact]
public void MinimalLegacyOutputItems()
{
string customTaskPath = Assembly.GetExecutingAssembly().Location;

string projectContents = $"""
<Project>
<UsingTask TaskName="TaskThatReturnsMinimalItem" AssemblyFile="{customTaskPath}" />

<Target Name="Build">
<TaskThatReturnsMinimalItem>
<Output TaskParameter="MinimalTaskItemOutput" ItemName="Outputs"/>
</TaskThatReturnsMinimalItem>

<Message Text="[%(Outputs.Identity): %(Outputs.a)]" Importance="High" />
</Target>
</Project>
""";

MockLogger logger = ObjectModelHelpers.BuildProjectExpectSuccess(projectContents, _testOutput, LoggerVerbosity.Diagnostic);
}

/// <summary>
/// Regression test for https://github.com/dotnet/msbuild/issues/5080
/// </summary>
Expand Down
48 changes: 48 additions & 0 deletions src/Build.UnitTests/TaskThatReturnsMinimalItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;

using Microsoft.Build.Framework;

namespace Microsoft.Build.Engine.UnitTests;

/// <summary>
/// Task that emulates .NET 3.5 tasks.
/// </summary>
public sealed class TaskThatReturnsMinimalItem : ITask
{
public IBuildEngine? BuildEngine { get; set; }
public ITaskHost? HostObject { get; set; }

[Output]
public ITaskItem MinimalTaskItemOutput { get => new MinimalTaskItem(); }

public bool Execute() => true;

/// <summary>
/// Minimal implementation of <see cref="ITaskItem"/> that uses a <see cref="Hashtable"/> for metadata,
/// like MSBuild 3 did.
/// </summary>
internal sealed class MinimalTaskItem : ITaskItem
{
public string ItemSpec { get => $"{nameof(MinimalTaskItem)}spec"; set => throw new NotImplementedException(); }

public ICollection MetadataNames => throw new NotImplementedException();

public int MetadataCount => throw new NotImplementedException();

public IDictionary CloneCustomMetadata()
{
Hashtable t = new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the important part, right? That it's a Hashtable and not a Dictionary<string, string>?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. I considered writing an even-more-malicious IDictionary implementation but it seemed like overkill.

t["key"] = "value";

return t;
}
public void CopyMetadataTo(ITaskItem destinationItem) => throw new NotImplementedException();
public string GetMetadata(string metadataName) => "value";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably shouldn't be my focus, since this is a test, but it bothers me a little that this will return "value" for undefined metadata. It's fine if you want to leave it, but I'd slightly prefer a check for "key" (and updating the test above accordingly)

public void RemoveMetadata(string metadataName) => throw new NotImplementedException();
public void SetMetadata(string metadataName, string metadataValue) => throw new NotImplementedException();
}
}
4 changes: 2 additions & 2 deletions src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,8 +1396,8 @@ private void GatherTaskItemOutputs(bool outputTargetIsItem, string outputTargetN
newItem = new ProjectItemInstance(_projectInstance, outputTargetName, EscapingUtilities.Escape(output.ItemSpec), parameterLocationEscaped);

newItem.SetMetadataOnTaskOutput(output.CloneCustomMetadata()
.Cast<KeyValuePair<string, string>>()
.Select(x => new KeyValuePair<string, string>(x.Key, EscapingUtilities.Escape(x.Value))));
.Cast<DictionaryEntry>()
.Select(x => new KeyValuePair<string, string>((string)x.Key, EscapingUtilities.Escape((string)x.Value))));
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Framework/ITaskItemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public static IEnumerable<KeyValuePair<string, string>> EnumerateMetadata(this I
return enumerableMetadata;
}

// In theory this should never be reachable.
// Fallback for
// * ITaskItem implementations from MSBuild 3.5 from the GAC
// * Custom ITaskItems that don't use Dictionary<string,string>
var list = new KeyValuePair<string, string>[customMetadata.Count];
int i = 0;

Expand Down