dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.05k stars 4.03k forks source link

ArgumentOutOfRangeException in SyntaxNode.FindNode #75014

Open KirillOsenkov opened 2 months ago

KirillOsenkov commented 2 months ago

I'm using the bits from 17.11.0.

Various analyzers have an ArgumentOutOfRangeException with the same pattern here:

https://github.com/dotnet/roslyn/blob/139f8454c7a9aca7807fe98bb760c3705559b902/src/Compilers/Core/Portable/Syntax/SyntaxNode.cs#L962-L967

FullSpan is contained inside span: image

Sample stack:

Microsoft.CodeAnalysis  SyntaxNode.FindNode Line 966
Microsoft.CodeAnalysis.Workspaces   SyntaxNodeExtensions.FindNode Line 786
Microsoft.CodeAnalysis.Workspaces   AnalysisContextExtensions.GetAnalysisRoot Line 217
Microsoft.CodeAnalysis.Features SimplifyTypeNamesDiagnosticAnalyzerBase`2.AnalyzerImpl.AnalyzeCodeBlock Line 247

context.CodeBlock is a subspan of context.FilterSpan here:

https://github.com/dotnet/roslyn/blob/139f8454c7a9aca7807fe98bb760c3705559b902/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/AnalysisContextExtensions.cs#L216-L217

image

@mavasani this is pretty noisy. Just from a cursory search:

In my case the repro was to hover over here: https://github.com/KirillOsenkov/MSBuildStructuredLog/blob/5e5f5f68741eb550a27c9cf1084a02dcaf914619/src/StructuredLogger/Strings/Strings.cs#L620

KirillOsenkov commented 2 months ago

This wasn't happening in 17.8.3.

KirillOsenkov commented 2 months ago

More stack for completeness since I have it:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: span
   at SyntaxNode Microsoft.CodeAnalysis.SyntaxNode.FindNode(TextSpan span, bool findInsideTrivia, bool getInnermostNodeForTie) in C:/Roslyn/src/Compilers/Core/Portable/Syntax/SyntaxNode.cs:line 964
   at void Microsoft.CodeAnalysis.SimplifyTypeNames.SimplifyTypeNamesDiagnosticAnalyzerBase<TLanguageKindEnum, TSimplifierOptions>+AnalyzerImpl.AnalyzeCodeBlock(CodeBlockAnalysisContext context) in C:/Roslyn/src/Analyzers/Core/Analyzers/SimplifyTypeNames/SimplifyTypeNamesDiagnosticAnalyzerBase.cs:line 247
   at void Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteBlockActions<TBlockAction>(PooledHashSet<TBlockAction> blockActions, SyntaxNode declaredNode, ISymbol declaredSymbol, DiagnosticAnalyzer analyzer, SemanticModel semanticModel, ImmutableArray<IOperation> operationBlocks, Action<Diagnostic> addDiagnostic, Func<Diagnostic, CancellationToken, bool> isSupportedDiagnostic, TextSpan? filterSpan, bool isGeneratedCode, CancellationToken cancellationToken)+((Action<CodeBlockAnalysisContext> action, CodeBlockAnalysisContext context) data) => { } in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs:line 901
   at void Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock<TArg>(DiagnosticAnalyzer analyzer, Action<TArg> analyze, TArg argument, AnalysisContextInfo? info, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs:line 1185
   at void Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows<TArg>(DiagnosticAnalyzer analyzer, Action<TArg> analyze, TArg argument, AnalysisContextInfo? contextInfo, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs:line 1169
   at void Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteBlockActions<TBlockAction>(PooledHashSet<TBlockAction> blockActions, SyntaxNode declaredNode, ISymbol declaredSymbol, DiagnosticAnalyzer analyzer, SemanticModel semanticModel, ImmutableArray<IOperation> operationBlocks, Action<Diagnostic> addDiagnostic, Func<Diagnostic, CancellationToken, bool> isSupportedDiagnostic, TextSpan? filterSpan, bool isGeneratedCode, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs:line 899
   at void Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteBlockActionsCore<TBlockStartAction, TBlockAction, TNodeAction, TNode, TLanguageKindEnum>(IEnumerable<TBlockStartAction> startActions, IEnumerable<TBlockAction> actions, IEnumerable<TBlockAction> endActions, DiagnosticAnalyzer analyzer, SyntaxNode declaredNode, ISymbol declaredSymbol, ImmutableArray<TNode> executableBlocks, Func<ImmutableArray<TNode>, IEnumerable<TNode>> getNodesToAnalyze, SemanticModel semanticModel, Func<SyntaxNode, TLanguageKindEnum> getKind, TextSpan? filterSpan, bool isGeneratedCode, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs:line 869
   at void Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver<TLanguageKindEnum>+<>c__DisplayClass11_0.<ExecuteDeclaringReferenceActions>g__executeCodeBlockActions|7(?)+executeCodeBlockActions(ImmutableArray<SyntaxNode> executableCodeBlocks, IEnumerable<ExecutableCodeBlockAnalyzerActions> codeBlockActions) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs:line 2802
   at void Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver<TLanguageKindEnum>+<>c__DisplayClass11_0.<ExecuteDeclaringReferenceActions>g__executeExecutableCodeActions|2(?)+executeExecutableCodeActions() in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs:line 2708
   at void Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver<TLanguageKindEnum>.ExecuteDeclaringReferenceActions(SyntaxReference decl, SymbolDeclaredCompilationEvent symbolEvent, AnalysisScope analysisScope, GroupedAnalyzerActions coreActions, GroupedAnalyzerActions additionalPerSymbolActions, bool shouldExecuteSyntaxNodeActions, bool shouldExecuteOperationActions, bool shouldExecuteCodeBlockActions, bool shouldExecuteOperationBlockActions, bool isInGeneratedCode, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs:line 2598
   at void Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver<TLanguageKindEnum>.ExecuteDeclaringReferenceActions(SymbolDeclaredCompilationEvent symbolEvent, AnalysisScope analysisScope, bool isGeneratedCodeSymbol, IGroupedAnalyzerActions additionalPerSymbolActions, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs:line 2529
   at async ValueTask<EventProcessedState> Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.TryProcessSymbolDeclaredAsync(SymbolDeclaredCompilationEvent symbolEvent, AnalysisScope analysisScope, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs:line 1776
   at void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<TResult>.Start<TStateMachine>(ref TStateMachine stateMachine)
   at ValueTask<EventProcessedState> Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.TryProcessSymbolDeclaredAsync(SymbolDeclaredCompilationEvent symbolEvent, AnalysisScope analysisScope, CancellationToken cancellationToken)
   at async ValueTask<EventProcessedState> Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.TryProcessEventCoreAsync(CompilationEvent compilationEvent, AnalysisScope analysisScope, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs:line 1725
   at void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<TResult>.Start<TStateMachine>(ref TStateMachine stateMachine)
   at ValueTask<EventProcessedState> Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.TryProcessEventCoreAsync(CompilationEvent compilationEvent, AnalysisScope analysisScope, CancellationToken cancellationToken)
   at async Task Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.ProcessEventAsync(CompilationEvent e, AnalysisScope analysisScope, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs:line 1636
   at void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<TStateMachine>(ref TStateMachine stateMachine)
   at Task Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.ProcessEventAsync(CompilationEvent e, AnalysisScope analysisScope, CancellationToken cancellationToken)
   at async Task<CompilationCompletedEvent> Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.ProcessCompilationEventsCoreAsync(AnalysisScope analysisScope, bool prePopulatedEventQueue, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs:line 1619
   at void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<TResult>.Start<TStateMachine>(ref TStateMachine stateMachine)
   at Task<CompilationCompletedEvent> Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.ProcessCompilationEventsCoreAsync(AnalysisScope analysisScope, bool prePopulatedEventQueue, CancellationToken cancellationToken)
   at async Task Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.ProcessCompilationEventsAsync(AnalysisScope analysisScope, bool prePopulatedEventQueue, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs:line 1554
   at void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<TStateMachine>(ref TStateMachine stateMachine)
   at Task Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.ProcessCompilationEventsAsync(AnalysisScope analysisScope, bool prePopulatedEventQueue, CancellationToken cancellationToken)
   at async Task Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.ExecutePrimaryAnalysisTaskAsync(AnalysisScope analysisScope, bool usingPrePopulatedEventQueue, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs:line 698
   at void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<TStateMachine>(ref TStateMachine stateMachine)
   at Task Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.ExecutePrimaryAnalysisTaskAsync(AnalysisScope analysisScope, bool usingPrePopulatedEventQueue, CancellationToken cancellationToken)
   at async Task Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.AttachQueueAndProcessAllEventsAsync(AsyncQueue<CompilationEvent> eventQueue, AnalysisScope analysisScope, CancellationToken cancellationToken) in C:/Roslyn/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs:line 642
   at void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<TStateMachine>(ref TStateMachine stateMachine)
   at Task Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver.AttachQueueAndProcessAllEventsAsync(AsyncQueue<CompilationEvent> eventQueue, AnalysisScope analysisScope, CancellationToken cancellationToken)
   at async Task Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.ComputeAnalyzerDiagnosticsAsync(AnalysisScope analysisScope, CancellationToken cancellationToken)+attachQueueAndProcessAllEventsAsync(?)
   at void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<TStateMachine>(ref TStateMachine stateMachine)
   at Task Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.ComputeAnalyzerDiagnosticsAsync(AnalysisScope analysisScope, CancellationToken cancellationToken)+attachQueueAndProcessAllEventsAsync(ArrayBuilder<ValueTuple<AnalysisScope, ImmutableArray<CompilationEvent>>> builder, AnalyzerDriver driver, CancellationToken cancellationToken)
   at async Task Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.ComputeAnalyzerDiagnosticsAsync(AnalysisScope analysisScope, CancellationToken cancellationToken)
KirillOsenkov commented 2 months ago

This seems to do the trick:

image