antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
16.92k stars 3.25k forks source link

Visual Studio 2022 Preview 3.1 crashes when using Antlr generated C# code #3274

Open dynamoRando opened 2 years ago

dynamoRando commented 2 years ago

Hello world,

Before I describe the issue, I want to be clear that I think this is a problem that Microsoft should be looking at, but per their request, I'm posting the issue here as well.

I also want to post that I do have a workaround, described at the above link as well.

I have a repository that also illustrates the issue here.

Basically, when using the TSQL grammar from here, if generated using Antlr 4.6.6, when using TSQLParser.cs, Visual Studio 2022 Preview 3.1 will crash if the project is targeting .NET 6.

When I generated the same file using Antlr 4.9.2, the project does not crash.

It's also odd to me that the problem does not manifest itself if I target .NET 5 instead of .NET 6, which is why I thought Microsoft should look at it, but well... you can see the response above.

This seems like it's related to issue #2660, but that is closed already.

Thanks, R.

dynamoRando commented 2 years ago

Just discovered the antlr4cs repository, will cross post here as well.

kaby76 commented 2 years ago

This problem is only incidental to VS2022 and Antlr4cs. It's a bug in the MS C# compiler, specifically in NET6.

I've reproduced the problem with trgen (trgen -t Antlr4cs, modifying the .csproj to use net6.0, then dotnet build). For dotnet build, the C# compiler outputs an internal error:

Microsoft (R) Build Engine version 17.0.0-preview-21378-03+d592862ed for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
     ...
    Using "Csc" task from assembly "C:\Program Files\dotnet\sdk\6.0.100-preview.7.21379.14\Roslyn\Microsoft.Build.Tasks.CodeAnalysis.dll".
     ...
C:\Program Files\dotnet\sdk\6.0.100-preview.7.21379.14\Roslyn\Microsoft.CSharp.Core.targets(75,5): error : Stack overflow. [C:\msys64\home\kenne\qu\tsql\Generated\Test.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.7.21379.14\Roslyn\Microsoft.CSharp.Core.targets(75,5): error :    at Microsoft.CodeAnalysis.SyntaxNode.GetRedAtZero[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef) [C:\msys64\home\kenne\qu\tsql\Generated\Test.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.7.21379.14\Roslyn\Microsoft.CSharp.Core.targets(75,5): error :    at Microsoft.CodeAnalysis.CSharp.Syntax.BinaryExpressionSyntax.GetNodeSlot(Int32) [C:\msys64\home\kenne\qu\tsql\Generated\Test.csproj]
     ...
               Done executing task "Csc" -- FAILED. (TaskId:85)

VS2022 deals with the language server not very well: sometimes it crashes, sometimes it times out, and shows a ribbon error.

I don't know whether there is a compiler option to use a larger stack, or whether it's unbounded.

The compiler does not crash for net6.0 (or net5.0) with Standard Antlr4, which you can recreate via trgen; dotnet build.

My recommendation is you update to Antlr4 C# Runtime Standard (v4.9.2), which is actively maintained. The new file-scoped namespace feature alone is worth moving to Net6.

sharwell commented 2 years ago

This looks like a bug in Visual Studio. I've reopened the original issue and added steps to obtain and attach a crash dump.

dynamoRando commented 2 years ago

Hi @kaby76 - thanks for the insight. Yeah that is ultimately the solution I went with was to regenerate the TSqlParser.cs file after downloading Antlr 4.9.2, and then replace the file that I had in my solution in Visual Studio. It just took a little while to figure out where things went wrong.

Hi @sharwell - thanks for taking the time to review the submission. I do have a repository that will reproduce the issue if you git clone and open in VS 2022 Preview 3.1, available here. Simply open the project in Visual Studio 2022 Preview 3.1 and the IDE will eventually just close out. I'm sure just as @kaby76 pointed out it's a Stack Overflow somewhere.

When I opened the original submission I thought it captured a crash dump, etc as I also tried to submit a video, but I guess not. I will try to follow the link provided in the submission to provide a crash dump in a little while, trying to get back home and take care of some things.

Thanks all. R.

dynamoRando commented 2 years ago

Hi @sharwell - I've followed instructions provided at the link but was unable to get anything outputted to the directory specified in the registry entry. I was, however, able to review Windows Event Viewer and pull a file out and added such commentary to the Developer Community forum. I marked that response as Private to MS since I attached the actual files.

Examining Windows Event Viewer and it seems to confirm what @kaby76 was referring to -

Fault bucket 2193626910751168788, type 5 Event Name: CLR20r3 Response: Not available Cab Id: 0

Problem signature: P1: WAC113LCQ0AZJLA1MRCJQCLDEU4BZX3T P2: 3.0.1039.21923 P3: f773f0d1 P4: Microsoft.CodeAnalysis P5: 4.0.21.40323 P6: bf57c823 P7: 1826 P8: 0 P9: System.StackOverflowException P10:

Attached files: \?\C:\ProgramData\Microsoft\Windows\WER\Temp\WERD40A.tmp.mdmp \?\C:\ProgramData\Microsoft\Windows\WER\Temp\WERE040.tmp.WERInternalMetadata.xml \?\C:\ProgramData\Microsoft\Windows\WER\Temp\WERE09F.tmp.xml \?\C:\ProgramData\Microsoft\Windows\WER\Temp\WERE0B3.tmp.csv \?\C:\ProgramData\Microsoft\Windows\WER\Temp\WERE0F2.tmp.txt

These files may be available here: \?\C:\ProgramData\Microsoft\Windows\WER\ReportArchive\AppCrash_WAC113LCQ0AZJLA1_84a64f985ab66083bb15c0951435ea516bb8c4e_1739a055_3d848245-c778-4917-be4f-aa34cc05aac8

Analysis symbol: Rechecking for solution: 0 Report Id: 042c7d3c-522e-43ae-9e16-78a264003553 Report Status: 268435456 Hashed bucket: 5496243f6f3d8305ae71540d910f9914 Cab Guid: 0

Since I do have a workaround, and I've got multiple conversations going in multiple places, I'm happy to close this thread and this thread, or if someone else would like to, that would also be great.

Thanks again for taking the time to review.