Skip to content

Conversation

@StephaneDelcroix
Copy link
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 #23961

This PR fixes XamlC to properly handle the ObsoleteAttribute by:

  1. Extracting and displaying the custom message from [Obsolete("message")] instead of a generic deprecation message
  2. Respecting the isError parameter - when [Obsolete("message", error: true)] is used, XamlC now emits an error (XC0619) instead of a warning (XC0618)
  3. Adding obsolete type detection for classes marked with [Obsolete] when used in XAML

Changes

  • ErrorMessages.resx: Updated ObsoleteProperty message format to include the custom message: "{0}" is deprecated: {1}
  • BuildException.cs: Added XC0619 (ObsoletePropertyError) for error cases when isError=true
  • SetPropertiesVisitor.cs:
    • Added TryGetObsoleteAttribute() helper to extract message and isError from the attribute
    • Added LogObsoleteWarningOrError() helper that logs the appropriate warning or error based on the attribute
    • Updated property/field obsolete checking to use these helpers
  • CreateObjectVisitor.cs: Added obsolete type detection when XAML elements are created

Before

XamlC warning XC0618: Property, Property setter or BindableProperty "MyProperty" is deprecated.

After

XamlC warning XC0618: "MyProperty" is deprecated: Use NewProperty instead.
XamlC error XC0619: "MyProperty" is obsolete: This property will be removed in the next version.

Testing

  • Added Maui23961.xaml / .cs - tests for warnings with error: false
  • Added Maui23961Error.rt.xaml / .cs - tests for errors with error: true
  • All 1780+ existing Xaml.UnitTests pass
  • Samples build successfully

Fixes #23961

- Extract and display the custom message from ObsoleteAttribute
- Respect the isError parameter: when true, emit XC0619 error instead of XC0618 warning
- Add obsolete type detection for classes used in XAML
- Add XC0619 (ObsoletePropertyError) for error cases
- Update message format to include the attribute's message text
- Add unit tests for both warning and error scenarios
Copilot AI review requested due to automatic review settings December 1, 2025 20:11
@StephaneDelcroix StephaneDelcroix added the area-xaml XAML, CSS, Triggers, Behaviors label Dec 1, 2025
@StephaneDelcroix StephaneDelcroix added this to the .NET 10.0 SR3 milestone Dec 1, 2025
Copilot finished reviewing on behalf of StephaneDelcroix December 1, 2025 20:14
Copy link
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 enhances XamlC to properly handle the ObsoleteAttribute by extracting and displaying custom messages and respecting the isError parameter to differentiate between warnings (XC0618) and errors (XC0619).

  • Adds helper methods TryGetObsoleteAttribute() and LogObsoleteWarningOrError() to extract obsolete attribute information and log appropriate diagnostics
  • Updates error message resources to include custom messages from ObsoleteAttribute
  • Extends obsolete checking to types in addition to properties, fields, and bindable properties

Reviewed changes

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

Show a summary per file
File Description
src/Controls/tests/Xaml.UnitTests/Issues/Maui23961Error.xaml.cs Test code for obsolete attributes with error: true parameter
src/Controls/tests/Xaml.UnitTests/Issues/Maui23961Error.rt.xaml XAML test file for obsolete error cases
src/Controls/tests/Xaml.UnitTests/Issues/Maui23961.xaml.cs Test code for obsolete attributes with error: false parameter (warnings)
src/Controls/tests/Xaml.UnitTests/Issues/Maui23961.xaml XAML test file for obsolete warning cases
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj Adds XC0619 to WarningsNotAsErrors to allow compilation of test files with obsolete errors
src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs Implements obsolete attribute extraction logic and refactors obsolete checking to use new helper methods
src/Controls/src/Build.Tasks/ErrorMessages.resx Updates error messages for obsolete warnings and errors to include custom messages
src/Controls/src/Build.Tasks/CreateObjectVisitor.cs Adds obsolete type checking when XAML elements are created
src/Controls/src/Build.Tasks/BuildException.cs Adds XC0619 error code for obsolete errors

else
{
// Runtime doesn't check for obsolete at compile time
var page = new Maui23961Error(inflator);
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

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

The constructor call new Maui23961Error(inflator) will fail because the class only has a parameterless constructor public Maui23961Error() => InitializeComponent(); (line 14). This parameterized constructor pattern with XamlInflator is not defined for this class.

Looking at other test files, the XamlInflator parameter constructor is typically auto-generated for regular XAML files (not .rt.xaml). However, this is a .rt.xaml file (runtime-only), which means the auto-generated XamlInflator constructor won't exist.

The test logic on lines 46-56 appears to have incorrect branching. For the XamlInflator.SourceGen case, it correctly recognizes that .rt.xaml files aren't processed, but the else block (line 51-56) attempts to use a non-existent constructor.

Consider revising the test to either:

  1. Use the parameterless constructor: var page = new Maui23961Error();
  2. Skip the runtime test path entirely for .rt.xaml files since they're meant for runtime-only processing
Suggested change
var page = new Maui23961Error(inflator);
var page = new Maui23961Error();

Copilot uses AI. Check for mistakes.
</data>
<data name="ObsoleteProperty" xml:space="preserve">
<value>Property, Property setter or BindableProperty "{0}" is deprecated.</value>
<value>"{0}" is deprecated: {1}</value>
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

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

The error message uses "is obsolete" while the warning message uses "is deprecated". For consistency with C# compiler behavior, both should use the same terminology. The C# compiler uses "is obsolete" for both warnings (CS0618) and errors (CS0619).

Consider changing the warning message from "{0}" is deprecated: {1} to "{0}" is obsolete: {1} to match standard C# compiler terminology and maintain consistency with the error message.

Suggested change
<value>"{0}" is deprecated: {1}</value>
<value>"{0}" is obsolete: {1}</value>

Copilot uses AI. Check for mistakes.
}

if ( dataTypeNode is ElementNode enode
if (dataTypeNode is ElementNode enode
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

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

[nitpick] This whitespace change (removing spaces after if) is unrelated to the PR's purpose of fixing ObsoleteAttribute handling. The PR should focus only on changes directly related to the obsolete attribute functionality to keep the changes minimal and reviewable.

Consider reverting this whitespace change.

Suggested change
if (dataTypeNode is ElementNode enode
if ( dataTypeNode is ElementNode enode

Copilot uses AI. Check for mistakes.
Samples use obsolete types like ListView, ViewCell, etc. which now
trigger XC0618 warnings. Add these codes to WarningsNotAsErrors to
prevent build failures when TreatWarningsAsErrors is enabled.
The TestResultPage.xaml uses Frame which is obsolete.
The ManualTests use obsolete types like TextCell in CollectionView tests.
The test now receives AggregateException when treatWarningsAsErrors is
true because both the obsolete Compatibility.StackLayout type and the
binding without DataType produce errors. Updated test to use Assert.Catch
and verify the expected XC0022 error is present.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-xaml XAML, CSS, Triggers, Behaviors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XamlC ignores isError option in ObsoleteAttribute

2 participants