dotnet / dotnet/roslyn

InvalidOperationException in DocumentHighlighting.AbstractDocumentHighlightsService.AddLocationSpanAsync

Open
#75,337 0 comments 0 reactions 1 assignee Claimed by @CyrusNajmabadi View on GitHub
Area-IDE
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

1. git clone https://github.com/dfederm/ReferenceTrimmer
2. git checkout 16c84c60539adfb9ee1343684eabd06c22bb1ed9
3. open ReferenceTrimmer.sln
4. open CollectDeclaredReferencesTask.cs line 205
5. invoke Light bulb on the word `nugetFramework` so that the preview appears

![image](https://github.com/user-attachments/assets/ac6bc2a9-ab23-4d5e-af58-2c0afb98c86d)

This is an interesting bug. This line blows up because the tree is not in the solution:
https://github.com/dotnet/roslyn/blob/1cd3100fdf6d223b1d4fde09e6b096ac14b13150/src/Features/Core/Portable/DocumentHighlighting/AbstractDocumentHighlightsService.cs#L308

This is because the solution is from the LightBulb preview workspace, not from the primary workspace. In that preview workspace, the tree was modified to replace var with the type name, so the tree it finds for the document id is different.

I notice a similar place here which also doesn't seem to account for the LightBulb preview workspace:
https://github.com/dotnet/roslyn/blob/1cd3100fdf6d223b1d4fde09e6b096ac14b13150/src/Features/Core/Portable/DocumentHighlighting/AbstractDocumentHighlightsService.cs#L238-L245

```
System.InvalidOperationException: Operation is not valid due to the current state of the object.
at Document Microsoft.CodeAnalysis.Shared.Extensions.ISolutionExtensions.GetRequiredDocument(Solution solution, SyntaxTree syntaxTree) in C:/Roslyn/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Extensions/ISolutionExtensions.cs:line 36
at async Task Microsoft.CodeAnalysis.DocumentHighlighting.AbstractDocumentHighlightsService.GetLocationSpanAsync(Solution solution, Location location, CancellationToken cancellationToken) in C:/Roslyn/src/Features/Core/Portable/DocumentHighlighting/AbstractDocumentHighlightsService.cs:line 306
at async Task Microsoft.CodeAnalysis.DocumentHighlighting.AbstractDocumentHighlightsService.AddLocationSpanAsync(Location location, Solution solution, HashSet spanSet, MultiDictionary tagList, HighlightSpanKind kind, CancellationToken cancellationToken) in C:/Roslyn/src/Features/Core/Portable/DocumentHighlighting/AbstractDocumentHighlightsService.cs:line 291
at async Task> Microsoft.CodeAnalysis.DocumentHighlighting.AbstractDocumentHighlightsService.CreateSpansAsync(Solution solution, ISymbol symbol, IEnumerable references, ArrayBuilder additionalReferences, IImmutableSet documentToSearch, CancellationToken cancellationToken) in C:/Roslyn/src/Features/Core/Portable/DocumentHighlighting/AbstractDocumentHighlightsService.cs:line 265
at async Task> Microsoft.CodeAnalysis.DocumentHighlighting.AbstractDocumentHighlightsService.FilterAndCreateSpansAsync(ImmutableArray references, Document startingDocument, IImmutableSet documentsToSearch, ISymbol symbol, FindReferencesSearchOptions options, CancellationToken cancellationToken) in C:/Roslyn/src/Features/Core/Portable/DocumentHighlighting/AbstractDocumentHighlightsService.cs:line 190
at async Task> Microsoft.CodeAnalysis.DocumentHighlighting.AbstractDocumentHighlightsService.GetTagsForReferencedSymbolAsync(ISymbol symbol, Document document, IImmutableSet documentsToSearch, CancellationToken cancellationToken) in C:/Roslyn/src/Features/Core/Portable/DocumentHighlighting/AbstractDocumentHighlightsService.cs:line 129
at async Task> Microsoft.CodeAnalysis.DocumentHighlighting.AbstractDocumentHighlightsService.GetDocumentHighlightsInCurrentProcessAsync(Document document, int position, IImmutableSet documentsToSearch, HighlightingOptions options, CancellationToken cancellationToken) in C:/Roslyn/src/Features/Core/Portable/DocumentHighlighting/AbstractDocumentHighlightsService.cs:line 81
at async Task> Microsoft.CodeAnalysis.DocumentHighlighting.AbstractDocumentHighlightsService.GetDocumentHighlightsAsync(Document document, int position, IImmutableSet documentsToSearch, HighlightingOptions options, CancellationToken cancellationToken) in C:/Roslyn/src/Features/Core/Portable/DocumentHighlighting/AbstractDocumentHighlightsService.cs:line 59
at async Task Microsoft.CodeAnalysis.Editor.ReferenceHighlighting.ReferenceHighlightingViewTaggerProvider.ProduceTagsAsync(TaggerContext context, SnapshotPoint position, Document document, HighlightingOptions options, CancellationToken cancellationToken) in C:/Roslyn/src/EditorFeatures/Core/ReferenceHighlighting/ReferenceHighlightingViewTaggerProvider.cs:line 159
at async ValueTask Microsoft.CodeAnalysis.Editor.Tagging.AbstractAsynchronousTaggerProvider+TagSource.ProduceTagsAsync(TaggerContext context, CancellationToken cancellationToken) in C:/Roslyn/src/EditorFeatures/Core/Tagging/AbstractAsynchronousTaggerProvider.TagSource_ProduceTags.cs:line 580
at async Task>> Microsoft.CodeAnalysis.Editor.Tagging.AbstractAsynchronousTaggerProvider+TagSource.RecomputeTagsAsync(bool highPriority, bool frozenPartialSemantics, bool calledFromJtfRun, CancellationToken cancellationToken)+(?) => { } in C:/Roslyn/src/EditorFeatures/Core/Tagging/AbstractAsynchronousTaggerProvider.TagSource_ProduceTags.cs:line 349
at async Task<(ImmutableDictionary> oldTagTrees, ImmutableDictionary> newTagTrees, TResult)> Microsoft.CodeAnalysis.Editor.Tagging.AbstractAsynchronousTaggerProvider+TagSource.CompareAndSwapTagTreesAsync(Func<(ITextBuffer newTagTrees, TagSpanIntervalTree result)> callback, TArgs args, CancellationToken cancellationToken) in C:/Roslyn/src/EditorFeatures/Core/Tagging/AbstractAsynchronousTaggerProvider.TagSource_ProduceTags.cs:line 252
at async Task>> Microsoft.CodeAnalysis.Editor.Tagging.AbstractAsynchronousTaggerProvider+TagSource.RecomputeTagsAsync(bool highPriority, bool frozenPartialSemantics, bool calledFromJtfRun, CancellationToken cancellationToken) in C:/Roslyn/src/EditorFeatures/Core/Tagging/AbstractAsynchronousTaggerProvider.TagSource_ProduceTags.cs:line 341
at async ValueTask Microsoft.CodeAnalysis.Editor.Tagging.AbstractAsynchronousTaggerProvider+TagSource.ProcessEventChangeAsync(ImmutableSegmentedList changes, CancellationToken cancellationToken)
at T Roslyn.Utilities.ValueTaskExtensions.VerifyCompleted(ValueTask task, string message) in C:/Roslyn/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/ValueTaskExtensions.cs:line 24
at async ValueTask Roslyn.Utilities.AsyncBatchingWorkQueue.ProcessNextBatchAsync() in C:/Roslyn/src/Workspaces/Core/Portable/Shared/Utilities/AsyncBatchingWorkQueue`2.cs:line 264
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.