Open aolszowka opened 5 years ago
I debugged through this and there's definitely something strange going on. I don't know the root cause, but teh symptom is that the formatter ends up taking the //
trivia off of the EOF token and instead add's it to the end of the PropertyDeclaration
node. This is an invalid tree that would not have been generated by the parser. as such, the formatter is violating our lexical/syntactic invariants.
That said, i don't know how important this is to fix. But we would certainly take a small fix here that addressed things if done properly.
@aolszowka I'd recommend using Formatter.GetFormattedTextChanges
instead of Formatter.Format
. This will allow you to look at the actual changes instead of an intermediate state that isn't directly used.
It would still be good to have it produce the correct tree for this case though.
@sharwell Doesn't that only give me the list of changes that need to be made? In the above I need the ability to make the fixes and push them back up as a PR. Or are you saying do both?
You can take the edits, tehn apply them to the text of the document to get the new document state that can be "push[ed] back up". :)
It depends on the application. If you need syntax trees and you need them to be accurate, you can use an approach like this:
Specifically, you can start with an initialTree
and obtain a truly-correct recreatedTree
using this:
The recreatedTree
would not fail the IsEquivalentTo
test.
tbh we don't care about the syntax trees honestly only the final product.
The way it works in production is we run the tool, if it returns false, tell git to generate a PR based on the changes for that file.
Thank you for the links though, I'll dig on it!
I have written a small utility to enforce some formatting rules using the Roslyn Formatter; after putting it in CI under our code base a single file kept popping out; the resulting format resulted in no changes which may indicate that there is an issue somewhere in SyntaxNode.IsEquivalentTo(SyntaxNode).
I stripped down our utility program to the bare minimum and ran the trouble file through Delta Debugging (https://www.st.cs.uni-saarland.de/dd/) using this utility which produced this nonsensical minimal test case, any removal of any character in this file will cause it to pass (I have also attached the file just in case this is something weird with the file format it is minifail.cs in the zip file).
Below is the test program (also included in the ZIP file) that will help reproduce the issue
RoslynRoundTripBug.zip
The only other reference to round tripping issues I can find that might be related are #30212 but that is on Linux (we're on Windows).