Skip to content

Fix item function chaining with whitespace before arrow operators - #12772

Merged
YuliiaKovalova merged 3 commits into
mainfrom
copilot/fix-item-function-chaining-whitespace
Nov 20, 2025
Merged

YuliiaKovalova merged 3 commits into
mainfrom
copilot/fix-item-function-chaining-whitespace

Conversation

Copilot AI commented Nov 16, 2025 •

Copy link
Copy Markdown
Contributor

Fixes #12713

Context

Item function chaining fails when whitespace precedes the second or subsequent -> operator:

<!-- Fails - unexpanded literal returned -->
<Test Include="@(I -> WithMetadataValue('M', 'T') -> WithMetadataValue('M', 'T'))" />

<!-- Works -->
<Test Include="@(I -> WithMetadataValue('M', 'T')-> WithMetadataValue('M', 'T'))" />

The parser in ExpressionShredder.cs checks for -> at loop start but doesn't consume trailing whitespace after processing each transform/function. When currentIndex sits on whitespace after ), the Sink call looking for - fails.

Changes Made

Added SinkWhitespace calls before loop continuation in two locations:

  • MoveNext() method: After quoted transforms (line 187) and function expressions (line 202)
  • GetReferencedItemNamesAndMetadata() method: After quoted transforms (line 341) and function expressions (line 348)

This consumes trailing whitespace before checking for the next ->.

Testing

  • Added ItemFunctionChainingWithWhitespaceBeforeArrow test in Expander_Tests.cs reproducing the exact issue
  • Added ExtractItemVectorExpressionsChainedFunctionsWithWhitespace test in ExpressionShredder_Tests.cs validating parser behavior with various whitespace patterns
  • Added ExtractItemVectorExpressionsInvalidSyntaxAfterWhitespace test to ensure invalid syntax after whitespace is properly rejected
  • Added edge case tests for trailing whitespace after function calls (@(I -> Count() )) and quoted transforms (@(I -> 'Replacement' ))
  • All 51 ExpressionShredder tests pass
  • 1093 of 1098 Evaluation tests pass (5 pre-existing platform-specific failures)

Notes

Four-line production code fix with comprehensive test coverage including edge cases. No breaking changes—existing whitespace-free syntax continues working.

Original prompt

This section details on the original issue you should resolve

<issue_title>Item function chaining whitespace requirement</issue_title>
<issue_description>### Issue Description

It seems item function chaining require absence of whitespace before second ->.

Steps to Reproduce

Test.proj:

<Project>
	<ItemGroup>
		<I Include="A">
			<M>F</M>
		</I>
		<I Include="B">
			<M>T</M>
		</I>
		<I Include="C">
			<M>T</M>
		</I>
		<Test1 Include="@(I -> WithMetadataValue('M', 'T') -> WithMetadataValue('M', 'T'))" />
		<Test2 Include="@(I -> WithMetadataValue('M', 'T')-> WithMetadataValue('M', 'T'))" />
	</ItemGroup>
</Project>

MSBuild command line:

dotnet msbuild -getItem:Test1,Test2

Expected Behavior

Both item types (Test1 and Test2) should have the same content.

Actual Behavior

{
  "Items": {
    "Test1": [
      {
        "Identity": "@(I -\u003E WithMetadataValue(\u0027M\u0027, \u0027T\u0027) -\u003E WithMetadataValue(\u0027M\u0027, \u0027T\u0027))",
        "FullPath": "M:\\Temp\\Test\\@(I -\u003E WithMetadataValue(\u0027M\u0027, \u0027T\u0027) -\u003E WithMetadataValue(\u0027M\u0027, \u0027T\u0027))",
        "RootDir": "M:\\",
        "Filename": "@(I -\u003E WithMetadataValue(\u0027M\u0027, \u0027T\u0027) -\u003E WithMetadataValue(\u0027M\u0027, \u0027T\u0027))",
        "Extension": "",
        "RelativeDir": "",
        "Directory": "Temp\\Test\\",
        "RecursiveDir": "",
        "ModifiedTime": "",
        "CreatedTime": "",
        "AccessedTime": "",
        "DefiningProjectFullPath": "M:\\Temp\\Test\\Test.proj",
        "DefiningProjectDirectory": "M:\\Temp\\Test\\",
        "DefiningProjectName": "Test",
        "DefiningProjectExtension": ".proj"
      }
    ],
    "Test2": [
      {
        "Identity": "B",
        "M": "T",
        "FullPath": "M:\\Temp\\Test\\B",
        "RootDir": "M:\\",
        "Filename": "B",
        "Extension": "",
        "RelativeDir": "",
        "Directory": "Temp\\Test\\",
        "RecursiveDir": "",
        "ModifiedTime": "",
        "CreatedTime": "",
        "AccessedTime": "",
        "DefiningProjectFullPath": "M:\\Temp\\Test\\Test.proj",
        "DefiningProjectDirectory": "M:\\Temp\\Test\\",
        "DefiningProjectName": "Test",
        "DefiningProjectExtension": ".proj"
      },
      {
        "Identity": "C",
        "M": "T",
        "FullPath": "M:\\Temp\\Test\\C",
        "RootDir": "M:\\",
        "Filename": "C",
        "Extension": "",
        "RelativeDir": "",
        "Directory": "Temp\\Test\\",
        "RecursiveDir": "",
        "ModifiedTime": "",
        "CreatedTime": "",
        "AccessedTime": "",
        "DefiningProjectFullPath": "M:\\Temp\\Test\\Test.proj",
        "DefiningProjectDirectory": "M:\\Temp\\Test\\",
        "DefiningProjectName": "Test",
        "DefiningProjectExtension": ".proj"
      }
    ]
  }
}

