elishalom / netcodemetrics

Other
33 stars 13 forks source link

'System.InvalidCastException' occurs intermittently when using Visual Studio 2019 (Professional) #11

Open michaelm7456 opened 5 years ago

michaelm7456 commented 5 years ago

When using Visual Studio 2019 Professional, an exception is occassionally thrown with a 'System.InvalidCastException' being recorded in the log.

[Log]

3028 Error Editor or Editor Extension System.InvalidCastException: Unable to cast object of type 'ICSharpCode.NRefactory.CSharp.ReturnStatement' to type 'ICSharpCode.NRefactory.CSharp.BlockStatement'. at ICSharpCode.NRefactory.CSharp.CSharpParser.ConversionVisitor.Visit(Method m) at Mono.CSharp.Method.Accept(StructuralVisitor visitor) at Mono.CSharp.StructuralVisitor.VisitTypeDefinition(TypeDefinition tc) at ICSharpCode.NRefactory.CSharp.CSharpParser.ConversionVisitor.Visit(Class c) at Mono.CSharp.Class.Accept(StructuralVisitor visitor) at ICSharpCode.NRefactory.CSharp.CSharpParser.ConversionVisitor.Visit(NamespaceContainer ns) at Mono.CSharp.NamespaceContainer.Accept(StructuralVisitor visitor) at ICSharpCode.NRefactory.CSharp.CSharpParser.ConversionVisitor.Visit(ModuleContainer mc) at Mono.CSharp.ModuleContainer.Accept(StructuralVisitor visitor) at ICSharpCode.NRefactory.CSharp.CSharpParser.Parse(CompilerCompilationUnit top, String fileName) at ICSharpCode.NRefactory.CSharp.CSharpParser.Parse(ITextSource program, String fileName, Int32 initialLine, Int32 initialColumn) at ICSharpCode.NRefactory.CSharp.CSharpParser.Parse(ITextSource program, String fileName) at ICSharpCode.NRefactory.CSharp.CSharpParser.Parse(TextReader reader, String fileName) at CodeMetrics.Parsing.MethodsExtractor.Extract(String fileCode) at CodeMetrics.Adornments.MetricsAdornment.Init(ITextSnapshot textSnapshot) at CodeMetrics.Adornments.MetricsAdornment..ctor(IWpfTextView view, IMethodsExtractor methodsExtractor, IComplexityCalculator complexityCalculator) at CodeMetrics.Adornments.MeticsAdornmentFactory.TextViewCreated(IWpfTextView textView) at Microsoft.VisualStudio.Text.Editor.Implementation.WpfTextView.<>c__DisplayClass254_0.<BindContentTypeSpecificAssets>b__1() at Microsoft.VisualStudio.Text.Utilities.GuardedOperations.CallExtensionPoint(Object errorSource, Action call) --- End of stack trace from previous location where exception was thrown --- at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)
elishalom commented 5 years ago

Thanks for the report. Is the exception consistent or is it thrown randomly?

michaelm7456 commented 5 years ago

No problem, for myself and my colleague currently using the tool, this error appears to occur intermittently while using Visual Studio 2019.

I've tested to verify whether the exception appears during a certain time or circumstance, such as starting up Visual Studio, loading a solution, or opening certain files in the 'Solution Explorer', but unfortunately it seems to be intermittent from what we can tell.

Thanks in advance.

KevinZonda commented 4 years ago

@elishalom I have the same problem, but at present, it's just the cs file of the winforms that has problems? There's nothing wrong with the other classes.

elishalom commented 4 years ago

Thanks for the update. I don't maintain the extension on a regular basis anymore, so it may take me some time to get to it. But, this issue is the top one I plan to fix.

KevinZonda commented 4 years ago

Thanks for the update. I don't maintain the extension on a regular basis anymore, so it may take me some time to get to it. But, this issue is the top one I plan to fix.

Thank you for bringing us such an excellent extension! We look forward to your update!

elishalom commented 4 years ago

Thanks for the update. I don't maintain the extension on a regular basis anymore, so it may take me some time to get to it. But, this issue is the top one I plan to fix.

Thank you for bringing us such an excellent extension! We look forward to your update!

Can you please share the code that causes the error? Even a dummy version that reproduces it will be very helpful. I'm trying to create a repro of the issue but I'm failing to create a code that raises the exception.

KevinZonda commented 4 years ago

Thanks for the update. I don't maintain the extension on a regular basis anymore, so it may take me some time to get to it. But, this issue is the top one I plan to fix.

Thank you for bringing us such an excellent extension! We look forward to your update!

Can you please share the code that causes the error? Even a dummy version that reproduces it will be very helpful. I'm trying to create a repro of the issue but I'm failing to create a code that raises the exception.

