Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
michaelm7456 opened this issue Sep 30, 2019 · 19 comments

Comments

@michaelm7456
Copy link

michaelm7456 commented Sep 30, 2019

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

[Log]

3028 2019/09/30 07:29:37.148 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
Copy link
Owner

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

@michaelm7456 michaelm7456 changed the title 'System.InvalidCastException' occurs when starting up Visual Studio 2019 (Professional) 'System.InvalidCastException' occurs intermittently when starting up Visual Studio 2019 (Professional) Oct 1, 2019
@michaelm7456 michaelm7456 changed the title 'System.InvalidCastException' occurs intermittently when starting up Visual Studio 2019 (Professional) 'System.InvalidCastException' occurs intermittently when using Visual Studio 2019 (Professional) Oct 1, 2019
@michaelm7456
Copy link
Author

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
Copy link

@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
Copy link
Owner

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
Copy link

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
Copy link
Owner

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
Copy link

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
Copy link
Owner

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
Copy link

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
Copy link

KevinZonda commented Mar 14, 2020

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
Copy link
Owner

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
Copy link

KevinZonda commented Mar 16, 2020

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
Copy link

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
Copy link
Owner

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants