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

InvalidOperationException on InitializeFieldFromConstructor #69

Open
verysimplenick opened this issue Jun 25, 2017 · 6 comments
Open

InvalidOperationException on InitializeFieldFromConstructor #69

verysimplenick opened this issue Jun 25, 2017 · 6 comments

Comments

@verysimplenick
Copy link

Hi. In VS 2017.2 often see exception in InitializeFieldFromConstructor while edit code in .net core projects (1.1).

System.InvalidOperationException : Sequence contains more than one element
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at HotCommands.InitializeFieldFromConstructor.GetConstructors(VariableDeclaratorSyntax fieldVariable)
   at async HotCommands.InitializeFieldFromConstructor.ComputeRefactoringsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeRefactorings.CodeRefactoringService.GetRefactoringFromProviderAsync(<Unknown Parameters>)
@verysimplenick verysimplenick changed the title InvalidOperationException on InitializeFieldFromConstructor InvalidOperationException on InitializeFieldFromConstructor Jun 25, 2017
verysimplenick pushed a commit to verysimplenick/HotCommands that referenced this issue Jun 25, 2017
change from SingleOrDefault to FirstOrDefault
@verysimplenick
Copy link
Author

I think I get error while have 2 classes on one file in .net core project. Check this please, I think need something condition in FirstOrDefault to search class with this field in file.

@justcla
Copy link
Owner

justcla commented Jun 26, 2017

Is this different to the issue fixed by pull request #67 ?
(I haven't uploaded the new built VSIX since Loren made the fix.)

Or is this a separate issue?

@verysimplenick
Copy link
Author

I think separate (NullReferenceException in #67 vs InvalidOperationException #69)

@tamlin-mike
Copy link

@justcla This is a different issue from #67.

This problem is (obviously - from the stack trace) the SingleOrDefault() from
var classDeclaration = fieldVariable.Ancestors().OfType<ClassDeclarationSyntax>().SingleOrDefault();

meaning the field variable has multiple ancestors. This happens with nested classes.

SingleOrDefault -> FirstOrDefault would indeed prevent the problem, and it seems the order in Ancestors() is innermost->outermost, so this seems to be the correct fix.

I'm quite sure fix can be applied, and issue closed.

@justcla
Copy link
Owner

justcla commented Oct 15, 2017

I've uploaded a new VSIX with the fix suggested by Mike. v1.1.7
@tamlin-mike @verysimplenick Can you please confirm if this fixes the issue?

@tamlin-mike
Copy link

As I'm using my home-built binary, I can only confirm that it works (as expected).

Additionally, it was @verysimplenick providing the patch (Jun 25). I just commented (and documented a bit) since I was now bitten by the bug too.

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

No branches or pull requests

3 participants