0

I have a CodeFix that gets applied "randomly" in Unit Testing. I have 46 unit tests for a single code fix and 20 fail. What is strange is that when I look at the Document I return in every case it is correct at the end of my CodeFix. but when I look at the end of ApplyFix sometime the document is correct and other times it is unchanged.

I have tried looking at the Document at the end of the CodeFix before I return it. After CodeAction.GetOperationsAsync is call my RegisterCodeFixesAsync is called and I see it register the fix. I then see my fix executed. Followed by the second line in ApplyFix

   Private Shared Function ApplyFix(document As Document, codeAction As CodeAction) As Document
        Dim operations As ImmutableArray(Of CodeActionOperation) = codeAction.GetOperationsAsync(CancellationToken.None).Result
        Dim solution As Solution = operations.OfType(Of ApplyChangesOperation).Single.ChangedSolution
        Return solution.GetDocument(document.Id)
    End Function

The code below is always executed

    context.RegisterCodeFix(CodeAction.Create(title, createChangedDocument:=Function(c As CancellationToken) AddAsClauseAsync(context.Document, DirectCast(declarationSyntax, VariableDeclaratorSyntax), c), equivalenceKey:=title), firstDiagnostic)

My fix gets called next and return the fixed document

    Dim NewDocument As Document = _Document.WithSyntaxRoot(newRoot)
    Return NewDocument

In every case I expect that when I look at solution.GetDocument(document.Id) I would see my fix but that only happens in ~50% of the tests, the failing tests just return the original document unchanged.

Paul Cohen
  • 241
  • 4
  • 14
  • Do any of these problems that need fixing overlap? – jmoreno Apr 12 '19 at 11:13
  • Great question, its very possible there is more then one issue in the document that this CodeFix could fix. But when I return from the codefix the document looks correct for for a single fix. It gets called again if there is another thing it needs to fix.It In the GUI of VS there is nothing show in the Preview Window when this happens. – Paul Cohen Apr 13 '19 at 06:43
  • After further debugging, I rolled back to a working version of the code then updated NuGet packages until things broke. All tests pass with Roslyn CodeAnalysis version 2.4 and 9 tests of 46 fail with version 2.9. – Paul Cohen Apr 17 '19 at 03:32

0 Answers0