dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.08k stars 4.04k forks source link

ObservableProperty breaks ExtractInterfaceCodeRefactoringProvider #68748

Open mikeKuester opened 1 year ago

mikeKuester commented 1 year ago

Version Used: Visual Studio Enterprise 2022, v17.6.4

Steps to Reproduce:

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.

mikeKuester commented 7 months ago

10 month later and still no solution for that?

CyrusNajmabadi commented 7 months ago

@mikeKuester There's only been one report of this. If you're interested in contributing a fix, we'd be happy to work with you on that.

mikeKuester commented 7 months ago

I'm just starting a discussion at the Community Toolkit project: https://github.com/CommunityToolkit/dotnet/discussions/871 I was also wondering that no one adds a comment here.

nvstrien commented 6 months ago

I reported it here, but the issue was closed https://developercommunity.visualstudio.com/t/ExtractInterfaceCodeRefactoringProvider-/10410407 and you can find references here https://github.com/dotnet/roslyn/issues/68971 and then back to you https://github.com/dotnet/roslyn/issues/68748. No solution yet.

CyrusNajmabadi commented 6 months ago

@nvstrien @mikeKuester we'd be happy to take community contributions here. Thanks!

nvstrien commented 6 months ago

I am afraid I am unfamiliar with how to debug an issue like this. Also, I was under the impression that the ExtractInterfaceCodeRefactoringProvider is Microsoft maintained code, but perhaps I am mistaken. If @CyrusNajmabadi you can provide me a pointer how to approach this, I can give it a try.

CyrusNajmabadi commented 6 months ago

ExtractInterfaceCodeRefactoringProvider is Microsoft maintained code

This type is contained within this project it is open source and anyone can contribute to it :-)

On the phone right now. But see our home page for a link on how to contribute! :-)

Skintkingle commented 4 months ago

I am also experiencing this now when using RelayCommand attributes from the same Community.Toolkit.Mvvm library. It appears to be an issue experienced when partial classes are used for code gen, which is increasingly likely in the world of Blazor and the trimmer doing trimmer things. Has anyone started looking at this?

usefulBeeing commented 3 months ago

I hope this issue gets some attention because it is causing productivity issues.

Where did the experts disappear?! :loudspeaker:

CyrusNajmabadi commented 3 months ago

@usefulBeeing see https://github.com/dotnet/roslyn/issues/68748#issuecomment-2075043435