icsharpcode / CodeConverter

Convert code from C# to VB.NET and vice versa using Roslyn
https://icsharpcode.github.io/CodeConverter/
MIT License
827 stars 217 forks source link

Exception after "Convert to C#" in unsaved window #1023

Open JohnRosePLI opened 1 year ago

JohnRosePLI commented 1 year ago

Hello,

When I want to translate any VB.NET Code to C# in Visual Studio 2022 I get the following error message: grafik

Regards John

GrahamTheCoder commented 1 year ago

Looks like an odd error. Since the code converter isn't shown in the stack trace I'll need a bit more info. Could you let me know which menu item you are using. E.g. Is it the right click menu in solution explorer on a single file? Feel free to screenshot if easier to describe. The other useful things would be to know if this happens instantly, whether the code converter section of the output window shows anything, and if this happens even when all files are already saved since it seems related to saving. Visual studio's about window has a copy info button which lists other installed extensions which could be interacting here too.

Thanks!

GrahamTheCoder commented 1 year ago

Thanks I've now actually managed to reproduce this issue. It happens when you use the right click menu within a code editor, or on the Edit menu to convert a single file that has unsaved changes. Once it's occurred, the code editor will be in a broken state (and prevent any other attempts at conversion) until you close the unsaved file (you'll no longer be able to save it).

Workaround: Save all changes before running conversion

I've spent a bit of time looking at making a fix to automatically do this but haven't succeeded yet by running the Dte.SaveAll command

For my own reference, the full stack trace is

    Microsoft.VisualStudio.CommonIDE.dll!Microsoft.VisualStudio.CommonIDE.Solutions.Dte.IdeAutomationObject<System.__Canon>.RunOnMainThreadHr.AnonymousMethod__21_0(System.Func<int> innerFunc) Unknown
>   ICSharpCode.CodeConverter.VsExtension.dll!ICSharpCode.CodeConverter.VsExtension.VisualStudioInteraction.EnsureBuiltAsync(System.Collections.Generic.IReadOnlyCollection<EnvDTE.Project> projects, System.Func<string, System.Threading.Tasks.Task> writeMessageAsync) Line 197  C#
    [Resuming Async Method] 
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
    mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()    Unknown
    mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0() Unknown
    mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()   Unknown
    mscorlib.dll!System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.GetActionLogDelegate.AnonymousMethod__0()   Unknown
    Microsoft.VisualStudio.Threading.dll!Microsoft.VisualStudio.Threading.JoinableTaskFactory.SingleExecuteProtector.TryExecute()   Unknown
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs)  Unknown
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown
    WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl()   Unknown
    WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(object obj)   Unknown
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
    WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext executionContext, System.Threading.ContextCallback callback, object state)  Unknown
    WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke()   Unknown
    WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue()  Unknown
    WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled)  Unknown
    WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown
    WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs)  Unknown
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown
    WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs)   Unknown
    WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam)  Unknown
    [Async Call Stack]  
    [Async] ICSharpCode.CodeConverter.VsExtension.dll!ICSharpCode.CodeConverter.VsExtension.CodeConversion.EnsureBuiltAsync(System.Collections.Generic.IReadOnlyCollection<EnvDTE.Project> readOnlyCollection) Line 115 C#
    [Async] ICSharpCode.CodeConverter.VsExtension.dll!ICSharpCode.CodeConverter.VsExtension.CodeConversion.ConvertDocumentAsync<ICSharpCode.CodeConverter.CSharp.VBToCSConversion>(string documentFilePath, Microsoft.VisualStudio.Text.Span selected, System.Threading.CancellationToken cancellationToken) Line 90    C#
    [Async] ICSharpCode.CodeConverter.VsExtension.dll!ICSharpCode.CodeConverter.VsExtension.ConvertVBToCSCommand.ConvertDocumentAsync(string documentPath, Microsoft.VisualStudio.Text.Span selected, System.Threading.CancellationToken cancellationToken) Line 183    C#
    [Async] ICSharpCode.CodeConverter.VsExtension.dll!ICSharpCode.CodeConverter.VsExtension.ConvertVBToCSCommand.CodeEditorMenuItemCallbackAsync(System.Threading.CancellationToken cancellationToken) Line 156 C#
    [Async] ICSharpCode.CodeConverter.VsExtension.dll!ICSharpCode.CodeConverter.VsExtension.OleMenuCommandWithBlockingStatus..ctor.__ExecuteAsync|1() Line 28   C#
    [Async] Microsoft.VisualStudio.Threading.dll!Microsoft.VisualStudio.Threading.JoinableTask.SetWrappedTask.AnonymousMethod__91_0(System.Threading.Tasks.Task t, object s)    Unknown
JohnRosePLI commented 1 year ago

Thank you for your research and infos so far. From your feedback I could identify the Problem on my site. The error occurs when the chosen file has not been saved before starting the converter. So I got the solution.