diff --git a/src/Build.UnitTests/Evaluation/Expander_Tests.cs b/src/Build.UnitTests/Evaluation/Expander_Tests.cs index 46762a8712e..8dc720b5536 100644 --- a/src/Build.UnitTests/Evaluation/Expander_Tests.cs +++ b/src/Build.UnitTests/Evaluation/Expander_Tests.cs @@ -5287,5 +5287,35 @@ public void PropertyFunctionRegisterBuildCheck() logger.AllBuildEvents.Count.ShouldBe(1); } } + + /// + /// Test for issue where chained item functions with empty results incorrectly evaluate as non-empty in conditions + /// + [Fact] + public void ChainedItemFunctionEmptyResultInCondition() + { + string content = @" + + + + + + + + + WithMetadataValue('Identity', 'Test1')->WithMetadataValue('Foo', 'Baz'))' == ''""> + TRUE + + + + + + "; + + MockLogger log = Helpers.BuildProjectWithNewOMExpectSuccess(content); + + // The chained WithMetadataValue should return empty, so the condition should be true and EmptyResult should be set + log.AssertLogContains("EmptyResult=TRUE"); + } } } diff --git a/src/Build/Evaluation/Expander.cs b/src/Build/Evaluation/Expander.cs index b40d997fb47..00c23338bd0 100644 --- a/src/Build/Evaluation/Expander.cs +++ b/src/Build/Evaluation/Expander.cs @@ -2021,21 +2021,25 @@ internal static List> Transform( break; } + // If we have another transform, swap the source and transform lists. + if (i < captures.Count - 1) + { + (transformedItems, sourceItems) = (sourceItems, transformedItems); + transformedItems.Clear(); + } + } + + // Check for break on non-empty only after ALL transforms are complete + if ((options & ExpanderOptions.BreakOnNotEmpty) != 0) + { foreach (KeyValuePair itemTuple in transformedItems) { - if (!string.IsNullOrEmpty(itemTuple.Key) && (options & ExpanderOptions.BreakOnNotEmpty) != 0) + if (!string.IsNullOrEmpty(itemTuple.Key)) { brokeEarly = true; return transformedItems; // break out early } } - - // If we have another transform, swap the source and transform lists. - if (i < captures.Count - 1) - { - (transformedItems, sourceItems) = (sourceItems, transformedItems); - transformedItems.Clear(); - } } brokeEarly = false;