roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

StackOverflowException when compiling project with large (12MB) cs file and targeting net6.0

Open omellet opened this issue 3 years ago • 7 comments

Version Used: 4.3.0-3.22423.10 (b35bb0ba)

Steps to Reproduce:

  1. Build project and use TargetFramework net6.0

(A sharplab repro is ideal for compiler/language issues whenever possible)

Expected Behavior: Builds in a couple seconds

Actual Behavior: Crashes with StackOverflowException:

Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.BinaryExpressionSyntax.CreateRed(Microsoft.CodeAnalysis.SyntaxNode parent, int position) Line 3537	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.SyntaxNode.GetRedAtZero<Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax>(ref Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax field) Line 196	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.Syntax.BinaryExpressionSyntax.GetNodeSlot(int index) Line 1688	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.ChildSyntaxList.ItemInternal(Microsoft.CodeAnalysis.SyntaxNode node, int index) Line 126	C#
 	System.Text.Json.SourceGeneration.dll!Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.GetMatchingNodes.__recurseChildren|7_3(Microsoft.CodeAnalysis.SyntaxNode node, ref System.Collections.Generic.ValueListBuilder<(string aliasName, string symbolName)> localAliases, ref System.Collections.Generic.ValueListBuilder<string> seenNames, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> results, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> attributeTargets, ref Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.<>c__DisplayClass7_0 value) Line 300	C#
 	System.Text.Json.SourceGeneration.dll!Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.GetMatchingNodes.__recurse|7_0(Microsoft.CodeAnalysis.SyntaxNode node, ref System.Collections.Generic.ValueListBuilder<(string aliasName, string symbolName)> localAliases, ref System.Collections.Generic.ValueListBuilder<string> seenNames, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> results, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> attributeTargets, ref Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.<>c__DisplayClass7_0 value) Line 288	C#
 	System.Text.Json.SourceGeneration.dll!Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.GetMatchingNodes.__recurseChildren|7_3(Microsoft.CodeAnalysis.SyntaxNode node, ref System.Collections.Generic.ValueListBuilder<(string aliasName, string symbolName)> localAliases, ref System.Collections.Generic.ValueListBuilder<string> seenNames, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> results, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> attributeTargets, ref Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.<>c__DisplayClass7_0 value) Line 300	C#
 	[The 2 frame(s) above this were repeated 2514 times]	
 	System.Text.Json.SourceGeneration.dll!Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.GetMatchingNodes.__recurse|7_0(Microsoft.CodeAnalysis.SyntaxNode node, ref System.Collections.Generic.ValueListBuilder<(string aliasName, string symbolName)> localAliases, ref System.Collections.Generic.ValueListBuilder<string> seenNames, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> results, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> attributeTargets, ref Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.<>c__DisplayClass7_0 value) Line 288	C#
 	System.Text.Json.SourceGeneration.dll!Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.GetMatchingNodes.__recurseChildren|7_3(Microsoft.CodeAnalysis.SyntaxNode node, ref System.Collections.Generic.ValueListBuilder<(string aliasName, string symbolName)> localAliases, ref System.Collections.Generic.ValueListBuilder<string> seenNames, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> results, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> attributeTargets, ref Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.<>c__DisplayClass7_0 value) Line 300	C#
 	System.Text.Json.SourceGeneration.dll!Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.GetMatchingNodes.__recurse|7_0(Microsoft.CodeAnalysis.SyntaxNode node, ref System.Collections.Generic.ValueListBuilder<(string aliasName, string symbolName)> localAliases, ref System.Collections.Generic.ValueListBuilder<string> seenNames, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> results, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> attributeTargets, ref Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.<>c__DisplayClass7_0 value) Line 255	C#
 	System.Text.Json.SourceGeneration.dll!Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.GetMatchingNodes.__recurseChildren|7_3(Microsoft.CodeAnalysis.SyntaxNode node, ref System.Collections.Generic.ValueListBuilder<(string aliasName, string symbolName)> localAliases, ref System.Collections.Generic.ValueListBuilder<string> seenNames, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> results, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> attributeTargets, ref Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.<>c__DisplayClass7_0 value) Line 300	C#
 	System.Text.Json.SourceGeneration.dll!Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.GetMatchingNodes.__recurse|7_0(Microsoft.CodeAnalysis.SyntaxNode node, ref System.Collections.Generic.ValueListBuilder<(string aliasName, string symbolName)> localAliases, ref System.Collections.Generic.ValueListBuilder<string> seenNames, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> results, ref System.Collections.Generic.ValueListBuilder<Microsoft.CodeAnalysis.SyntaxNode> attributeTargets, ref Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.<>c__DisplayClass7_0 value) Line 245	C#
 	System.Text.Json.SourceGeneration.dll!Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.GetMatchingNodes(Microsoft.CodeAnalysis.DotnetRuntime.Extensions.ISyntaxHelper syntaxHelper, Microsoft.CodeAnalysis.DotnetRuntime.Extensions.GlobalAliases globalAliases, Microsoft.CodeAnalysis.SyntaxTree syntaxTree, string name, System.Func<Microsoft.CodeAnalysis.SyntaxNode, System.Threading.CancellationToken, bool> predicate, System.Threading.CancellationToken cancellationToken) Line 218	C#
 	System.Text.Json.SourceGeneration.dll!Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.ForAttributeWithSimpleName.AnonymousMethod__5((Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.SyntaxTreeInfo Left, Microsoft.CodeAnalysis.DotnetRuntime.Extensions.GlobalAliases Right) tuple, System.Threading.CancellationToken c) Line 152	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.UserFunctionExtensions.WrapUserFunction.AnonymousMethod__0((System.__Canon, System.__Canon) input, System.Threading.CancellationToken token) Line 31	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.TransformNode<(Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.SyntaxTreeInfo, Microsoft.CodeAnalysis.DotnetRuntime.Extensions.GlobalAliases), (Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>)>..ctor.AnonymousMethod__0((Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.SyntaxTreeInfo, Microsoft.CodeAnalysis.DotnetRuntime.Extensions.GlobalAliases) i, System.Threading.CancellationToken token) Line 24	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.TransformNode<(Microsoft.CodeAnalysis.DotnetRuntime.Extensions.SyntaxValueProviderExtensions.SyntaxTreeInfo, Microsoft.CodeAnalysis.DotnetRuntime.Extensions.GlobalAliases), (Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>)>.UpdateStateTable(Microsoft.CodeAnalysis.DriverStateTable.Builder builder, Microsoft.CodeAnalysis.NodeStateTable<(Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>)> previousTable, System.Threading.CancellationToken cancellationToken) Line 72	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.DriverStateTable.Builder.GetLatestStateTableForNode<(Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>)>(Microsoft.CodeAnalysis.IIncrementalGeneratorNode<(Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>)> source) Line 59	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.TransformNode<(Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>), (Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>)>.UpdateStateTable(Microsoft.CodeAnalysis.DriverStateTable.Builder builder, Microsoft.CodeAnalysis.NodeStateTable<(Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>)> previousTable, System.Threading.CancellationToken cancellationToken) Line 43	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.DriverStateTable.Builder.GetLatestStateTableForNode<(Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>)>(Microsoft.CodeAnalysis.IIncrementalGeneratorNode<(Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>)> source) Line 59	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.CombineNode<(Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>), Microsoft.CodeAnalysis.Compilation>.UpdateStateTable(Microsoft.CodeAnalysis.DriverStateTable.Builder graphState, Microsoft.CodeAnalysis.NodeStateTable<((Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>), Microsoft.CodeAnalysis.Compilation)> previousTable, System.Threading.CancellationToken cancellationToken) Line 32	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.DriverStateTable.Builder.GetLatestStateTableForNode<((Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>), Microsoft.CodeAnalysis.Compilation)>(Microsoft.CodeAnalysis.IIncrementalGeneratorNode<((Microsoft.CodeAnalysis.SyntaxTree, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxNode>), Microsoft.CodeAnalysis.Compilation)> source) Line 59	C#

