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

NullReferenceException after upgrading from 1.2.1 -> 2.3.1 #22710

Closed
JohanLarsson opened this issue Oct 16, 2017 · 5 comments
Closed

NullReferenceException after upgrading from 1.2.1 -> 2.3.1 #22710

JohanLarsson opened this issue Oct 16, 2017 · 5 comments
Labels
Milestone

Comments

@JohanLarsson
Copy link

JohanLarsson commented Oct 16, 2017

Version 2.3.1:

Steps to Reproduce:

  1. Clone https://github.com/DotNetAnalyzers/PropertyChangedAnalyzers/tree/8d5fc945d8a879214f76e38e8b53111397547505
  2. Run tests

Expected Behavior:

All tests passed when using 1.2.1

Actual Behavior:

System.NullReferenceException : Object reference not set to an instance of an object.
at Microsoft.CodeAnalysis.CodeFixes.BatchFixAllProvider.<TryAddDocumentMergeChangesAsync>d__19.MoveNext()

Debugged it and oldSolution.Workspace.Services.GetService<IDocumentTextDifferencingService>() returns null now. http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces/CodeFixes/FixAllOccurrences/BatchFixAllProvider.cs,320

Not unlikely that something is wrong in the test fixture but something changed between versions.

@Pilchie
Copy link
Member

Pilchie commented Oct 16, 2017

How is your Workspace constructed in tests?

@JeremyTCD
Copy link

I have the same issue with 2.4.0. I'm using the default AdhocWorkspace constructor to instantiate a Workspace instance to test batch fixes for my diagnostic analyzers:

public AdhocWorkspace()
: this(Host.Mef.MefHostServices.DefaultHost)
{
}

BatchFixAllProvider attempts to retrieve an IDocumentTextDifferencingService from the workspace but is unable to find one. As mentioned by @JohanLarsson, the following GetService invocation returns null:

var differenceService = oldSolution.Workspace.Services.GetService<IDocumentTextDifferencingService>();

Changing the line to the following allows tests to run normally:

var differenceService = oldSolution.Workspace.Services.GetService<IDocumentTextDifferencingService>() ?? new DefaultDocumentTextDifferencingService();

I'm not completely certain, but it appears that if a workspace has no IDocumentTextDifferencingService then defaulting to DefaultDocumentTextDifferencingService is acceptable.

@jinujoseph jinujoseph added this to the 15.7 milestone Oct 19, 2017
@jinujoseph jinujoseph modified the milestones: 15.7, Unknown Nov 1, 2017
@ThyPillow
Copy link

I have the same issue with 2.6.1.
I'm using a MSBuildWorkspace to apply several identified code fixes on all projects of a solution, this relates to #2020 as this is exactly what I am doing.

As long as there's only one Diagnostic provided, the code fix is being applied with no error, but as soon as the BatchFixAllProvider is trying to merge several of them, the NullReferenceException is thrown.

It seems the LinkedFileDiffMergingSession already has the behavior described by @JeremyTCD

var textDifferencingService = _oldSolution.Workspace.Services.GetService<IDocumentTextDifferencingService>() ?? new DefaultDocumentTextDifferencingService();

If you are okay with this proposed behavior I can submit the PR to fix it.

@ThyPillow
Copy link

This has been fixed by #23492

@CyrusNajmabadi
Copy link
Member

This was fixed. CLosing out.

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

No branches or pull requests

6 participants