dotnet/roslyn

ObservableProperty breaks ExtractInterfaceCodeRefactoringProvider

Open

#68,748 opened on Jun 23, 2023

View on GitHub
 (11 comments) (0 reactions) (1 assignee)C# (4,257 forks)batch import
Area-IDEBughelp wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

Version Used: Visual Studio Enterprise 2022, v17.6.4

Steps to Reproduce:

  • Create a new project with the "WPF Application" template for C#.
  • Add the Communmity.Toolkit.Mvvm package (8.2.0).
  • Add a partial public class and inherit from ObservableObject.
  • Add a private field with the attribute ObservableProperty.
  • Right click on the class name and choose the refactoring action "Extract interface".
public partial class ViewModel : ObservableObject
{
    [ObservableProperty]
    private int _myProperty;
}

A minimal repro, with source-code provided, is ideal. Using sharplab is preferred for compiler/language issues whenever possible.

Diagnostic Id: none

If this is a report about a bug in an analyzer, please include the diagnostic if possible (e.g. "IDE0030").

Expected Behavior:

The interface should be extracted from the class.

If I use this code - just with a public property with out using the ObservableProperty attribute, the interface could be extracted.

public partial class ViewModel : ObservableObject
{
    public int MyProperty { get; set; }
}

Actual Behavior:

Visual Studio shows the following error:

"ExtractInterfaceCodeRefactoringProvider encountered an error and has been disabled."

System.InvalidOperationException : Operation is not valid due to the current state of the object.
   at Microsoft.CodeAnalysis.Shared.Extensions.ISolutionExtensions.GetRequiredDocument(Solution solution,SyntaxTree syntaxTree)
   at async Microsoft.CodeAnalysis.Shared.Utilities.AnnotatedSymbolMapping.CreateAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.ExtractInterface.AbstractExtractInterfaceService.ExtractInterfaceToNewFileAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.ExtractInterface.AbstractExtractInterfaceService.ExtractInterfaceFromAnalyzedTypeAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.ExtractInterface.ExtractInterfaceCodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeActionWithOptions.GetOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.InvokeWorkerAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformActionAsync(<Unknown Parameters>)

I first reported this to the CommunitiyToolkit/dotnet Project: https://github.com/CommunityToolkit/dotnet/issues/726 but as @333fred mentioned there, this is a roslyn bug.

Contributor guide