Skip to content

CS9257: "Both partial property declarations must be required or neither may be required" on a required partial property with [ObservableProperty] #969

@nalka0

Description

@nalka0

Describe the bug

On main, the below code generates public partial string Foo ... which yields this error:

CS9257: "Both partial property declarations must be required or neither may be required"

It should generate public required partial string Foo ... instead.

public partial class Test : ObservableObject
{
    [ObservableProperty]
    public required partial string Foo { get; set; }
}

Steps to reproduce

  1. Run dotnet pack src/CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.csproj (on the current main)
  2. Create a WPF App (.NET 8)
  3. Install the package generated by step 1
  4. Add <LangVersion>preview</LangVersion> to the .csproj
  5. Create a class as follows:
using CommunityToolkit.Mvvm.ComponentModel;

namespace WpfApp1;

public partial class Test : ObservableObject
{
    [ObservableProperty]
    public required partial string Foo { get; set; }
}

Expected behavior

public partial class Test : ObservableObject
{
    [ObservableProperty]
    public required partial string Foo { get; set; }
}

Generates public required partial string Foo ... instead of public partial string Foo ...

IDE and version

VS 2022

IDE version

17.11.5

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

main (8.4.0-build.11)

Help us help you

Yes, but only if others can assist

Metadata

Metadata

Assignees

No one assigned

    Labels

    analyzer 👓A new analyzer being implemented or updatedmvvm-toolkit 🧰Issues/PRs for the MVVM Toolkit

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions