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

Stack overflow when handling chained/nested else-if #75043

Closed
sgkoishi opened this issue Sep 10, 2024 · 6 comments
Closed

Stack overflow when handling chained/nested else-if #75043

sgkoishi opened this issue Sep 10, 2024 · 6 comments
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@sgkoishi
Copy link

sgkoishi commented Sep 10, 2024

Version Used:
Microsoft.CodeAnalysis.CSharp 4.10.0.0

Steps to Reproduce and result:

  1. Create a Program.cs with a if statement and 2000 else if attached to it. (I know this is bad...)
  2. Run dotnet format
  3. (Windows) Got two errors:
    • Unable to organize imports for FILENAME. The document is too complex.
      same as VisualBasicOrganizeImportsService.OrganizeImportsAsync throws InsufficientExecutionStackException #44710
      https://github.com/dotnet/sdk/blob/5171dbf074918745f77c1ecce3474977fa45381d/src/BuiltInTools/dotnet-format/Formatters/OrganizeImportsFormatter.cs#L53-L59
      Unhandled exception: System.InsufficientExecutionStackException: Insufficient stack to continue executing the program safely. This can happen from having too many functions on the call stack or function on the stack using too much stack space.
         at System.Runtime.CompilerServices.RuntimeHelpers.EnsureSufficientExecutionStack()
         (visit to nodes)
      
         at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node)
         at Microsoft.CodeAnalysis.CSharp.Formatting.TriviaRewriter.Visit(SyntaxNode node)
         at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitElseClause(ElseClauseSyntax node)
         at Microsoft.CodeAnalysis.CSharp.Syntax.ElseClauseSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
         at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node)
         at Microsoft.CodeAnalysis.CSharp.Formatting.TriviaRewriter.Visit(SyntaxNode node)
         at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitIfStatement(IfStatementSyntax node)
         at Microsoft.CodeAnalysis.CSharp.Syntax.IfStatementSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
         (repeated for 2000 times)
      
      
         (visit to nodes)
         at Microsoft.CodeAnalysis.CSharp.Formatting.TriviaRewriter.Visit(SyntaxNode node)
         at Microsoft.CodeAnalysis.CSharp.Formatting.TriviaRewriter.Transform()
         at Microsoft.CodeAnalysis.CSharp.Formatting.FormattingResult.Rewriter(Dictionary`2 changeMap, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Formatting.AbstractFormattingResult.CreateFormattedRoot(CancellationToken cancellationToken)
         at Roslyn.Utilities.CancellableLazy`1.GetValue(CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Formatting.AbstractFormattingResult.GetFormattedRoot(CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Formatting.Formatter.Format(SyntaxNode node, IEnumerable`1 spans, SolutionServices services, SyntaxFormattingOptions options, IEnumerable`1 rules, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Formatting.Formatter.FormatAsync(Document document, IEnumerable`1 spans, SyntaxFormattingOptions options, IEnumerable`1 rules, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Formatting.Formatter.FormatAsync(Document document, IEnumerable`1 spans, OptionSet options, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Tools.Formatters.WhitespaceFormatter.GetFormattedDocument(Document document, OptionSet optionSet, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Tools.Formatters.WhitespaceFormatter.FormatFileAsync(Document document, SourceText sourceText, OptionSet optionSet, AnalyzerConfigOptions analyzerConfigOptions, FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Tools.Formatters.DocumentFormatter.GetFormattedSourceTextAsync(Document document, OptionSet optionSet, AnalyzerConfigOptions analyzerConfigOptions, FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Tools.Formatters.DocumentFormatter.<>c__DisplayClass8_1.<<FormatFiles>b__0>d.MoveNext()
      --- End of stack trace from previous location ---
         at Microsoft.CodeAnalysis.Tools.Formatters.DocumentFormatter.ApplyFileChangesAsync(Solution solution, ImmutableArray`1 formattedDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Tools.Formatters.DocumentFormatter.FormatAsync(Workspace workspace, Solution solution, ImmutableArray`1 formattableDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Tools.CodeFormatter.RunCodeFormattersAsync(Workspace workspace, Solution solution, ImmutableArray`1 formattableDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Tools.CodeFormatter.FormatWorkspaceAsync(FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken, String binaryLogPath)
         at Microsoft.CodeAnalysis.Tools.FormatCommandCommon.FormatAsync(FormatOptions formatOptions, ILogger`1 logger, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Tools.Commands.RootFormatCommand.FormatCommandDefaultHandler.InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
         at System.CommandLine.Invocation.InvocationPipeline.InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
      
    • Stack overflow.
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.Create(Microsoft.CodeAnalysis.CSharp.BoundNode)
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.CreateBoundIfStatementOperation(Microsoft.CodeAnalysis.CSharp.BoundIfStatement)
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.Create(Microsoft.CodeAnalysis.CSharp.BoundNode)
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.CreateBoundIfStatementOperation(Microsoft.CodeAnalysis.CSharp.BoundIfStatement)
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.Create(Microsoft.CodeAnalysis.CSharp.BoundNode)
         (repeated)
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.CreateBoundIfStatementOperation(Microsoft.CodeAnalysis.CSharp.BoundIfStatement)
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.Create(Microsoft.CodeAnalysis.CSharp.BoundNode)
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.CreateFromArray[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.Immutable.ImmutableArray`1<System.__Canon>)
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.CreateBoundBlockOperation(Microsoft.CodeAnalysis.CSharp.BoundBlock)
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.Create(Microsoft.CodeAnalysis.CSharp.BoundNode)
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.CreateMethodBodyOperation(Microsoft.CodeAnalysis.CSharp.BoundNonConstructorMethodBody)
         at Microsoft.CodeAnalysis.Operations.CSharpOperationFactory.Create(Microsoft.CodeAnalysis.CSharp.BoundNode)
         at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.GetRootOperation()
         at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.GetOperationWorker(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode, System.Threading.CancellationToken)
         at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.GetOperationWorker(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode, System.Threading.CancellationToken)
         at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetOperationCore(Microsoft.CodeAnalysis.SyntaxNode, System.Threading.CancellationToken)
         at Microsoft.CodeAnalysis.SemanticModel.GetOperation(Microsoft.CodeAnalysis.SyntaxNode, System.Threading.CancellationToken)
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver`1[[Microsoft.CodeAnalysis.CSharp.SyntaxKind, Microsoft.CodeAnalysis.CSharp, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].GetOperationBlocksToAnalyze(System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.SyntaxNode>, Microsoft.CodeAnalysis.SemanticModel, System.Threading.CancellationToken)
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver`1+<>c__DisplayClass11_0[[Microsoft.CodeAnalysis.CSharp.SyntaxKind, Microsoft.CodeAnalysis.CSharp, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].<ExecuteDeclaringReferenceActions>g__executeExecutableCodeActions|2()
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver`1[[Microsoft.CodeAnalysis.CSharp.SyntaxKind, Microsoft.CodeAnalysis.CSharp, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].ExecuteDeclaringReferenceActions(Microsoft.CodeAnalysis.SyntaxReference, Microsoft.CodeAnalysis.Diagnostics.SymbolDeclaredCompilationEvent, Microsoft.CodeAnalysis.Diagnostics.AnalysisScope, GroupedAnalyzerActions<Microsoft.CodeAnalysis.CSharp.SyntaxKind>, GroupedAnalyzerActions<Microsoft.CodeAnalysis.CSharp.SyntaxKind>, Boolean, Boolean, Boolean, Boolean, Boolean, System.Threading.CancellationToken)
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver`1[[Microsoft.CodeAnalysis.CSharp.SyntaxKind, Microsoft.CodeAnalysis.CSharp, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].ExecuteDeclaringReferenceActions(Microsoft.CodeAnalysis.Diagnostics.SymbolDeclaredCompilationEvent, Microsoft.CodeAnalysis.Diagnostics.AnalysisScope, Boolean, IGroupedAnalyzerActions, System.Threading.CancellationToken)
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<TryProcessSymbolDeclaredAsync>d__137.MoveNext()
         at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<TryProcessSymbolDeclaredAsync>d__137, Microsoft.CodeAnalysis, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]](<TryProcessSymbolDeclaredAsync>d__137 ByRef)
         at System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Start[[Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<TryProcessSymbolDeclaredAsync>d__137, Microsoft.CodeAnalysis, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]](<TryProcessSymbolDeclaredAsync>d__137 ByRef)
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.TryProcessSymbolDeclaredAsync(Microsoft.CodeAnalysis.Diagnostics.SymbolDeclaredCompilationEvent, Microsoft.CodeAnalysis.Diagnostics.AnalysisScope, System.Threading.CancellationToken)   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<TryProcessEventCoreAsync>d__136.MoveNext()
         at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<TryProcessEventCoreAsync>d__136, Microsoft.CodeAnalysis, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]](<TryProcessEventCoreAsync>d__136 ByRef)
         at System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Start[[Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<TryProcessEventCoreAsync>d__136, Microsoft.CodeAnalysis, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]](<TryProcessEventCoreAsync>d__136 ByRef)
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.TryProcessEventCoreAsync(Microsoft.CodeAnalysis.Diagnostics.CompilationEvent, Microsoft.CodeAnalysis.Diagnostics.AnalysisScope, System.Threading.CancellationToken)
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<ProcessEventAsync>d__134.MoveNext()
         at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<ProcessEventAsync>d__134, Microsoft.CodeAnalysis, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]](<ProcessEventAsync>d__134 ByRef)
         at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<ProcessEventAsync>d__134, Microsoft.CodeAnalysis, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]](<ProcessEventAsync>d__134 ByRef)
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.ProcessEventAsync(Microsoft.CodeAnalysis.Diagnostics.CompilationEvent, Microsoft.CodeAnalysis.Diagnostics.AnalysisScope, System.Threading.CancellationToken)
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<ProcessCompilationEventsCoreAsync>d__133.MoveNext()
         at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<ProcessCompilationEventsCoreAsync>d__133, Microsoft.CodeAnalysis, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].ExecutionContextCallback(System.Object)
         at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
         at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<ProcessCompilationEventsCoreAsync>d__133, Microsoft.CodeAnalysis, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].MoveNext(System.Threading.Thread)
         at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver+<ProcessCompilationEventsCoreAsync>d__133, Microsoft.CodeAnalysis, Version=4.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].ExecuteFromThreadPool(System.Threading.Thread)
         at System.Threading.ThreadPoolWorkQueue.Dispatch()
         at System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()
      

Linux survives under 1500 else-ifs, but eventually it will run out of stack space when nesting more of them

Sample: notsosmart.zip

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Sep 10, 2024
@cston
Copy link
Member

cston commented Sep 10, 2024

See also #72393.

@CyrusNajmabadi
Copy link
Member

Wat. Why is the syntax formatter hitting the compiler binding code?

@CyrusNajmabadi
Copy link
Member

Note, unless the core syntax rewriter is updated to handle this, we are unlikely to do anything about this on the ide side. Using those rewriters is a core part of formatting.

@sgkoishi
Copy link
Author

Wat. Why is the syntax formatter hitting the compiler binding code?

Sorry for the confusion, they are not from the same error, I've updated the stack trace. Switching to Linux for a different default stack size seems to work in my case, hopefully it will be supported via else if

@CyrusNajmabadi
Copy link
Member

Ah ok. That makes more sense :-)

@jaredpar
Copy link
Member

Closing as dupe

@jaredpar jaredpar closed this as not planned Won't fix, can't repro, duplicate, stale Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

4 participants