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

Omnisharp Server crashing with a NullReferenceException #2125

Closed
BeminAbe opened this issue Apr 1, 2021 · 18 comments · Fixed by #2233
Closed

Omnisharp Server crashing with a NullReferenceException #2125

BeminAbe opened this issue Apr 1, 2021 · 18 comments · Fixed by #2233
Labels

Comments

@BeminAbe
Copy link

BeminAbe commented Apr 1, 2021

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
@JoeRobich JoeRobich transferred this issue from dotnet/vscode-csharp Apr 1, 2021
@JoeRobich
Copy link
Member

@filipw Looks like this change might have caused a NRE e502657

@filipw
Copy link
Member

filipw commented Apr 1, 2021

It crashes on that line, but I'd say that crash can only happen if we have a null document, which should never happen.
So to me it seems like the null reference is a symptom, but not the root cause for the actual problem.

It would be good to have the repro project.

@filipw filipw added the bug label Apr 5, 2021
@filipw
Copy link
Member

filipw commented Apr 5, 2021

@BeminAbe are you able to provide a repro project for this?

@BeminAbe
Copy link
Author

BeminAbe commented Apr 5, 2021

As of right now I don't have a repro project. Are there any tools/settings I can use that may help me with making one?

@NTaylorMullen
Copy link
Contributor

Just ran into this by building/running local O#-vscode as well:
image

@NTaylorMullen
Copy link
Contributor

Was running in a Blazor web project if it helps

@BeminAbe
Copy link
Author

@NTaylorMullen Which version of dotnet were you running?
I had the issue when my team was running 6.0.100-preview.2.21155.3 but due to the shear amount of problems we encountered we had to switch back to Version 5.0.202 which doesn't seem to have this issue.

@NTaylorMullen
Copy link
Contributor

6.0.0-preview.2.21118.6

@Duckers
Copy link

Duckers commented Jun 27, 2021

Same problem in a vanilla ASP.NET Core MVC template, repro attached:
McvTest.zip

Open any C# file, and OmniSharp crashes with the same stack trace as others have got above

@TheAifam5
Copy link

TheAifam5 commented Jul 5, 2021

Me too. Tried that NRE patch but still crashes with the similar stacktrace.

I will update this post later to provide the full stacktrace.

@JosNun
Copy link

JosNun commented Jul 16, 2021

Not a 100% solution, but a temp workaround for me to at least temporarily prevent the crashing was to replace this line with
Projects = documents.Where(document => document?.Project?.Name != null).Select(document => document?.Project?.Name).ToArray(),

It's not a 100% solution, since it doesn't address the root of the issue, and an error is still logged in the console (though, it's gracefully handled), but the patch at least prevented it from crashing all the time and being generally unusable with the latest preview versions of .NET 6.

@Xeevis
Copy link

Xeevis commented Jul 28, 2021

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

var documents = workspace.GetDocuments(lineSpan.Path);

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

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

public IEnumerable<Document> GetDocuments(string filePath)
{
return CurrentSolution
.GetDocumentIdsWithFilePath(filePath)
.Select(id => CurrentSolution.GetDocument(id));
}

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

@jsheely
Copy link

jsheely commented Aug 27, 2021

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

@laxedo17
Copy link

laxedo17 commented Oct 9, 2021

happened to me yesterday. Everything was working perfectly fine from the very beginning -I started the .NET project in VS Code-. It happened when I deleted a couple of extensions, and reloaded the project, for whatever reason Omnisharp didnt like that. I could never fix it afterwards, had to load the .csproj file in Visual Studio 2022 Preview and continue to work from there. I like VS very much but I prefer Visual Studio Code.

@JoeRobich
Copy link
Member

@laxedo17 Since your issue could have a different root cause, could you please open a new issue and share all the requested logs?

@Igorgro
Copy link

Igorgro commented Oct 12, 2021

Having exactly th same issue. It happens at random moments for example when adding modifiers to class members or when moving or copy code lines.

Unhandled Exception: System.NullReferenceException:  Object reference not set to an instance of an object.
   at OmniSharp.Helpers.LocationExtensions.<>c.<GetQuickFix>b__0_2(Document document) in D:\a\1\s\src\OmniSharp.Roslyn.CSharp\Helpers\LocationExtensions.cs:line 40
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at OmniSharp.Helpers.LocationExtensions.GetQuickFix(Location location, OmniSharpWorkspace workspace) in D:\a\1\s\src\OmniSharp.Roslyn.CSharp\Helpers\LocationExtensions.cs:line 32
   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()

@mikechari
Copy link

mikechari commented Jan 24, 2022

Maybe related suddenly i am getting this one:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at OmniSharp.Helpers.DiagnosticExtensions.<>c.<DistinctDiagnosticLocationsByProject>b__2_0(DocumentDiagnostics x) in D:\a\1\s\src\OmniSharp.Roslyn.CSharp\Helpers\DiagnosticExtensions.cs:line 38
   at System.Linq.Enumerable.<SelectManyIterator>d__23`3.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Lookup`2.Create[TSource](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.GroupedEnumerable`3.GetEnumerator()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`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()

@filipw
Copy link
Member

filipw commented Jan 24, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.