omellet avatar Sep 22 '22 20:09 omellet

Notable that I'm trying to suppress code analysis for this project to work around the crash but I still see three analyzers on the csc.exe command line. When building w/ net472 target I don't see any analyzers being used.

omellet avatar Sep 22 '22 20:09 omellet

Can you supply the file that is causing the crash?

CyrusNajmabadi avatar Sep 22 '22 20:09 CyrusNajmabadi

I can't, unfortunately. It's proprietary code. I'll try to recreate it in a test project.

omellet avatar Sep 22 '22 20:09 omellet

@jaredpar this is my code. Should be easy to convert it to non-recursive. I can do that when I get back from vacation. Or, if this is critical, I can walk anyone through it.

CyrusNajmabadi avatar Sep 22 '22 20:09 CyrusNajmabadi

@omellet no worries! The stack and explanation are likely sufficient here. Thanks for reporting this, and sorry for he hassle here!

CyrusNajmabadi avatar Sep 22 '22 20:09 CyrusNajmabadi

Am I wrong to think the analyzer shouldn't be running at all if I see /skipanalyzers+ on the command line?

omellet avatar Sep 22 '22 20:09 omellet

This isn't an analyzer. It's a source generator. :-)

CyrusNajmabadi avatar Sep 22 '22 21:09 CyrusNajmabadi