jmoreno
jmoreno

Reputation: 13561

Code fix for document different from code fix for line

I have written a Roslyn code Analyzer and related CodeFixProvider, which works. When I use it against individual lightbulbs it works perfectly, when I choose to do the whole document, it seems to be getting corrupted results as if several fixes were merged together.

I am using the WellKnownFixAllProviders to provide the infrastructure for fixing the problem. Debugging the code and what is happening, all looks fine, but the previewed (and accepted) document seems to have some duplicated or corrupted results.

Upvotes: 0

Views: 60

Answers (1)

jmoreno
jmoreno

Reputation: 13561

The WellKnownFixAllProvider works by batching all the fixes in parallel, passing the same, immutable, document. Each fix returns the result of a single change to the original document and those changed documents are merged together to produce the final result. This means that if the fixes overlap the merge can produce results that would not happen if the document was mutated in sequence.

The only real cure for this is to either not have overlapping fixes or to write your own fixallprovider that operates in sequence instead of in parallel.

If the CodeFixProvider is being used for a one shot change to your codebase, it might be possible to hack a workaround where your fix provider keeps track of it’s changes and doesn’t produce conflicting changes. But that would be inherently fragile and not so thing you’d want to do for a fix that would be used by the general public.

Upvotes: 0

Related Questions