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.