I upload my ActivityLog.xml to Firefox Send. Here is https://send.firefox.com/download/de30e1ab028364ea/#fXVNs7c6paqUdYZbggqGSA

And my Visual Studio Version is Visual Studio Enterprise 2019(VisualStudio.16.Release/16.4.4+29728.190)

elishalom commented 4 years ago

Thanks for the details. I could not reproduce the exception, but, I think I identified a potential spot that causes the exception. I've updated the extension with a fix to that issue. The version with the change is 0.9.1.1. If one of you install it, can you please update on this thread if the exception is raised again?

KevinZonda commented 4 years ago

Thanks for the details. I could not reproduce the exception, but, I think I identified a potential spot that causes the exception. I've updated the extension with a fix to that issue. The version with the change is 0.9.1.1. If one of you install it, can you please update on this thread if the exception is raised again?

@elishalom The frustration is that the problem still seems to exist.

In the picture, my red arrow points to the file I edited. When I open the CS file corresponding to the form, an error will occur, but opening other classes will not appear.

Exception

Details of the extension I installed

Extension Details

Here is the new ActivityLog

https://send.firefox.com/download/b0383d087d78b5ef/#aMWbojoW6lnfrT6M3dp9Kg

KevinZonda commented 4 years ago

Thanks for the details. I could not reproduce the exception, but, I think I identified a potential spot that causes the exception. I've updated the extension with a fix to that issue. The version with the change is 0.9.1.1. If one of you install it, can you please update on this thread if the exception is raised again?

And what I want to add is that Code Metrics will not work in the cs file that the form is bound to. Just like

Wrong

NOT

Right

Also, it's amazing that in WPF project, the file bound by WinForms is normal, but the WPF file is abnormal. In some WinForms project, the CS file bound by the form is abnormal (Some are normal).

In WPF project:

WPF

WinForms in WPF

In WinForms project:

Wrong

Some may work

Right

elishalom commented 4 years ago

Thanks for the detail @KevinZonda. Is it possible for you to share some of the code that causes the exception? I hope that I'll be able to reproduce the issue and track the exception.

KevinZonda commented 4 years ago

Thanks for the detail @KevinZonda. Is it possible for you to share some of the code that causes the exception? I hope that I'll be able to reproduce the issue and track the exception.

I'm not sure how to share such code. I'm just editing a my repository. Here is

https://github.com/KevinZonda/Widget-WPF

In this repository, the xaml.cs will cause the exception, but other cs file will not.

KevinZonda commented 4 years ago

Thanks for the detail @KevinZonda. Is it possible for you to share some of the code that causes the exception? I hope that I'll be able to reproduce the issue and track the exception.

Exception also happens in this file:

https://github.com/elishalom/netcodemetrics/blob/master/CodeMetrics.Adornments/CodeMetricsPackage.cs

Image

elishalom commented 4 years ago

Thanks, that reproduces the error. The exception is due to the fact the complexity extension is based on an old version of NRefactory which does not support new features of the language. I'll port it to roslyn to fix it and potentially support new features of the language.

KevinZonda commented 4 years ago

Thanks, that reproduces the error. The exception is due to the fact the complexity extension is based on an old version of NRefactory which does not support new features of the language. I'll port it to roslyn to fix it and potentially support new features of the language.

That sounds fantastic!

ghadzhigeorgiev commented 4 years ago

Hi,

This issue is still present and quite annoying. In fact it renders the whole extension useless. On top of that, once you get the exception, the extension stops working.

Are there any plans on fixing that soon?

KevinZonda commented 4 years ago

Hi,

This issue is still present and quite annoying. In fact it renders the whole extension useless. On top of that, once you get the exception, the extension stops working.

Are there any plans on fixing that soon?

using roslyn code analysis may help, see more https://github.com/vojtechkacmarik/netcodemetrics and #1

ghadzhigeorgiev commented 4 years ago

using roslyn code analysis may help, see more https://github.com/vojtechkacmarik/netcodemetrics and #1

Thank you but this extension is not installing on latest VS 2019 16.7 Downloaded from Marketplace and attempting to install results in https://prnt.sc/txp3a7 If installation is attempted within VS, result is https://prnt.sc/txp4lj

michaelm7456 commented 4 years ago

Hi guys,

I've raised a Pull Request here on the 'vojtechkacmarik' fork of the original Repo, which means the Extension (with the Roslyn changes) will now work with Visual Studio 2019 once this PR is merged.

The good news is I've got the Roslyn version of the Extension installed successfully after building it locally with the PR changes for updating the VSIX manifest.

The Roslyn version also seems to be able to handle calculations involving Expression Body syntax, where the original Code Metrices Extension I think still has issues with this.

[Screenshot of 'Expression Body' Code samples with Roslyn Code Metrics Tool installed]

Code Metrics with Expression Body

Hope this helps!