Skip to content

[XSG] Fix Style setter with content syntax in SourceGen - #32890

Merged
StephaneDelcroix merged 6 commits into
mainfrom
copilot/fix-absolute-layout-style-error
Nov 28, 2025
Merged

StephaneDelcroix merged 6 commits into
mainfrom
copilot/fix-absolute-layout-style-error

Conversation

Copilot AI commented Nov 27, 2025 •

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description of Change

XAML SourceGen produces invalid C# when Setter uses content property syntax:

<Style x:Key="NetworkIndicator" TargetType="Image">
    <Setter Property="AbsoluteLayout.LayoutBounds">10,10,20,20</Setter>
</Style>

Generated code was:

.Value = "10,10,20,20";  // ← invalid: empty variable name
var setter = new Setter {Property = ..., Value = ...};

Root cause: When Setter value is a collection item (content property syntax) rather than an attribute, the inline initialization optimization marked properties to skip but missed the content property. SetPropertiesVisitor then processed it with the placeholder empty variable name.

Fix: In CreateValuesVisitor, when handling known value providers that can be inlined, also add the content property name to SkipProperties when the value comes from a collection item.

Tests added:

  • Maui32879.xaml/.xaml.cs - Xaml.UnitTests to verify the fix works across all XAML inflators (Runtime, XamlC, SourceGen)
  • Maui32879Tests.cs - SourceGen.UnitTests snapshot test that validates the source generator produces valid C# code, with the expected generated output clearly shown in the test

Issues Fixed

Fixes #32879

Copilot AI and others added 2 commits November 27, 2025 16:16
When using content property syntax for Setter values like:
  <Setter Property="AbsoluteLayout.LayoutBounds">10,10,20,20</Setter>

The source generator was producing invalid code:
  .Value = "10,10,20,20";
  var setter = new Setter {...};

The fix adds the content property name to SkipProperties when the value
is specified as a collection item, preventing SetPropertiesVisitor from
trying to process it separately with an empty variable name.

Fixes #32879

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix style with setter on AbsoluteLayout.LayoutBounds Fix Style setter with attached property content syntax in SourceGen Nov 27, 2025
Copilot AI requested a review from simonrozsival November 27, 2025 16:25
@simonrozsival

Copy link
Copy Markdown
Member

@copilot Please add also a SourceGen test

Added Maui32879Tests.cs with two tests:
- StyleSetterWithAttachedPropertyContentSyntax: Tests the fixed issue where
  using content property syntax like <Setter Property="...">value</Setter>
  previously generated invalid C# code
- StyleSetterWithAttachedPropertyAttributeSyntax: Tests that the normal
  attribute syntax still works correctly

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>

Copilot AI commented Nov 27, 2025

Copy link
Copy Markdown
Contributor Author

@copilot Please add also a SourceGen test

Added SourceGen unit tests in Maui32879Tests.cs (commit 2422c6a):

  • StyleSetterWithAttachedPropertyContentSyntax: Tests the fixed issue with content property syntax
  • StyleSetterWithAttachedPropertyAttributeSyntax: Verifies attribute syntax still works

@simonrozsival simonrozsival 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 address my comments please

- Remove Console.WriteLine debug statements
- Simplify asserts inline with result.Diagnostics

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Transform Maui32879Tests to use the snapshot test pattern to clearly show
the expected generated C# code. This makes it easy to verify that the
Setter is properly generated inline without the invalid `.Value = ...`
dead code line.

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Copilot AI requested a review from simonrozsival November 27, 2025 17:19
@simonrozsival
simonrozsival marked this pull request as ready for review November 27, 2025 17:31
Copilot AI review requested due to automatic review settings November 27, 2025 17:31
@simonrozsival

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

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 bug in the XAML SourceGen where using content property syntax for Setter.Value (e.g., <Setter Property="...">10,10,20,20</Setter>) generated invalid C# code. The bug occurred when the inline initialization optimization for known value providers (like Setter) marked explicit properties to skip but missed the content property when it was specified as a collection item.

Key changes:

  • Added logic to skip the content property when it's specified as a collection item for types that can be inlined (known value providers)
  • Added comprehensive test coverage in both Xaml.UnitTests and SourceGen.UnitTests to verify the fix

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/Controls/src/SourceGen/Visitors/CreateValuesVisitor.cs Added logic to detect and skip content properties specified as collection items when handling known value providers that can be inlined
src/Controls/tests/Xaml.UnitTests/Issues/Maui32879.xaml XAML test file demonstrating the Style setter with attached property using content property syntax
src/Controls/tests/Xaml.UnitTests/Issues/Maui32879.xaml.cs Runtime test that verifies the fix works across all XAML inflators (Runtime, XamlC, SourceGen)
src/Controls/tests/SourceGen.UnitTests/Maui32879Tests.cs Snapshot test validating the source generator produces valid C# code with the expected output

@StephaneDelcroix StephaneDelcroix changed the title Fix Style setter with attached property content syntax in SourceGen [XSG] Fix Style setter with attached property content syntax in SourceGen Nov 27, 2025
@StephaneDelcroix StephaneDelcroix changed the title [XSG] Fix Style setter with attached property content syntax in SourceGen [XSG] Fix Style setter with content syntax in SourceGen Nov 27, 2025
@StephaneDelcroix StephaneDelcroix added area-xaml XAML, CSS, Triggers, Behaviors xsg Xaml sourceGen labels Nov 28, 2025
@StephaneDelcroix
StephaneDelcroix enabled auto-merge (squash) November 28, 2025 07:51
@StephaneDelcroix
StephaneDelcroix merged commit dc76e8c into main Nov 28, 2025
@StephaneDelcroix
StephaneDelcroix deleted the copilot/fix-absolute-layout-style-error branch November 28, 2025 10:33
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-xaml XAML, CSS, Triggers, Behaviors xsg Xaml sourceGen

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Style with setter on AbsoluteLayout.LayoutBounds and SourceGen fails

4 participants