Skip to content

Commit

Permalink
Any have metadata value when empty Fixes #5113 (#8603)
Browse files Browse the repository at this point in the history
Previously, when we saw the Item was empty, we'd jump out early. Fortunately, we already special-cased Count, and it turns out we can do the same with AnyHaveMetadataValue. Fixes #5113.
  • Loading branch information
Forgind committed Mar 28, 2023
1 parent ea90db2 commit da690cf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions documentation/wiki/ChangeWaves.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ A wave of features is set to "rotate out" (i.e. become standard functionality) t
- [Eliminate project string cache](https://github.com/dotnet/msbuild/pull/7965)
- [Log an error when no provided search path for an import exists](https://github.com/dotnet/msbuild/pull/8095)
- [Log assembly loads](https://github.com/dotnet/msbuild/pull/8316)
- [AnyHaveMetadataValue returns false when passed an empty list](https://github.com/dotnet/msbuild/pull/8603)

### 17.4
- [Respect deps.json when loading assemblies](https://github.com/dotnet/msbuild/pull/7520)
Expand Down
12 changes: 12 additions & 0 deletions src/Build.UnitTests/Evaluation/Expander_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ public void ExpandItemVectorFunctionsAnyHaveMetadataValue()
Assert.Equal("false", itemsFalse[0].EvaluatedInclude);
}

[Fact]
public void ExpandEmptyItemVectorFunctionWithAnyHaveMetadataValue()
{
ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
Expander<ProjectPropertyInstance, ProjectItemInstance> expander = CreateItemFunctionExpander();
ProjectItemInstanceFactory itemFactory = new ProjectItemInstanceFactory(project, "i");

IList<ProjectItemInstance> itemsEmpty = expander.ExpandIntoItemsLeaveEscaped("@(unsetItem->AnyHaveMetadataValue('Metadatum', 'value'))", itemFactory, ExpanderOptions.ExpandItems, MockElementLocation.Instance);
ProjectItemInstance pii = itemsEmpty.ShouldHaveSingleItem<ProjectItemInstance>();
pii.EvaluatedInclude.ShouldBe("false");
}

/// <summary>
/// Expand an item vector function Metadata()->DirectoryName()->Distinct()
/// </summary>
Expand Down
11 changes: 8 additions & 3 deletions src/Build/Evaluation/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1946,11 +1946,16 @@ private static class ItemExpander
// If there are no items of the given type, then bail out early
if (itemsOfType.Count == 0)
{
// .. but only if there isn't a function "Count()", since that will want to return something (zero) for an empty list
// ... but only if there isn't a function "Count", since that will want to return something (zero) for an empty list
if (expressionCapture.Captures?.Any(capture => string.Equals(capture.FunctionName, "Count", StringComparison.OrdinalIgnoreCase)) != true)
{
itemsFromCapture = new List<Pair<string, S>>();
return false;
// ...or a function "AnyHaveMetadataValue", since that will want to return false for an empty list.
if (!ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_6) ||
expressionCapture.Captures?.Any(capture => string.Equals(capture.FunctionName, "AnyHaveMetadataValue", StringComparison.OrdinalIgnoreCase)) != true)
{
itemsFromCapture = new List<Pair<string, S>>();
return false;
}
}
}

Expand Down

0 comments on commit da690cf

Please sign in to comment.