Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
109 changes: 109 additions & 0 deletions src/Build.UnitTests/BackEnd/ItemDefinitionMetadataInTaskHost_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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.Diagnostics;
using System.IO;
using Microsoft.Build.Execution;
using Microsoft.Build.UnitTests;
using Shouldly;
using Xunit;

#nullable disable

namespace Microsoft.Build.Engine.UnitTests.BackEnd
{
/// <summary>
/// Metadata inherited from an item definition, such as <c>%(Filename)</c>, is stored unexpanded and expanded
/// when it is read. These tests assert that a task observes the same value whether it runs in-proc or in a
/// task host.
/// Regression tests for https://github.com/dotnet/msbuild/issues/14763.
/// </summary>
public sealed class ItemDefinitionMetadataInTaskHost_Tests
{
private static string AssemblyLocation { get; } =
typeof(ItemDefinitionMetadataInTaskHost_Tests).Assembly.Location
?? Path.Combine(AppContext.BaseDirectory, "Microsoft.Build.Engine.UnitTests.dll");

private readonly ITestOutputHelper _output;

public ItemDefinitionMetadataInTaskHost_Tests(ITestOutputHelper output) => _output = output;

[Theory]
[InlineData(false)]
[InlineData(true)]
public void MetadataReferencingBuiltInMetadataIsExpandedForTheTask(bool useTaskHost)
{
Observe("%(Filename)", useTaskHost).ShouldBe("hello");
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void MetadataReferencingBuiltInMetadataFollowsReassignedItemSpec(bool useTaskHost)
{
Observe("%(Filename)", useTaskHost, newItemSpec: @"other\renamed.txt").ShouldBe("renamed");
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void MetadataOverriddenOnTheItemWinsOverTheItemDefinition(bool useTaskHost)
{
Observe("%(Filename)", useTaskHost, itemOverride: "explicit").ShouldBe("explicit");
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void EscapedMetadataReferenceIsNotExpanded(bool useTaskHost)
{
Observe("%25(Filename)", useTaskHost).ShouldBe("%(Filename)");
}

/// <summary>
/// Runs a task against an item whose definition carries <paramref name="definitionValue"/> and returns the
/// value the task itself observed, having asserted the task ran where the test intended.
/// </summary>
private string Observe(string definitionValue, bool useTaskHost, string newItemSpec = null, string itemOverride = null)
{
using TestEnvironment env = TestEnvironment.Create(_output);

string project = $"""
<Project>
<UsingTask TaskName="MetadataObservationTask" AssemblyFile="{AssemblyLocation}"{(useTaskHost ? @" TaskFactory=""TaskHostFactory""" : string.Empty)} />
<ItemDefinitionGroup>
<Thing>
<NameMeta>{definitionValue}</NameMeta>
</Thing>
</ItemDefinitionGroup>
<ItemGroup>
<Thing Include="folder\hello.txt">
{(itemOverride is null ? string.Empty : $"<NameMeta>{itemOverride}</NameMeta>")}
</Thing>
</ItemGroup>
<Target Name="Observe">
<MetadataObservationTask Items="@(Thing)" MetadataName="NameMeta" NewItemSpec="{newItemSpec}">
<Output PropertyName="ObservedValue" TaskParameter="ObservedValue" />
<Output PropertyName="TaskProcessId" TaskParameter="TaskProcessId" />
</MetadataObservationTask>
</Target>
</Project>
""";

ProjectInstance projectInstance = new(env.CreateFile("test.proj", project).Path);

BuildResult result = BuildManager.DefaultBuildManager.Build(
new BuildParameters { EnableNodeReuse = false },
new BuildRequestData(projectInstance, targetsToBuild: ["Observe"]));

result.OverallResult.ShouldBe(BuildResultCode.Success);

int taskProcessId = int.Parse(projectInstance.GetPropertyValue("TaskProcessId"));
bool ranOutOfProc = taskProcessId != Process.GetCurrentProcess().Id;
ranOutOfProc.ShouldBe(useTaskHost, $"the task was expected to run {(useTaskHost ? "in a task host" : "in-proc")}");

return projectInstance.GetPropertyValue("ObservedValue");
}
}
}
57 changes: 57 additions & 0 deletions src/Build.UnitTests/BackEnd/MetadataObservationTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

#nullable disable

namespace Microsoft.Build.UnitTests
{
/// <summary>
/// Reports the value of a metadata name as the task itself observes it, plus the id of the process the
/// task ran in, so tests can tell an in-proc execution apart from a task host one.
/// Optionally reassigns ItemSpec first, to exercise metadata that derives from it.
/// </summary>
public class MetadataObservationTask : Task
{
[Required]
public ITaskItem[] Items { get; set; }

[Required]
public string MetadataName { get; set; }

public string NewItemSpec { get; set; }

[Output]
public string ObservedValue { get; set; }

[Output]
public int TaskProcessId { get; set; }

public override bool Execute()
{
TaskProcessId = Process.GetCurrentProcess().Id;

if (Items.Length > 0)
{
ITaskItem item = Items[0];

if (!string.IsNullOrEmpty(NewItemSpec))
{
item.ItemSpec = NewItemSpec;
}

ObservedValue = item.GetMetadata(MetadataName);
}
else
{
ObservedValue = string.Empty;
}

return true;
}
}
}

117 changes: 117 additions & 0 deletions src/Framework/BuiltInMetadataExpander.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// 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 Microsoft.NET.StringTools;

namespace Microsoft.Build.Framework;

/// <summary>
/// Expands built-in metadata references, such as <c>%(Filename)</c>, against an item.
/// </summary>
/// <remarks>
/// A deliberately minimal stand-in for the evaluation expander, which also handles properties, item vectors,
/// custom metadata, transforms and truncation. Built-in metadata references are the only expression form that
/// survives evaluation unexpanded, so they are the only one that can reach a task host still unexpanded. This
/// lives in Framework because the evaluation expander is internal to <c>Microsoft.Build</c>, while this is needed
/// by <c>MSBuild</c> and <c>Microsoft.Build.Tasks</c> too.
///
/// Keep in step with <c>Expander.ExpandIntoStringLeaveEscaped</c> under <c>ExpanderOptions.ExpandBuiltInMetadata</c>,
/// which is what <c>ProjectItemInstance.TaskItem.GetMetadataEscaped</c> uses for the same job in-proc.
Comment on lines +18 to +19

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am on board with this for the purpose of getting a fix out, but please file a follow-up to unify these.

/// </remarks>
internal static class BuiltInMetadataExpander
{
/// <summary>
/// Expands every built-in metadata reference in <paramref name="escapedValue"/> against the given item.
/// Anything else, including a reference that cannot be satisfied, is left as it is.
/// </summary>
/// <param name="escapedValue">The escaped value to expand.</param>
/// <param name="escapedItemSpec">The escaped item spec that built-in metadata is derived from.</param>
/// <param name="escapedDefiningProject">The escaped path of the project that defined the item.</param>
/// <param name="escapedRecursiveDir">
/// The item's RecursiveDir, which comes from the wildcard the item was expanded from rather than the item spec.
/// </param>
/// <param name="cache">Cache of already derived modifier values for this item.</param>
/// <returns>The value with built-in metadata references expanded.</returns>
internal static string? Expand(
string? escapedValue,
string escapedItemSpec,
string? escapedDefiningProject,
string? escapedRecursiveDir,
ref ItemSpecModifiers.Cache cache)
{
int index = escapedValue is null ? -1 : escapedValue.IndexOf("%(", StringComparison.Ordinal);

if (index < 0)
{
return escapedValue;
}

SpanBasedStringBuilder? builder = null;
int copiedUpTo = 0;

try
{
while (index >= 0)
{
int closingParenthesis = escapedValue!.IndexOf(')', index + 2);

if (closingParenthesis < 0)
{
break;
}

if (TryGetModifier(escapedValue, index + 2, closingParenthesis, out ItemSpecModifierKind kind))
{
builder ??= Strings.GetSpanBasedStringBuilder();
builder.Append(escapedValue, copiedUpTo, index - copiedUpTo);
builder.Append(kind is ItemSpecModifierKind.RecursiveDir
? escapedRecursiveDir ?? string.Empty
: ItemSpecModifiers.GetItemSpecModifier(escapedItemSpec, kind, currentDirectory: null, escapedDefiningProject, ref cache));
copiedUpTo = closingParenthesis + 1;
}

index = escapedValue.IndexOf("%(", closingParenthesis + 1, StringComparison.Ordinal);
}

if (builder is null)
{
return escapedValue;
}

builder.Append(escapedValue!, copiedUpTo, escapedValue!.Length - copiedUpTo);
return builder.ToString();
}
finally
{
builder?.Dispose();
}
}

/// <summary>
/// Reads the metadata name between <c>%(</c> and its closing parenthesis and resolves it to a built-in
/// metadata kind, allowing surrounding whitespace as the evaluation expander does. A name qualified by an item
/// type is rejected, since the engine resolves built-in metadata against a table with no item type and so
/// never satisfies one either.
/// </summary>
private static bool TryGetModifier(string value, int start, int end, out ItemSpecModifierKind kind)
{
while (start < end && char.IsWhiteSpace(value[start]))
{
start++;
}

while (end > start && char.IsWhiteSpace(value[end - 1]))
{
end--;
}

if (end <= start)
{
kind = default;
return false;
}

return ItemSpecModifiers.TryGetModifierKind(value.Substring(start, end - start), out kind);
}
}
Loading
Loading