Analysis

No response

Versions & Configurations

>dotnet --version
10.0.100-rc.2.25502.107

>dotnet msbuild -version
18.0.0.50307
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@baronfel</author><body>
Interesting - it seems like our expression parser is pretty strict there. Good find, and thanks for the minimal, descriptive reproduction! </body></comment_new>
</comments>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hello @@copilot, I noticed that you’re changing an .swr file or any file under src/Package/MSBuild.VSSetup.. Please make sure to validate this change by an experimental VS insertion. This is accomplished by pushing to an exp/* branch, which requires write permissions to this repo.

Add SinkWhitespace before checking for next arrow operator in chained transforms/functions to allow whitespace before subsequent arrows. Previously, whitespace before the second and subsequent arrows would cause parsing to fail.

Added comprehensive tests to verify the fix works for various whitespace patterns.

Co-authored-by: JanProvaznik <[email protected]>
Copilot AI changed the title [WIP] Fix whitespace requirement for item function chaining Fix item function chaining with whitespace before arrow operators Nov 16, 2025
Copilot AI requested a review from JanProvaznik November 16, 2025 21:56

@JanProvaznik JanProvaznik left a comment

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.

@copilot good change though I have some feedback to ensure correctness:

Required Changes / Additional Tests
To approve this safely, I require the following test cases to be added to Expander_Tests.cs or ExpressionShredder_Tests.cs:
Trailing Whitespace:
code
C#
// Ensure this does not throw
expression = "@(I -> Count() )";
Quoted Transform Trailing Whitespace:
code
C#
// Ensure this does not throw
expression = "@(I -> 'Replacement' )";
Invalid Syntax after Whitespace (Regression check):
code
C#
// Should throw a clear syntax error, not crash or hang
expression = "@(I -> Count() invalid)";
Summary
The code change is likely correct, but insufficiently tested for "End of Expression" boundaries. Given the criticality of ExpressionShredder, we cannot assume the loop termination logic handles the new state without explicit proof.
Please add the trailing whitespace test cases before merging.

Added tests as requested by reviewer:
- Trailing whitespace after function calls
- Trailing whitespace after quoted transforms
- Invalid syntax after whitespace to ensure proper error handling

All tests pass successfully.

Co-authored-by: JanProvaznik <[email protected]>

Copilot AI commented Nov 19, 2025

Copy link
Copy Markdown
Contributor Author

@copilot good change though I have some feedback to ensure correctness:

Required Changes / Additional Tests...

Added all requested test cases in commit ef43b74:

  1. Trailing whitespace after function call: @(I -> Count() ) - Verified it parses correctly
  2. Trailing whitespace after quoted transform: @(I -> 'Replacement' ) - Verified it parses correctly
  3. Invalid syntax after whitespace: @(I -> Count() invalid) - Verified it's properly rejected without crashing

All 51 ExpressionShredder tests now pass, including the new edge case tests.

@JanProvaznik
JanProvaznik marked this pull request as ready for review November 19, 2025 10:53
Copilot AI review requested due to automatic review settings November 19, 2025 10:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a parser bug where item function chaining failed when whitespace preceded the second or subsequent -> operator. The fix adds SinkWhitespace calls to consume trailing whitespace after processing each transform/function expression, before checking for the next -> operator.

  • Adds whitespace consumption in two methods: MoveNext() and GetReferencedItemNamesAndMetadata()
  • Provides comprehensive test coverage including edge cases for trailing whitespace
  • Minimal change with only 4 lines of production code added

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Build/Evaluation/ExpressionShredder.cs Added SinkWhitespace calls after processing quoted transforms and function expressions in both MoveNext() and GetReferencedItemNamesAndMetadata() methods to consume trailing whitespace before checking for the next -> operator
src/Build.UnitTests/Evaluation/ExpressionShredder_Tests.cs Added comprehensive parser-level tests for chained functions with various whitespace patterns, trailing whitespace after functions/transforms, and invalid syntax validation
src/Build.UnitTests/Evaluation/Expander_Tests.cs Added integration test that validates the fix by building a project with chained item functions containing whitespace before the second arrow operator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Item function chaining whitespace requirement

4 participants