Joshlo
Joshlo

Reputation: 794

Roslyn CodeFixProvider gives VS 2015 error

I have created a roslyn CodeAnalyzer and a CodeFixProvider.

The Analyzer runs fine, and creates the rule, but when I'm trying to open the popup showing the fix, I get a "One or more errors occurred" VS popup.

First time I ran it, it worked fine, but then I stopped debugging and after that it gave me that error, so I tried on another computer and again it worked fine the first time I debugged.

My Analyzer:

private static void Analyze(SyntaxNodeAnalysisContext context)
{
    var localDeclaration = (LocalDeclarationStatementSyntax)context.Node;

    foreach (var variable in localDeclaration.Declaration.Variables)
    {
        var initializer = variable.Initializer;
        if (initializer == null) return;
    }

    var node = context.Node;

    while (node.Kind() != SyntaxKind.MethodDeclaration)
    {
        node = node.Parent;
    }

    var method = (MethodDeclarationSyntax)node;

    if (method.AttributeLists.Any(x => x.Attributes.Any(y => y.Name is IdentifierNameSyntax && ((IdentifierNameSyntax)y.Name).Identifier.Text.ToLower().Contains("test"))))
    {
        context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation()));
    }
}

My CodeFixProvider

private async Task<Document> AddAssertionsAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken)
{
    var editor = await DocumentEditor.CreateAsync(document, cancellationToken);

    var assert = SyntaxFactory.IdentifierName("Assert");
    var areEqual = SyntaxFactory.IdentifierName("AreEqual");

    var memberAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, assert,
            areEqual);

    var firstArgument = SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(@"")));
    var secondArgument = SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(@"")));

    var argumentList = SyntaxFactory.SeparatedList<ArgumentSyntax>(
        new SyntaxNodeOrToken[] {
            firstArgument,
            SyntaxFactory.Token(SyntaxKind.CommaToken),
            secondArgument
        });

    var assertToken =
        SyntaxFactory.ExpressionStatement(
        SyntaxFactory.InvocationExpression(memberAccess,
        SyntaxFactory.ArgumentList(argumentList)));

    editor.InsertAfter(localDeclaration, assertToken);
    var newDocument = editor.GetChangedDocument();

    return newDocument;
}

What I'm trying to achive is

[Test]
public void blah()
{
    var stat = string.Empty;
}

Becomes

[Test]
public void blah()
{
    var stat = string.Empty;
    Assert.AreEqual("", "");
}

When you press ctrl+. on "stat"... And it is here that VS2015 gives the error, just not first time...

Upvotes: 5

Views: 235

Answers (3)

Wilsade
Wilsade

Reputation: 51

It also happened with me. I tried to find an answer in other forums with no luck. My workaround was incrementing the vsixminifest Version number.

Hope it helps. Thanks, Wilsade

Upvotes: 0

Maxence
Maxence

Reputation: 13286

Don't know why, but I've found that deleting the %localappdata%\Microsoft\VisualStudio\14.0Exp folder helps.

Upvotes: 2

Joshlo
Joshlo

Reputation: 794

Not sure what was wrong, since it only failed in debug. looked like a VS bug when running in debug

Upvotes: 0

Related Questions