omnisharp-roslyn: Omnisharp Server crashing with a NullReferenceException

Issue Description

Omnisharp keeps throwing a NullReferenceException only when opening a c# file in our Client Project. Our workspace is a blazorwasm application split into multiple Projects Client, Server, Shared and Core.

Steps to Reproduce

As of right now I haven’t found any.

Expected Behavior

O# Server not crashing.

Actual Behavior

O# Server crashing due to NullReferenceException.

Logs

OmniSharp log


Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at OmniSharp.Helpers.LocationExtensions.<>c__DisplayClass0_0.<GetQuickFix>b__3(Document d) in D:\a\1\s\src\OmniSharp.Roslyn.CSharp\Helpers\LocationExtensions.cs:line 45
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at OmniSharp.Helpers.LocationExtensions.<GetQuickFix>g__GetSourceText|0_0(Location location, IEnumerable`1 documents, Boolean hasMappedPath) in D:\a\1\s\src\OmniSharp.Roslyn.CSharp\Helpers\LocationExtensions.cs:line 45
   at OmniSharp.Helpers.LocationExtensions.GetQuickFix(Location location, OmniSharpWorkspace workspace) in D:\a\1\s\src\OmniSharp.Roslyn.CSharp\Helpers\LocationExtensions.cs:line 19
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.<DistinctIterator>d__64`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
   at OmniSharp.Protocol.Packet.ToString() in D:\a\1\s\src\OmniSharp.Host\Protocol\Packet.cs:line 22
   at System.IO.TextWriter.WriteLine(Object value)
   at System.IO.TextWriter.SyncTextWriter.WriteLine(Object value)
   at OmniSharp.Services.SharedTextWriter.ProcessWriteQueue() in D:\a\1\s\src\OmniSharp.Host\Services\SharedTextWriter.cs:line 48
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Environment information

VSCode version: 1.55.0 C# Extension: 1.23.9

Dotnet Information .NET SDK (reflecting any global.json): Version: 6.0.100-preview.2.21155.3 Commit: 1a9103db2d

Runtime Environment: OS Name: Windows OS Version: 10.0.19042 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\6.0.100-preview.2.21155.3\

Host (useful for support): Version: 6.0.0-preview.2.21154.6 Commit: 3eaf1f316b

.NET SDKs installed: 6.0.100-preview.2.21155.3 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 6.0.0-preview.2.21154.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0-preview.2.21154.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0-preview.2.21154.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download

Visual Studio Code Extensions
Extension Author Version
csharp ms-dotnettools 1.23.9

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 18 (5 by maintainers)

Most upvoted comments

Another tempfix that will stop Omnisharp from crashing just after opening .cs files is to disable References Code Lens. "csharp.referencesCodeLens.enabled": false. This will not prevent crash when you try to seek the references manually however.

This issue is happening for .cs files that have types that are used inside .razor files. So when CodeLens tries to retrieve references, it will attempt to load documents for .razor files that are for some reason not present in the CurrentSolution.

When it’s calling this line https://github.com/OmniSharp/omnisharp-roslyn/blob/9c293cbce30d17875526220a718a8396d987d626/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs#L23

where lineSpan.Path is for example e:/BlazorApp/src/Client/Shared/MainLayout.razor

This code will respond with null which eventually leads to NRE.

https://github.com/OmniSharp/omnisharp-roslyn/blob/9c293cbce30d17875526220a718a8396d987d626/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs#L339-L344

.cs <-> .cs references work fine. Only .cs -> .razor is broken.

@mikechari not related. remove “omnisharp.path”:“latest” from your settings, as that opts you into the latest prerelease of OmniSharp which contains a regression

Adding a reference repository and explanation on how to replicate the issue found in README

https://github.com/jsheely/dotnet6-omni-bug1

This bug does not appear to happen in dotnet 5. It’s something specific to dotnet core 6. Including the latest preview 7 release