CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
2.99k stars 294 forks source link

ObservableProperty breaks ExtractInterfaceCodeRefactoringProvider #726

Closed mikeKuester closed 1 year ago

mikeKuester commented 1 year ago

Describe the bug

After adding the ObservableProperty attribute, the refactoring action "Extract Interface" throws an exception in Visual Studio 2022.

"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>)

Steps to reproduce

Environment:

Steps:

public partial class ViewModel : ObservableObject
{
    [ObservableProperty]
    private int _myProperty;
}

Visual studio show 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>)

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; }
}

Screenshots

image

IDE and version

VS 2022

IDE version

17.6.4

Nuget packages

Nuget package version(s)

8.2.0

Help us help you

No, just wanted to report this

Sergio0694 commented 1 year ago

@333fred this seems like it could just be an issue in the ExtractInterfaceCodeRefactoringProvider and not actually a bug in the MVVM Toolkit? Should we create a tracking issue for this in the Roslyn repo and just link back to this here? 🤔

333fred commented 1 year ago

Yup, this is a roslyn bug.

mikeKuester commented 1 year ago

Should I enter it as a new issue in dotnet/roslyn?

333fred commented 1 year ago

Yes

mikeKuester commented 1 year ago

I reported this to dotnet/roslyn, so this issue can be closed.