-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand built-in metadata references in the task host #14770
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
Open
JanProvaznik
wants to merge
5
commits into
dotnet:main
Choose a base branch
from
JanProvaznik:proto/simple-expand-on-read
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
41c4e7d
Expand built-in metadata references in the task host
JanProvaznik f3061e6
Substitute when cloning, and invalidate cached path metadata
JanProvaznik 9e2ca79
Name the concepts this relies on, and guard them with tests
JanProvaznik 0748ddb
Unit test the expander, and correct its scan for a nested reference
JanProvaznik 8b58b31
Address review feedback
JanProvaznik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/Build.UnitTests/BackEnd/ItemDefinitionMetadataInTaskHost_Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| /// </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); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.