Skip to content

[XSG] Fix invalid code generation for Setter with OnPlatform without Default#33681

Merged
PureWeen merged 2 commits intomainfrom
fix/sourcegen-onplatform-setter-33676
Jan 25, 2026
Merged

[XSG] Fix invalid code generation for Setter with OnPlatform without Default#33681
PureWeen merged 2 commits intomainfrom
fix/sourcegen-onplatform-setter-33676

Conversation

@StephaneDelcroix
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

Fixes #33676

When a Setter uses {OnPlatform} without a Default value and the target platform doesn't match any specified platforms, SourceGen generates invalid C# code like:

var object0 = (object)((global::Microsoft.Maui.Controls.Xaml.IValueProvider)).ProvideValue(xamlServiceProvider);

This causes compilation errors:

  • CS0119: 'IValueProvider' is a type, which is not valid in the given context
  • CS0120: An object reference is required for the non-static member

Root Cause

When {OnPlatform} has no matching platform and no Default, SimplifyOnPlatformVisitor removes the Value property from the Setter. However, SetterValueProvider.CanProvideValue() incorrectly returned true for this case (because null is neither MarkupNode nor ElementNode), causing CreateValuesVisitor to register a placeholder with an empty name. Later, ProvideValue generated invalid code using that empty name.

Fix

Added a null check in CanProvideValue() to return false when there's no value node, preventing the invalid code path.

Testing

  • Added unit test Maui33676 that reproduces the issue
  • Verified all 108 OnPlatform-related tests pass
  • Verified all 48 Setter-related tests pass

…out Default

Fixes #33676

When a Setter uses {OnPlatform} without a Default value and the target
platform doesn't match, SimplifyOnPlatformVisitor removes the Value
property. However, SetterValueProvider.CanProvideValue() incorrectly
returned true for this case (null is neither MarkupNode nor ElementNode),
causing invalid code generation like:
  ((IValueProvider)).ProvideValue(...)

The fix adds a null check in CanProvideValue() to return false when
there's no value node, preventing the invalid code path.
Copilot AI review requested due to automatic review settings January 22, 2026 15:51
@StephaneDelcroix StephaneDelcroix changed the title [SourceGen] Fix invalid code generation for Setter with OnPlatform without Default [XSG] Fix invalid code generation for Setter with OnPlatform without Default Jan 22, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 critical bug in SourceGen where Setter elements using {OnPlatform} without a Default value would generate invalid C# code when the target platform didn't match any specified platforms. The fix adds a null check in SetterValueProvider.CanProvideValue() to prevent attempting to generate code for setters with no value.

Changes:

  • Added null check in SetterValueProvider.CanProvideValue() to return false when GetValueNode() returns null
  • Added comprehensive test coverage for both non-matching and matching platform scenarios
  • Test verifies that invalid code is not generated and that matching platforms still work correctly

Reviewed changes

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

File Description
src/Controls/src/SourceGen/SetterValueProvider.cs Added null check in CanProvideValue() to handle cases where OnPlatform removes the Value property (lines 19-22)
src/Controls/tests/Xaml.UnitTests/Issues/Maui33676.xaml XAML test file with Setter using OnPlatform without Default value for iOS platform only
src/Controls/tests/Xaml.UnitTests/Issues/Maui33676.xaml.cs Test file with two test methods: one verifying no invalid code is generated for non-matching platforms (Android), and one verifying correct behavior for matching platforms (iOS)

When a Setter inside a VisualState.Setters has an OnPlatform value with
no matching platform and no Default, the Setter should be skipped
entirely rather than falling through to IValueProvider handling.

The fix:
1. SetterValueProvider.TryProvideValue now returns true with null
   returnType when valueNode is null (signaling skip)
2. NodeSGExtensions.TryProvideValue detects this sentinel and removes
   the node from Variables
3. SetPropertiesVisitor and SetPropertyHelpers check if node exists
   in Variables before adding to collection

Adds unit test Maui33676_VisualState for the VisualState.Setters case.
@PureWeen
Copy link
Copy Markdown
Member

/azp run maui-pr-uitests, maui-pr-devicetests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

@PureWeen PureWeen disabled auto-merge January 25, 2026 04:54
@PureWeen PureWeen merged commit 2a49837 into main Jan 25, 2026
160 of 163 checks passed
@PureWeen PureWeen deleted the fix/sourcegen-onplatform-setter-33676 branch January 25, 2026 04:54
StephaneDelcroix added a commit that referenced this pull request Feb 14, 2026
Fixes #34039

When <Setter.Value> is used as a property element with a namespace URI
other than MauiUri (e.g., MauiGlobalUri with implicit xmlns),
GetValueNode() failed to find it. Since PR #33681 changed the null
return to a skip sentinel, this caused the Setter to be removed from
Variables entirely, preventing the .Add() call from being generated.

The fix makes GetValueNode() iterate all properties matching
LocalName == "Value" instead of checking specific namespace URIs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators Feb 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[XSG] Setter with OnPlatform without Default generates invalid code when platform doesn't match

4 participants