cezarypiatek / MappingGenerator

:arrows_counterclockwise: "AutoMapper" like, Roslyn based, code fix provider that allows to generate mapping code in design time.
https://marketplace.visualstudio.com/items?itemName=54748ff9-45fc-43c2-8ec5-cf7912bc3b84.mappinggenerator
MIT License
1.03k stars 120 forks source link

Error when opening quick actions menu #57

Closed cocowalla closed 5 years ago

cocowalla commented 5 years ago

I've just installed this extension, but get an error when opening the Visual Studio "Quick Actions" menu, before I've even hit "Generate mapping code". Stack trace:

System.NullReferenceException : Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxReplacer.Replacer`1.<>c.<.ctor>b__10_0(SyntaxNode n)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
   at System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1 other)
   at System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection,IEqualityComparer`1 comparer)
   at System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection)
   at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxReplacer.Replacer`1..ctor(IEnumerable`1 nodes,Func`3 computeReplacementNode,IEnumerable`1 tokens,Func`3 computeReplacementToken,IEnumerable`1 trivia,Func`3 computeReplacementTrivia)
   at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxReplacer.Replace[TNode](SyntaxNode root,IEnumerable`1 nodes,Func`3 computeReplacementNode,IEnumerable`1 tokens,Func`3 computeReplacementToken,IEnumerable`1 trivia,Func`3 computeReplacementTrivia)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode.ReplaceCore[TNode](IEnumerable`1 nodes,Func`3 computeReplacementNode,IEnumerable`1 tokens,Func`3 computeReplacementToken,IEnumerable`1 trivia,Func`3 computeReplacementTrivia)
   at Microsoft.CodeAnalysis.SyntaxNodeExtensions.ReplaceNode[TRoot](TRoot root,SyntaxNode oldNode,SyntaxNode newNode)
   at async MappingGenerator.SyntaxGeneratorExtensions.ReplaceNodes(<Unknown Parameters>)
   at async MappingGenerator.MappingGeneratorRefactoring.GenerateMappingMethodBody(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)

I'm on Visual Studio 2017, version 15.9.4.

Before opening the Quick Actions menu, I've typed this stub:

public static class Mapper
{
    public static MyDTO Map(MyEntity entity)
}
cezarypiatek commented 5 years ago

Hi

Could you provide your code of MyDTO and MyEntity classes?

cocowalla commented 5 years ago

Sorry, I had meant to mention, the issue is present even with the most trivial classes:

public class Test
{
    public static class Mapper
    {
        public static Dto Map(Entity entity)
    }
}

public class Entity
{
    public Guid? Id { get; set; }
}

public class Dto
{
    public Guid? Id { get; set; }
}

But! I just noticed that the issue only presents if I have a stub without parentheses. So, this causes the error:

public static Dto Map(Entity entity)

Whereas it does work if I add the brackets first:

public static Dto Map(Entity entity)
{

}
cezarypiatek commented 5 years ago

@cocowalla Here's a version with fixed issue. Would you mind to test it and tell me if everything is working as expected? https://ci.appveyor.com/api/buildjobs/4o5lo4e0ga2a7uy1/artifacts/MappingGenerator%2FMappingGenerator%2FMappingGenerator.Vsix%2Fbin%2FRelease%2FMappingGenerator.vsix

cocowalla commented 5 years ago

@cezarypiatek excellent, it works as expected 😄

cezarypiatek commented 5 years ago

Thanks for testing.